#!/bin/sh
#
#
local_ftp="/usr/bsd/ftp"
getting_program_args=0
getting_input_args=0
getting_output_args=0
program_args=""
num_program_args=0
output_count=0
remote_output_list=""

echo 'entering grami_test_ftp' > grami_test_ftp.log
/usr/bin/env >> grami_test_ftp.log
echo "pwd = " >> grami_test_ftp.log
pwd >> grami_test_ftp.log
echo "USER = ${USER}" >> grami_test_ftp.log
echo "before HOST = ${HOST}" >> grami_test_ftp.log
HOST=flash
echo "after HOST = ${HOST}" >> grami_test_ftp.log
#
for x in $*; do
  if test $getting_program_args -eq 0; then
     if test $x = "-program"; then
        getting_program_args=1
        getting_output_args=0
        getting_input_args=0
        echo 'found -program' >> grami_test_ftp.log
        continue
     elif test $x = "-input"; then
        getting_output_args=0
        getting_input_args=1
        echo 'found -input' >> grami_test_ftp.log
        continue
     elif test $x = "-output"; then
        getting_input_args=0
        getting_output_args=1
        echo 'found -output' >> grami_test_ftp.log
        continue
     fi
  fi
  if test $getting_program_args -eq 1; then
     num_program_args=`expr $num_program_args + 1`
     if test $num_program_args -eq 1; then
        echo 'this is the executable' >> grami_test_ftp.log
     else
        echo 'this is an executable arg' >> grami_test_ftp.log
     fi
     program_command="$program_command $x"
  elif test $getting_input_args -eq 1; then
     url_str="`echo $x \
          | sed 's-\([^/]*\)\(/.*\)/\([^/]*\)$-\1:\2:\3-'`"

     remote_mach="`echo $url_str | awk -F: '{print $1}`"
     remote_dir="`echo $url_str | awk -F: '{print $2}' | sed 's-^/--'`"
     remote_file="`echo $url_str | awk -F: '{print $3}'`"

     echo "$remote_mach" >> grami_test_ftp.log
     echo "$remote_dir" >> grami_test_ftp.log
     echo "$remote_file" >> grami_test_ftp.log
 
     if [ "$x" = "$url_str" ]; then
        echo "Malformed URL --> $x" >> grami_test_ftp.log
     fi

     if [ "${remote_dir}X" = "X" ]; then
        remote_dir="."
     fi

     echo 'found input arg transferring file' >> grami_test_ftp.log
     $local_ftp -n $remote_mach <<-GET_END
        user anonymous ${USER}@${HOST}
        cd $remote_dir
        binary
        get ${remote_file}
        quit
GET_END
  elif test $getting_output_args -eq 1; then
     echo 'this is an output arg' >> grami_test_ftp.log
     remote_output_list="$remote_output_list $x"
  else
     echo 'this is an unknown arg' >> grami_test_ftp.log
  fi
  echo "arg ---> $x" >> grami_test_ftp.log
done
#
echo "" >> grami_test_ftp.log
echo "HOME = ${HOME}" >> grami_test_ftp.log
echo "USER = ${USER}" >> grami_test_ftp.log
echo "PWD  = ${PWD}" >> grami_test_ftp.log
echo "" >> grami_test_ftp.log
for x in $program_command; do
   echo "x = $x" >> grami_test_ftp.log
   echo "x = $x"
#   if test ! -a $$x; then
#      echo "file $x not found" >> grami_test_ftp.log
#      echo "program will not be executed" >> grami_test_ftp.log
#      echo "file $x not found"
#      echo "program will not be executed"
#      exit 1
#   fi
done
#
echo "running command ----> $program_command" >> grami_test_ftp.log
$program_command
echo "" >> grami_test_ftp.log
#
for x in $remote_output_list; do
   url_str="`echo $x \
        | sed 's-\([^/]*\)\(/.*\)/\([^/]*\)$-\1:\2:\3-'`"

   remote_mach="`echo $url_str | awk -F: '{print $1}`"
   remote_dir="`echo $url_str | awk -F: '{print $2}' | sed 's-^/--'`"
   remote_file="`echo $url_str | awk -F: '{print $3}'`"

   if [ "$x" = "$url_str" ]; then
      echo "Malformed URL --> $x" >> grami_test_ftp.log
   fi

   if [ "${remote_dir}X" = "X" ]; then
      remote_dir="."
   fi

   echo 'transferring output file' >> grami_test_ftp.log

   $local_ftp -n $remote_mach <<-PUT_END
       user anonymous ${USER}@${HOST}
       cd $remote_dir
       binary
       put ${remote_file}
       quit
PUT_END
   echo "output file tranfer completed." >> grami_test_ftp.log
   echo "machine --> $remote_mach" >> grami_test_ftp.log
   echo "dir     --> $remote_dir" >> grami_test_ftp.log
   echo "file    --> $remote_file" >> grami_test_ftp.log
done
