#!/bin/sh
#transferDevTarArchive
# tar logical tape device
# Perform error checking and then read or write a collection of files.

# Preferance variables that control functionality.
uniqueLog=false
login=$USER

# --------------FUNCTIONS---------------------

#   This routine performs some basic error checking 
#   before we bring up a window.
#   $1:action to perform.
checkTape() {
    #	----First we want to see if the machine has a tape drive.
    #	----We use an mt command on either the remote or the local machine.
    if [ $hostName ]; then
	#   ----Remote
	errString=`mt -t $login@$hostName:$noRewindTapeDev exist 2>&1`
	tarError=$?
	#   ----If errString is not NULL then the mt command did not even work.
	# If no permission to use as $USER, try as guest.
	if [ $tarError -ne 0 -a \( "$errString" = "Permission denied." -o \
		"$errString" = "Login incorrect." \) ]; then
	    login=guest
	    errString=`mt -t $login@$hostName:$noRewindTapeDev exist 2>&1`
	    tarError=$?
	    # Check if guest has permission.
	    if [ $tarError -ne 0 -a \( "$errString" = "Permission denied." -o \
		   "$errString" = "Login incorrect." \)  ]; then
		inform "You do not have permission to use the machine $hostName."
		exit 1
	    fi
	fi
	#   ----Local
    else
	mt -t $noRewindTapeDev exist
	tarError=$?
    fi

    #	----Now we can check that the tape drive exists.
    if [ $tarError != 0 ]; then
	#   ----Complain about there being no tape drive.
	if [ $hostName ]; then
	    inform "The machine $hostName does not have a tape drive."
	else
	    inform "This machine does not have a tape drive."
	fi
	exit 1
    fi

    #   ----Get info about the current state of the tape drive.
    if [ $hostName ]; then
	mt -t $login@$hostName:$tapeDev rew 2>/dev/null
	tapeInfo=`mt -t $login@$hostName:$tapeDev stat 2>&1`
    else
	mt -t $tapeDev rew 2>/dev/null
	tapeInfo=`mt -t $tapeDev stat 2>&1`
    fi
    cartridgeInfo=`echo $tapeInfo | sed -e 's/.*Media : //g'`
    ready=`echo "$cartridgeInfo" | cut -f1 -d","`

    #   ----Check that the tape is ready.
    if [ "$ready" != READY ]; then
	#   ----Complain about the tape not being ready.
	if [ $hostName ]; then
	    confirm -t"Either there is no tape in $hostName's drive or the door latch is opened."\
		-t "Please insert a tape, then retry your command." > /dev/null
	else
	    confirm -t"Either there is no tape in the drive or the door latch is opened."\
		 -t"Please insert a tape, then retry your command." > /dev/null
	fi
	exit 1
    fi

    #	----If we want to write, then see if the tape is writable.
    if [ $1 = write ]; then
	writable=`echo "$cartridgeInfo" | cut -f2 -d" "`
	if [ "$writable" != writable, ]; then
	    #	----Complain.
	    if [ $hostName ]; then
		confirm -t"The tape cartridge in $hostNames's drive is write protected."\
		     -t "Please insert an unprotected tape, then retry your comand." > /dev/null
	    else
		confirm -t"The tape cartridge is write protected."\
		     -t "Please insert an unprotected tape, then retry your comand." > /dev/null
	    fi
	    exit 1
	fi
    fi

    #   ----See if we can read from tape into the current directory.
    if [ $1 = read -a ! -w . ]; then
	#   ----Complain.
	inform "No permission to write in this directory."
	exit 1
    fi
}

tarSession(){
    #	$1 action, $2 title
    #	----Go ahead with the appropriate action.
    curDate=`date "+%m%d%y:%H%M%S"`
    if [ $uniqueLog = true ]; then
	logSuffix=tar_$1_log_$curDate
    else
	logSuffix=tar_$1_log
    fi
    logfile=$logDirectory/$logSuffix
    #	check that the directory is writable
    (cat /dev/null >> "$logfile") 2>/dev/null
    if [ $? -ne 0 ]; then
	check=`confirm \
	    -t"Unable to create $logSuffix in $logDirectory."\
	    -t"Accept will write $logSuffix in $HOME."\
	    -t"Cancel will continue with no log file."\
	    -b Cancel -b Accept`
	if [ $check = Cancel ]; then
	    logfile=/dev/null
	else
	    logfile="$HOME/$logSuffix"
	fi
    fi

    wsh -C 6,32,3,32  -t "$2" -n "transfer" -p 400,400 \
	-s 70,14 -m 120,40 -H -r 1000 -c $0 Action $1 $logfile $login
}

checkContinue() {
    check=`confirm -t"When your tape is properly loaded, click Accept to:"\
	    -t"$1."\
	    -b Cancel -b Accept`
    if [ $check = Cancel ]; then
	exit 1
    fi
}

#   ---------------FUNCTIONS used in the Action phase-------------------

checkError() {
if [ $1 != 0 ]; then
    tput bel
    tput rev
    echo THIS SESSION HAS NOT BEEN SUCCESSFULLY COMPLETED.
    tput rmul
    echo
fi
}

#   ----START OF MAIN

# scriptCall=`basename $0`
# hostName=`expr $scriptCall : '.*\.\(.*\)'`
logDirectory=`pwd`
if [ "$OZ_HOSTNAME" ]
then
	hostName=$OZ_HOSTNAME
fi
if [ "$OZ_TAPEDEV" ]
then
	tapeDev=$OZ_TAPEDEV
	if [ $tapeDev = "/dev/tape" ]
	then
		noRewindTapeDev=/dev/nrtape
	else
