#!/bin/sh
#Tag 0x00000500
####

usage()
{
	echo "Usage $0 [-h hostname] [-t tapedevice] [directory_name/file_name]"
	exit
}
####
notabspath()
{
	echo "you need to specify the absolute pathname for $FILE"
	exit
}

####

RDIR=""
TP="/dev/tape"
HN="LOCAL"
FILE=""

####
if [ $# -gt 0 ]
then
	while [ $1 = "-h" -o $1 = "-t" ]
	do
		if [ $1 = "-h" ]
		then
			shift
			if [ $# -lt 1 ]
			then
				usage
			fi
			HN=$1
			shift
			if [ $# -lt 1 ]
			then
				break
			fi
		fi
		if [ $1 = "-t" ]
		then
			shift
			if [ $# -lt 1 ]
			then
				usage
			fi
			TP=$1
			shift
			if [ $# -lt 1 ]
			then
				break
			fi
		fi
	done
fi

####

RDIR="$@"
while [ $# -gt 0 ]
do
	FILE=$1
	shift
	case "$FILE" in
		/*)
			continue ;;
		 *)	notabspath ;
			break ;;
	esac
done

####

if [ $HN = "LOCAL" ]
then
	ret=`mt -t $TP status 2>&1`
else
	ret=`(/usr/etc/ping -s 1 -c 1 $HN 2>&1)`
	case "$ret" {
		*100%?packet?loss*)	echo "Error: Unable to communicate with host:" "$HN" ;
					exit ;;
		}
	ret=`(rsh $HN -l guest mt -t $TP status 2>&1)`
fi
case "$ret" {
	*Login?incorrect*)	echo "Error: guest login incorrect on:" "$HN" ;
				exit ;;
	*No?such?file*)		echo "Error: Tape device $TP not configured." ;
				exit ;;
	*Not?READY*)		echo "Error: There is no tape in the drive." ;
				exit ;;
	*Unknown?host*)		echo "Error: Host $HN is unknown." ;
				exit ;;
	*can*get?status*)	echo "Error: Cannot stat $TP." ;
				exit ;;
	*I/O?error*)		echo "Error: Unable to I/O on $TP." ;
				exit ;;
}
sleep 2
if [ $HN = "LOCAL" ]
then
	/usr/sbin/bru -g -f $TP > /dev/null 2>&1
else
	/usr/sbin/bru -g -f guest@$HN:$TP > /dev/null 2>&1
fi
if [ $? -ne 0 -a $? -ne 2 ]
then
	echo "Error: Unable to read the tape, check tape format."
	exit
fi
#
echo ""
echo "Restore started.  Please wait..."
trap 'exit' 2
if [ $HN = "LOCAL" ]
then
	/usr/sbin/bru -xv -ur -f $TP $RDIR
else
	/usr/sbin/bru -xv -ur -f guest@$HN:$TP $RDIR
fi

if [ $? = 0 ]
then
	echo "\nRestore completed successfully."
else
	echo "\nRestore finished; errors occurred during recovery\007"
fi
