#!/bin/ksh
. ./settings.ksh

#-----------------------------------------------------------------------------------
#ftpcheck [-in]
# i - interactive flag for setting remotehost, user, password, and idlhost, idluser
# n - turn off ftp
#-----------------------------------------------------------------------------------
debugflag=1
#-------------------------------------------
#Begin User edit Section
#-------------------------------------------
#-------------------------------------------------------------------
#command arguments retrievment
#felix add for retrieve command line arguments
#usage: ftp_check1 -D[path] -L[label] -m 
#       path store in destpath
#       label stroe in destlabel
#       m means no menu
#example: ftp_check1 -D/clincal -L/patient101 -m
#-------------------------------------------------------------------

   Rflag=
   Dflag=
   Lflag=
   mflag=0
   while getopts R:D:L:m name
   do
     case $name in
       R) Rflag=1
       	  Rval="$OPTARG";;
       D) Dflag=1
       	  Dval="$OPTARG";;
       L) Lflag=1
          Lval="$OPTARG";;
       m) mflag=1;;
       ?) printf "Usage: %s: [-D path] [-L label] -m args\n" $0
          exit 2;;
     esac
   done
#echo Rval is $Rval
#echo Dval is $Dval
#echo Lval is $Lval
#echo mflag is $mflag
#echo name is $name
#echo '***'
destpath=$Dval
subdir=$destpath
destlabel=$Lval
#if [! -z "$mflag" ];then 
#    nomenu=1
#fi

#   if [ ! -z "$Dflag" ]; then
#     printf 'Option -D "%s" specified\n' "$Dval"
#   fi
#   if [ ! -z "$Lflag" ]; then
#     printf 'Option -L "%s" specified\n' "$Lval"
#   fi
#   if [ ! -z "$mflag" ]; then
#     printf 'Option -m specified\n'
#   fi
   shift `expr $OPTIND - 1`
#   printf "Remaining arguments are: %s\n" "$*"
#end felix add-------------------------------------------------------

#-------------------------------------------
#autoftp settings
#enter info for remotehost, user, password
#-------------------------------------------
ftpflag=1
remotehost=`echo $ftpserver`
remotehostname=`echo $ftpservername`
user=`echo $ftpuser`
#---------------------------------------------
#Enter your password for this user here
#If you prefer to be prompted everytime for it
# then either comment this line out
# or leave it as:  pass=`echo `
#---------------------------------------------
pass=`echo $ftppass`

#----------------------------------------------------
#autorecon settings
#enter info for idlhost and user
#your .rhosts file must allow magnet access for this
#----------------------------------------------------
idlhost=`echo $ftpserver`
idluser=$user
idlhostname=`echo $ftpservername`