# The no rewind tape will not allways be tape devide with nr on the end.
# bug #204371	noRewindTapeDev="$tapeDev"nr
		noRewindTapeDev="$tapeDev"
	fi
else
	tapeDev=/dev/tape
	noRewindTapeDev=/dev/nrtape
fi

case $1
    in
	"menu")
	    if
		test $hostName
	    then
		echo "list Listing of tar Tape on $hostName"
		echo "read Read from tar Tape on $hostName"
		echo "write Copy to tar Tape on $hostName"
	    else
		echo "list Listing of Local tar Tape"
		echo "read Read from Local tar Tape"
		echo "write Copy to Local tar Tape"
	    fi
	;;
	"versionsOK")
	    echo "local remote"
	;;
	"read")
	    if [ "$SELECTED" ]; then
		set -- $* $SELECTED
		if [ $# -gt 2 ]; then
		    inform "There are too many selections."
		    exit 1
		fi
		if [ -d $SELECTED ]; then
		    cd $SELECTED
		else
		    inform "Selected item is not a directory."
		    exit 1
		fi
	    fi

	    # Put up a confirmer quickly.
	    if [ "$hostName" ]; then
		checkContinue "Read the tar tape on $hostName"
	    else
		checkContinue "Read the tar tape on the local machine"
	    fi

	    checkTape $1
	    if [ "$hostName" ]; then
		tarSession $1 "Read from $hostName's tar tape"
	    else 
		tarSession $1 "Read from local tar tape"
	    fi
	;;
	"write")
	    if [ -z "$SELECTED" ]; then
		inform "No files have been selected to write."
		exit 1
	    fi

	    # Put up a confirmer quickly.
	    if [ "$hostName" ]; then
		checkContinue "Write the tar tape on $hostName"
	    else
		checkContinue "Write the tar tape on the local machine"
	    fi

	    checkTape $1
	    if [ "$hostName" ]; then
		tarSession $1 "Copy to $hostName's tar tape"
	    else
		tarSession $1 "Copy to local tar tape"
	    fi
	;;
	"list")
	    # Put up a confirmer quickly.
	    if [ "$hostName" ]; then
		checkContinue "List the contents of the tar tape on $hostName"
	    else
		checkContinue "List the contents of the tar tape on the local machine"
	    fi

	    checkTape $1
	    if [ "$hostName" ]; then
		tarSession $1 "Listing of $hostName's tar tape"
	    else
		tarSession $1 "Listing of local tar tape"
	    fi
	;;

#   ----This is the case in which tarArchive calls itself inside of a wsh for
#   ----the second phase of operation.
	"Action")
# $2:cmd $3:logfile $4:login
login="$4"

	#   ----Make sure that a temp file is set up
	if [ ! -w /tmp/tarStat ]; then
	    touch /tmp/tarStat
	    chmod +w /tmp/tarStat
	fi

	case $2
	    in 
	    "write")
		echo
		echo TAR WRITE >> $3
		echo ---------- >> $3

		# Pathnames will be either all relative or all full.
		for i in $SELECTED; do
		    case "$i"
			in
			/*)
			    directoryName=`dirname "$i"`
			    fileName=`basename "$i"`
			    selection="$selection -C $directoryName $fileName"
			    ;;
			*)
			    selection="$selection $i"
			    ;;
		    esac
		done

		SELECTED="$selection"

		#----   Must use temp files to obtain the exit status.
		if [ $hostName ]; then
		    (tar cvf $login@$hostName:$tapeDev $SELECTED;
			    echo $? > /tmp/tarStat) 2>&1 | tee -a $3
		else
		    (tar cvf $tapeDev $SELECTED; 
			    echo $? > /tmp/tarStat) 2>&1 | tee -a $3
		fi
		tarError=`cat /tmp/tarStat`
		echo
		checkError $tarError
		echo Finished writing to tape.
	    ;;

	    "read")
		tput bold
		echo Accessing Tape
		echo
		tput rmul
		echo TAR READ >> $3
		echo ---------- >> $3

		echo Reading into `pwd`
		echo

		#   ----Must use tmp file to obtain exit status
		if [ $hostName ]; then
		    (tar xvopRf $login@$hostName:$tapeDev;
			    echo $? > /tmp/tarStat) 2>&1 | tee -a $3
		else
		    (tar xvopRf $tapeDev;
			    echo $? > /tmp/tarStat) 2>&1 | tee -a $3
		fi
		tarError=`cat /tmp/tarStat`
		echo
		checkError $tarError
		echo Finished reading from tape.
	    ;;

	    "list")
		tput bold
		echo Accessing Tape
		echo
		tput rmul
		echo TAR DIRECTORY >> $3
		echo ------------- >> $3

		#   ----Must use tmp file to obtain exit status
		if [ $hostName ]; then
		    (tar tvf $login@$hostName:$tapeDev;
			    echo $? > /tmp/tarStat) 2>&1 | tee -a $3
		else
		    (tar tvf $tapeDev;
			    echo $? > /tmp/tarStat) 2>&1 | tee -a $3
		fi
		tarError=`cat /tmp/tarStat`
		echo
		checkError $tarError
	    ;;
	esac

	if [ "$3" != "/dev/null" ]; then
	    echo "A log of this session has been written in:"
	    echo $3
	else
	    echo "No log file written this session."
	fi
	;;
	*)
	    echo Invalid argument: $1 1>&2
	    exit 1
	;;
esac
exit 0

