#!/bin/sh
#transferDevCpioArchive
# cpio 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`
	cpioError=$?
	#   ----If errString is not NULL then the mt command did not even work.
	# If no permission to use as $USER, try as guest.
	if [ $cpioError -ne 0 -a \( "$errString" = "Permission denied." -o \
		"$errString" = "Login incorrect." \) ]; then
	    login=guest
	    errString=`mt -t $login@$hostName:$noRewindTapeDev exist 2>&1`
	    cpioError=$?
	    # Check if guest has permission.
	    if [ $cpioError -ne 0 -a \( "$errStrign" = "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
	cpioError=$?
    fi

    #	----Now we can check that the tape drive exists.
    if [ $cpioError != 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
}

cpioSession(){
    #	$1 action, $2 title
    #	----Go ahead with the appropriate action.
    curDate=`date "+%m%d%y:%H%M%S"`
    if [ $uniqueLog = true ]; then
	logSuffix=cpio_$1_log_$curDate
    else
	logSuffix=cpio_$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 "dir Listing of cpio Tape on $hostName"
		echo "import Read from cpio Tape on $hostName"
		echo "export Copy to cpio Tape on $hostName"
	    else
		echo "dir Listing of Local cpio Tape"
		echo "import Read from Local cpio Tape"
		echo "export Copy to Local cpio 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 cpio tape on $hostName"
	    else
		checkContinue "Read the cpio tape on the local machine"
	    fi

	    checkTape $1
	    if [ "$hostName" ]; then
		cpioSession $1 "Read from $hostName's cpio tape"
	    else 
		cpioSession $1 "Read from local cpio 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 cpio tape on $hostName"
	    else
		checkContinue "Write the cpio tape on the local machine"
	    fi

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

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

#   ----This is the case in which cpioArchive 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/cpioStat ]; then
	    touch /tmp/cpioStat
	    chmod +w /tmp/cpioStat
	fi

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

		gotoRoot=false
		# Pathnames will be either all relative or all full.
		for i in $SELECTED; do
		    case "$i"
			in
			/*)
			    relativeName=`echo $i | sed sr/rr1`
			    selection="$selection $relativeName"
			    gotoRoot=true;
			    ;;
		    esac
		done

		# Write all files as relative path names
		# (since no cpio option to read relative)
		if [ $gotoRoot = true ]; then
		    cd /
		    SELECTED="$selection"
		fi

		#----   Must use temp files to obtain the exit status.
		if [ $hostName ]; then
		    find $SELECTED -print | (cpio -ovC204800 \
			-O$login@$hostName:$tapeDev; echo $? > /tmp/cpioStat) \
			2>&1 | tee -a $3
		else
		    find $SELECTED -print | (cpio -ovC204800 -O$tapeDev; 
			    echo $? > /tmp/cpioStat) 2>&1 | tee -a $3
		fi
		cpioError=`cat /tmp/cpioStat`
		echo
		checkError $cpioError
		echo Finished writing to tape.
	    ;;

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

		echo Reading into `pwd`
		echo

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

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

		#   ----Must use tmp file to obtain exit status
		if [ $hostName ]; then
		    (cpio -itvC204800 -I$login@$hostName:$tapeDev; 
			    echo $? > /tmp/cpioStat) 2>&1 | tee -a $3
		else
		    (cpio -itvC204800 -I$tapeDev; 
			    echo $? > /tmp/cpioStat) 2>&1 | tee -a $3
		fi
		cpioError=`cat /tmp/cpioStat`
		echo
		checkError $cpioError
	    ;;
	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

