#!/bin/sh
#transferDevRcp

# NOTE: This is a sample transfer device for illustration purposes.
# It does not currently have adequate error handling.

# Preferance variables that control functionality.
login=$USER	# Can set to be guest

hostName=`expr \`basename $0\` : '.*\.\(.*\)'`

if [ -z "$hostName" -a $1 != Action ]; then
    hostName="a remote machine"
fi

case $1
    in
	"menu")
	    echo "import Get files from $hostName"
	    echo "export Send files to $hostName"
	;;
	"versionsOK")
	    echo "remote local"
	;;
	"query")
	    #   Ask whether to copy files in or out
	    wsh -C 6,32,3,32 -t "RCP files"\
	    -n "transfer" -p 400,400 -s 65,14 -m 120,40 -H -r1000 \
	    -c $0 Action query
	    ;;
	"import")
	    #	If a directory is selected, then cd there first.
	    if [ "$SELECTED" ]; then
		set -- $SELECTED
		if [ $# -gt 1 ]; then
		    inform "There are too many selections."
		    exit 1
		fi
		# We only allow to copy to a directory, not a specific file.
		if [ -d $SELECTED ]; then
		    cd $SELECTED
		else
		    inform "Selected item is not a directory."
		    exit 1
		fi
	    fi
	    wsh -C 6,32,3,32 -t "RCP files from $hostName"\
		-n "transfer" -p 400,400 -s 65,14 -m 120,40 -H -r1000 \
		-c $0 Action import
	;;
	"export")
	    wsh -C 6,32,3,32 -t "RCP files to $hostName" \
		-n "transfer" -p 400,400 -s 65,14 -m 120,40 -H -r1000 \
		-c $0 Action export
	;;

	"Action")
	    case $2
		in
		    "query")
			while true; do
			    tput bold
			    echo "Copy in (i) or out (o)? \c"
			    tput rmul
			    read choice
			    case $choice in
			        i|I) exec $0 Action import ;;
			        o|O) exec $0 Action export ;;
			    esac
			done
			;;
		    "import")
			if [ -z "$hostName" ]; then
			    tput bold
			    echo "Enter the name of a remote machine:"
			    tput rmul
			    read hostName
			fi
			tput bold
			echo "Enter the name of a file or directory on $hostName to copy:"
			tput rmul
			read fileName

			# set up the path as rcp would for $login
			case $fileName
			    in
				~* | /*)
				    # leave as is.
				    ;;
				*)
				    fileName="~$login/$fileName"
				    ;;
			esac

			echo
			echo "Attempting an rcp as $login."

			# Try as guest if no permission for user.
			# Permission check for machine, not file.
			errStr=`rsh "$hostName" -l "$login" echo 2>&1`
			success=$?
			echo "$errStr"
			if [ $success -ne 0 ]; then
			    if [ "$errStr" = "Permission denied." -o \
				    "$errStr" = "Login incorrect." ]; then
				login=guest
				echo
				echo "Attempting an rcp as guest."
				success=0;
			    fi
			fi

			# RCP for real (if it makes sense).
			if [ $success -eq 0 ]; then
			    rcp -rv "$login@$hostName:$fileName" .
			    success=$?
			fi
		    ;;
		    "export")
			if [ -z "$hostName" ]; then
			    tput bold
			    echo "Enter the name of a remote machine:"
			    tput rmul
			    read hostName
			fi

			while [ -z "$SELECTED" ]; do
			    tput bold
			    echo "enter the name of a local file or directory to write:"
			    tput rmul
			    read SELECTED
			done
			tput bold
			echo "Enter the name of a file or directory on $hostName to write:"
			tput rmul
			read fileName

			# set up the path as rcp would for $login
			case $fileName
			    in
				~* | /*)
				    # leave as is.
				    ;;
				*)
				    fileName="~$login/$fileName"
				    ;;
			esac

			echo
			echo "Attempting an rcp as $login."

			# Try as guest if no permission for user.
			# Permission check for machine, not file.
			errStr=`rsh "$hostName" -l "$login" echo 2>&1`
			success=$?
			echo "$errStr"
			if [ $success -ne 0 ]; then
			    if [ "$errStr" = "Permission denied." -o \
				   "$errStr" = "Login incorrect." ]; then
				login=guest
				echo
				echo "Attempting an rcp as guest."
				success=0;
			    fi
			fi

			# RCP for real (if it makes sense).
			if [ $success -eq 0 ]; then
			    rcp -rv $SELECTED "$login@$hostName:$fileName"
			    success=$?
			fi
		    ;;
	    esac
	    echo
	    tput bold
	    if [ $success -ne 0 ]; then
		echo NOT COMPLETED
	    else
		echo SUCCESSFULLY COMPLETED
	    fi
	    tput rmul
	    exit 0
	;;
	*)
	    echo Invalid argument: $1 1>&2
	    exit 1
	;;
esac
exit 0