#----------------------------------------------------
#Do Not edit the next 2 lines
#----------------------------------------------------
#rawdir=/usr/g/mrraw/
rawdir=$MOJOE_RAW
if (test ${#rawdir} -lt 1)
    then
    rawdir=/usr/g/mrraw/
fi
label=`date '+%m_%d_%y'`
#mojoe_dir=$HOME/mojoe
mojoe_dir=$MOJOE_DIR
if (test ${#mojoe_dir} -lt 1)
    then
    mojoe_dir=$HOME/mojoe
fi


#-------------------------------------------
#Set remote directories to try for ftping
#This is where you define your RAID device
#-------------------------------------------
remotedir1=`echo /raid/neuro`
remotedir2=`echo /raid/neuro`
remotedir3=`echo /raid/neuro`
remotedir4=`echo /raid/neuro`
#remotedir2=`echo /fmri/fmri/DATA`
#remotedir3=`echo /fmri2/fmri/DATA`
#remotedir4=`echo /fmri2/fmri/DATA`
#remotedir3=`echo /fmri3/fmri/DATA`
#remotedir4=`echo /fmri4/fmri/DATA`

if (test ${#Rflag} -gt 0) then
    diskpath=$Rval
    destpath=$Dval
    remotedir1=$diskpath
    remotedir2=$remotedir1
    remotedir3=$remotedir1
    remotedir4=$remotedir1
    subdir=$destpath
    echo "setting disk remotedir, subdir to:" $remotedir1, $subdir
    whosedir=`echo $Dval | tr '/' '\t' | cut -f1`
    whoseexist=`ssh -l $user $remotehost 'test -d ' $Rval/$whosedir';echo $?'`
    if (test $whoseexist -ne 0) then
	echo ERROR!!! No user directory on specified disk!!! FTP-CHECK failure!!!
	errorcode=`echo no_user_disk_dir` 
	ftpflag=0
	writeflag=0
	k1=`echo $@ | grep w`
	if (test "$k1") then writeflag=1; fi
	if (test $writeflag -eq 1) then
	    echo "reply: $ftpflag $errorcode $remotehost $remotehostname $user $pass $remotedir $avail $idlhostname $idlhost $idluser " >$mojoe_dir/secretpassword
	fi
	exit
    fi
fi

#------------------------------------------------------------
#Set the menu subdirectories to choose from
#Note: these subdirectories should exist in  the
#remote directories listed above
#Right now there is a limit of 26 on subdirs
#This can be increased by editing the menu shell script
#These are unix directory names, so make sure they exist
# and no spaces or funny characters
#Set the otherflag=1 if you want a user editable entry
#------------------------------------------------------------
menuflag=0
if (test ${#destpath} -eq 0) 
    then 
	menuflag=1
fi

otherflag=1
subdir1='clinical'
subdir2='plaurien'
subdir3='maldjian'
subdir4='bkraft'
subdir5='burdette'
subdir6='moody'
subdir7='chepuri'
subdir8='flowers'
subdir9='baker'
subdir10='ppg'
subdir11='diffusion'
subdir12='jzhu'
subdir13='monkey_data'
subdir14='porrino'
subdir15='dagenbach'
subdir16='coghill'





#------------------------------------------
#set minimum space required for ftping (700MB)
#------------------------------------------
minimum=`echo 700000`

#-------------------------------------------
#End User Edit Section
#-------------------------------------------

#******************************************
#DO NOT EDIT BELOW THIS LINE
#******************************************

tr=`echo tr`
awk=`echo awk`

#-------------------------------------------------------------
#executable definitions
#--------------------------------------------------------------

autoftp=$mojoe_dir/autoftp
imenu=$mojoe_dir/imenu



#------------------------------------------
#Check if this is a 5X system
#------------------------------------------
OS=`uname`
if test $OS = SunOS
	then
	tapedev=/dev/nrst8
	bf=96
	echo SunOS detected
	tr=`echo /usr/xpg4/bin/tr`
	awk=`echo /usr/xpg4/bin/awk`
fi



outputf=`echo outputfile$$`
title=`echo Select a Subdirectory for FTP`
if (test $otherflag -eq 1) then other=`echo -other`; fi
if (test $menuflag -eq 1)
 then 
	$imenu title=$title choices=$subdir1,$subdir2,$subdir3,$subdir4,$subdir5,$subdir6,$subdir7,$subdir8,$subdir9,$subdir10,$subdir11,$subdir12,$subdir13,$subdir14,$subdir15,$subdir16 $other file=$outputf
	subdir=`cat $outputf`
	if (test ${#subdir} -gt 0) 
	then 
		slash_test=`echo $subdir | $awk '{print substr($1,1,1)}' `
		if (test $slash_test = '/') 
			then
			remotedir1=$subdir
			remotedir2=$subdir
			remotedir3=$subdir
			remotedir4=$subdir
			subdir=''
		else
			subdir='/'$subdir
		fi
	fi
	unalias rm
	rm $outputf
fi


errorcode=`echo OK`
#----------------------------------------------
#Check command line flags for interactive mode
#---------------------------------------------

k=`echo $@ | grep n`
if (test "$k") then ftpflag=0; fi
if (test $ftpflag = 1)
	then
	k=`echo $@ | grep i`
	if (test "$k") then 
		echo "Enter remote hostname: \c  "
		read remotehostname
		echo "Enter remote host ip address: \c  "
		read remotehost
		echo "Enter username for $remotehost: \c  "
		read user
		echo "Enter password for $user: \c "
		stty -echo
		read pass
		stty echo
		echo ""
		echo "Enter ftp directory1 for $remotehost: \c  "
		read remotedir1
		echo "Enter ftp directory2 for $remotehost: \c  "
		read remotedir2
		remotedir3=$remotedir2
		remotedir4=$remotedir2
		echo "Enter idl hostname: \c  "
		read idlhostname
		echo "Enter idlhost ip address: \c  "
		read idlhost
		echo "Enter idlx username for $idlhost: \c  "
		read idluser
	fi
fi

#-------------------------------
# If password is not hardcoded in
#  then prompt for one
#------------------------------

if (test "$pass") then donothing=`echo `; else
	echo "Enter password for user $user on $remotehostname ($remotehost): \c "
	stty -echo
	read pass
	stty echo
	echo ""
fi

#***************************************************
#felix modified 2/6/03
#to get subfolder name from command line arguments
#creat on the remotehost
#***************************************************
if (test ${#destpath} -eq 0) 
then
    #interactive mode
    destpath=$subdir
fi

##put the path into a array p
set -A patharray `echo $destpath | awk '{split($0,a,"/");for(i=1;i<=10;i++) print a[i]}'`
 
##i is a flag, only test disk free space for the top of subdirectory
i=`echo 0`

#***************************************************
#test remote host disk space and create subfolder
#***************************************************
for subdir in ${patharray[@]}
do     
    if (test "$i" -eq 0)
    then
	#**following haven't been modified**********
	remotedir=$remotedir1/$subdir
	avail=`ssh -l $user $remotehost df -k $remotedir  | $awk '{print $4}' | $tr -cd '0-9 '`
	if (test "$avail" -lt $minimum) then
	    if (test "$debugflag" -eq 1) then echo "Insufficient Space on $remotedir (available: $avail , minimum: $minimum)"  ; fi;
	echo "trying $remotedir2/$subdir..."
	echo

	remotedir=$remotedir2/$subdir
	avail=`ssh -l $user $remotehost df -k $remotedir  | $awk '{print $4}' | $tr -cd '0-9 '`
	if (test "$avail" -lt $minimum) then
	    if (test "$debugflag" -eq 1) then echo "Insufficient Space on $remotedir (available: $avail , minimum: $minimum)"  ; fi;
       	echo "trying $remotedir3/$subdir..."
       	echo
       	remotedir=$remotedir3/$subdir
       	avail=`ssh -l $user $remotehost df -k $remotedir | $awk '{print $4}' | $tr -cd '0-9 '`
       	if (test "$avail" -lt $minimum) then
	    if (test "$debugflag" -eq 1) then echo "Insufficient Space on $remotedir (available: $avail , minimum: $minimum)"  ; fi;
       	echo "trying $remotedir4/$subdir..."
       	echo
       
	remotedir=$remotedir4/$subdir
       	avail=`ssh -l $user $remotehost df -k $remotedir  | $awk '{print $4}' | $tr -cd '0-9 '`
       	echo
       	fi
	fi
	fi
	if (test "$avail" -lt $minimum) then
	ftpflag=0
	errorcode=`echo disk_space`
	echo "*************************************************************************"
	echo "Insufficient Disk Space on $remotehostname ($remotehost)"
	echo "Minimum: $minimum bytes"
	echo "Available: $avail bytes"
	echo "Autoftp Off"
	echo "*************************************************************************"
    fi
    #****end of unmodified section*********************************
    i=$i+1
    tempdir=$remotedir
else
    tempdir=$tempdir/$subdir
    exist=`ssh -l $user $remotehost 'test -d ' $tempdir';echo $?'`
    if (test $exist -eq 0)
    then
	remotedir=$tempdir 
    else
	ssh -l $user $remotehost 'mkdir' $tempdir
	remotedir=$tempdir
    fi
fi
done
#************************************************************************
#* End of felix modification*********************************************
#************************************************************************


if (test $ftpflag = 1)
	then
	#---------------------------------
	#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 
		errorcode=`echo login_error`
		error_msg=`echo "error on login or password "`
		ftpflag=0
	fi
	if (test "$server_error") then
		errorcode=`echo hostname_error` 
		error_msg=`echo "error on hostname"`
		ftpflag=0
	fi
	if (test $ftpflag = 0)
		then
		if (test "$debugflag" -eq 1) then
			echo "*************************************************************************"
			echo "ERROR:  No FTP Connection"
			echo $error_msg
			echo $ftptest
			echo "*************************************************************************"
		fi
	fi

fi

if (test $ftpflag = 1)
	then
	if (test "$debugflag" -eq 1) then 
		echo "*************************************************************************"
		echo "Autoftp:   Connection Active"
		echo "Hostname:  $remotehostname ($remotehost)"
		echo "Directory: $remotedir"
		echo "Available: $avail bytes"
		echo "Data will be automatically ftped to $remotedir on server $remotehostname  ($remotehost) "
		echo "*************************************************************************"
	fi
fi

verboseflag=0
k=`echo $@ | grep verbose`
if (test "$k") then verboseflag=1; fi
if (test $verboseflag -eq 1) then
	#echo "[37m"
	echo reply: $ftpflag $errorcode $remotehost $remotehostname $user $pass $remotedir $avail $idlhostname $idlhost $idluser 
	#echo '[0m'
fi

writeflag=0
k1=`echo $@ | grep w`
if (test "$k1") then writeflag=1; fi
if (test $writeflag -eq 1) then
	echo "reply: $ftpflag $errorcode $remotehost $remotehostname $user $pass $remotedir $avail $idlhostname $idlhost $idluser " >$mojoe_dir/secretpassword
fi
exit
