#!/usr/bin/ksh
#Program author:  J. Maldjian

#This is the autoftp script adapted from my proxyftp script
#It should live on the magnet
#autoftp  aw_fmri fmri lab /AW_FMRI/SMN patient_name /usr/mrraw Pfile_name
#---------------------------------------------------------------------------------
# -testflag
#To test the ftp connection:
#ftptest=`autoftp  $remotehost $user $pass $remotedir junk $rawdir junk2 -testflag`
#login_error=`echo $ftptest | grep failed`
#server_error=`echo $ftptest | grep unknown`
#if (test "$login_error") then echo failed; else echo success; fi
#---------------------------------------------------------------------------------

#$1= remote host	sox
#$2= remote user	dicom
#$3= remote passwd
#$4= remote directory	/IMAGE_ARCHIVE
#$5= remote target directory to make  data   patient_name
#$6= local directory /export/home/sdc/film  (ie of Pfile)
#$7= local files note wildcards ('*') must come in with single quotes
#[$8]= optional remote filename if different from local (and if not *); default is same name
#[$9]= optional transfer mode (default binary)

testflag=`echo $@ | grep testflag `

#--------------------------------------
#flag for performing a remote telnet
# and changing group write permission
# on newly created directories
# (requires rtelnet script)
#---------------------------------------
rtelnet_flag=1

#echo "This is an automated ftp script."
proxy=`echo `

#
# If no parameters are set. Display this command line help message
#

if [ $# -eq 0 ]; then
       echo "autoftp rhost user passwd remotedir remotemkdir localdir localfiles_to_put"
       exit 0;
fi


#
# getmode function is not used 
#

getmode () {
	mode="binary";}

dumpfile1 () {
        if [ ${rfile} ]; then
                echo "${mode}" >> ${buffer};
                echo "mput ${rfile} " >> ${buffer};
                echo "chmod 664 ${rfile}" >> ${buffer};
        fi;}

dumpfile2 () {
        if [ ${rfile} ]; then
                echo "${mode}" >> ${buffer};
 		echo "delete  ${tfile} " >> ${buffer};
                echo "put ${rfile} ${tfile} " >> ${buffer};
                echo "chmod 664 ${tfile}" >> ${buffer};
        fi;}


echo `whoami`

buffer=$HOME/buffer;
echo This is a test > $buffer
echo $buffer

set -f                                            # no globbing allowed
dir="temp";
file="temp";

#------------------------
#Open the FTP connection
#------------------------
site=${1};
echo "open $site" >> $buffer;
echo "user $2 $3" >> $buffer;


testflag2=${#testflag}
if test $testflag2 -eq 0
then
	echo "prompt" >> $buffer;
	#-----------------------------
	#lcd to the data directory
	#-----------------------------
	dir=${6};
	echo "lcd ${dir}" >> $buffer;

	#-----------------------------
	#cd to the remote directory
	#-----------------------------
	#echo -n "What remote directory? :>"
	rdir=${4}
	echo "cd ${rdir}" >> $buffer;
	#echo -n "What remote directory to make? :>"
	mkdir=${5}
	if [ ${mkdir} ]; then
		echo "mkdir ${mkdir}" >> $buffer;
		echo "cd ${mkdir}" >> $buffer;
	fi;
	#echo "proxy runique" >> $buffer;
	#echo -n "What files? :>";
	rfile=${7};
	if [ $# -eq 7 ]; then
	     tfile=$rfile;
	     else
	      tfile=${8};
	fi

	#---------------------
	#Set the transfer mode
	#----------------------
	mode="binary";
	if [ $# -eq 9 ]; then
	    mode=${9};
	fi

	star="*";
	if  [ "$rfile" = "$star" ]; then	
	#       getmode;
	       dumpfile1 "*" $mode $buffer;
	       break;
	       else
	#         getmode;
	         dumpfile2 $file $mode $buffer;
	fi;
fi
file="temp";
echo "bye" >> $buffer;

# Get the time from the user
# Unix can't handle colons

#echo -n "Give the time ( 400 is standard ) :>"
#read time;
#echo $time

# Write a mini-shellscript for at

#tmpfile=/tmp/ncftp.$$;

tmpfile=$HOME/ncftp.$$;
echo '#!/bin/sh' > $tmpfile;
#echo '/usr/ucb/ftp -ni << EOT' >> $tmpfile;             # -ni for passwd & *
echo 'ftp -ni << EOT' >> $tmpfile;             # -ni for passwd & *
cat $buffer >> $tmpfile;                                # dump buffer
echo 'EOT' >> $tmpfile;
#at ${time:="400"} < $tmpfile;

#echo 'Your requests have been saved in /usr/spool/cron/atjobs'
#echo 'If you want to see your at jobs, type at -l'


#-------------------------
# Delete the buffer file
#-------------------------
rm $buffer;


#------------------------------------
#Set group write permissions
#on any remote directories created
#using the rtelnet script
#------------------------------------
if (test $rtelnet_flag = 1) 
	then
	if [ ${mkdir} ]; then
		#echo "./rtelnet $1 $2 $3 'chmod g+s $rdir/$mkdir' 5" >> $tmpfile;
		echo "ssh -l $2  $1 'chmod g+s $rdir/$mkdir' " >> $tmpfile;

	fi;
fi



#-------------------
#Execute the script
#-------------------
chmod a+x $tmpfile
$tmpfile 

#-------------------------
# Delete the script 
#-------------------------
rm $tmpfile
exit 0;
