#!/sbin/sh
####

usage()
{
	echo "Usage $0 [-v] [-h hostname] [-t tapedevice]"
	exit
}

####

TP="/dev/tape"
HN="localhost"
FILE=""
bruv= tarv= cpiov=   # verify options

####
if [ $# -gt 0 ]
then
	while [ $1 = "-h" -o $1 = "-t" -o $1 = "-v" ]
	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
		if [ $1 = "-v" ]
		then
			shift
			bruv=d tarv=C cpiov=TT   # verify options
		fi
	done
fi

####

if [ $HN = "localhost" ]
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 guest@$HN -n 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 = "localhost" ]
then
	TPDEV="$TP"
else
	TPDEV="guest@$HN:$TP"
fi


# function to determine tape type.
# assumes blocksize never > 256KB
get_backup_type() {
	rsh guest@$HN -n dd if=$TP bs=256k count=1 > /tmp/$$type 2>/dev/null
	LANG=C ttype="`file /tmp/$$type`"
	case "$ttype" {
	*bru*) echo Backup is a bru archive
		labelcmd="bru -g -f /tmp/$$type"
		listcmd="bru -tvj$bruv -f $TPDEV"
		;;
	*xfsdump*) echo Backup is an xfsdump archive
		labelcmd=
		# no verify option
		listcmd="xfsrestore -v verbose -t -f $TPDEV"
		;;
	*cpio*) echo Backup is a cpio archive
		labelcmd="cpiolabel /tmp/$$type"
		listcmd="cpio -itvk$cpiov -I $TPDEV"
		;;
	*tar*) echo Backup is a tar archive
		labelcmd="tarlabel /tmp/$$type"
		listcmd="tar -vtRf$tarv $TPDEV"
		;;
	*) echo Unable to determine backup type, assuming cpio
		labelcmd="cpiolabel /tmp/$$type"
		listcmd="cpio -itvk$cpiov -I $TPDEV"
		;;
	}
}

# get label from a tar tape.
# since tar has no real labels, we simply fake this one.
# if it's written in the "sgi system backup format", it
# has such a label, otherwise we fake it.
# basicly, we assume the first file is small, and extract it.
tarlabel()
{
		file="`tar Rtf $1 2>&1 | sed -n 1p`"
		if [ $? -ne 0 ] #{
		then
			echo "Error trying to read label file"
			return 0
		fi #}
		# get the label file; first file on tape
		(cd /; tar Rxf $1 $file 2>/dev/null; if [ -f "$file" ]; 
			then cat $file; rm -f $file; fi)
		return 1;
}

# get label from a cpio tape.
# since tar has no real labels, we simply fake this one.
# if it's written in the "sgi system backup format", it
# has such a label, otherwise we fake it.
# basicly, we assume the first file is small, and extract it.
cpiolabel()
{
		file="`cpio -ti -I $1 2>&1 | sed -n 1p`"
		if [ $? -ne 0 ] #{
		then
			echo "Error trying to read label file"
			return 0
		fi #}
		# get the label file; first file on tape
		(cd /; cpio -i -I $1 $file >/dev/null 2>&1; if [ -f "$file" ]; 
			then cat $file; rm -f $file; fi)
		return 1;
}

trap 'rm -f /tmp/$$type /tmp/$$lab; trap 0; exit 0' 0
trap 'echo Interrupted; rm -f /tmp/$$type; trap 0; exit 1' 1 2

echo ""
echo "Tape listing started.  Please wait..."

get_backup_type
$labelcmd | tee /tmp/$$lab

# if they asked for verify, try to start from same dir.  This shouldn't
# be an issue if made through Backup, but may be done through other scripts
if [ -n "$cpiov" ]
then
	sdir="`sed -n /label:/'s/.* //p' /tmp/$$lab`"
	if [ -n "$sdir" ]; then
		cd $sdir # may not work right if there was no label file...
	fi
fi
echo ""
$listcmd

echo ""
echo "Tape listing complete."
