#!/bin/sh
#Tag 0x00000700
####

usage()
{
	echo "Usage $0 [-h hostname] [-t tapedevice] [-i] directory_name/file_name"
	exit 1
}
####
notabspath()
{
	echo "you need to specify the absolute pathname for $FILE"
	exit 1
}
####
nonrootincr()
{
	echo "you can only do incremental backup from root ( / )"
	exit 1
}
####

BDIR=""
TP="/dev/tape"
HN="LOCAL"
FILE=""
INCR="FALSE"

####

if [ $# -eq 0 ]
then
	usage
fi

####

while [ $1 = "-h" -o $1 = "-t" -o $1 = "-i" ]
do
	if [ $1 = "-i" ]
	then
		INCR="TRUE"
		shift
		if [ $# -lt 1 ]
		then
			usage
		fi
	fi
	if [ $1 = "-h" ]
	then
		shift
		if [ $# -lt 1 ]
		then
			usage
		fi
		HN=$1
		shift
		if [ $# -lt 1 ]
		then
			usage
		fi
	fi
	if [ $1 = "-t" ]
	then
		shift
		if [ $# -lt 1 ]
		then
			usage
		fi
		TP=$1
		shift
		if [ $# -lt 1 ]
		then
			usage
		fi
	fi
done

####

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

####

if [ "$BDIR" != "/" -a "$INCR" = "TRUE" ]
then
	nonrootincr
fi

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 1;;
		}
	ret=`(rsh $HN -l guest mt -t $TP status 2>&1)`
fi

case "$ret" {
	*Login?incorrect*)	echo "Error: guest login incorrect on:" "$HN" ;
				exit 1 ;;
	*No?such?file*)		echo "Error: Tape device $TP not configured." ;
				exit 1 ;;
	*Not?READY*)		echo "Error: There is no tape in the drive." ;
				exit 1 ;;
	*write?protected*)	echo "Error: The tape is write protected." ;
				exit 1 ;;
	*Unknown?host*)		echo "Error: Host $HN is unknown." ;
				exit 1 ;;
	*can*get?status*)	echo "Error: Cannot stat $TP." ;
				exit 1 ;;
	*I/O?error*)		echo "Error: Unable to I/O on $TP." ;
				exit 1 ;;
	}

####

sleep 2
echo ""
echo "Backup started.  Please wait..."
trap 'echo Backup interrupted; exit 1' 2
if [ "$BDIR" = "/" ]
then
    if [ "$INCR" = "TRUE" ]
    then
	INCR_DATE=`cat /etc/lastbackup`
	if [ $HN = "LOCAL" ]
	then
		/usr/sbin/bru -cvR -L"Incremental Backup of files modified since $INCR_DATE" -f $TP -n $INCR_DATE /
	else
		/usr/sbin/bru -cvR -L"Incremental Backup of files modified since $INCR_DATE" -f guest@$HN:$TP -n $INCR_DATE /
	fi
	exitstat=$?
    else
	/etc/dvhtool -v list | grep '[0123456789]' | sed -e /bsttab/d \
	-e /sgilabel/d -e 's/^[	 ]*//' -e 's/[	 ].*//' > /tmp/volhdrlist
	# now that we have logical volumes, need lvtab also
	cat /dev/null > /tmp/mtab
	tabfiles=./tmp/mtab
	cat /etc/mtab | while read dev dir typ rest
	do
		if [ "$typ" = "efs" ]
		then
			/etc/devnm "$dir" >> /tmp/mtab
		fi
	done
	if [ -s /etc/lvtab ]
		then cp /etc/lvtab /tmp
		tabfiles="$tabfiles ./tmp/lvtab"
	fi
	cd /
	if [ $HN = "LOCAL" ]
	then
		/usr/sbin/bru -cvR -L"Full Backup" -f $TP $tabfiles /
	else
		/usr/sbin/bru -cvR -L"Full Backup" -f guest@$HN:$TP $tabfiles /
	fi
	exitstat=$?

	if [ $exitstat -eq 0 ]
	then date +%D > /etc/lastbackup
	else echo $0 failed "(exit status $?)", /etc/lastbackup not updated
	fi
    fi
else
	if [ $HN = "LOCAL" ]
	then
	    /usr/sbin/bru -cvR -L"Partial Backup" -f $TP $BDIR
	else
	    /usr/sbin/bru -cvR -L"Partial Backup" -f guest@$HN:$TP $BDIR
	fi
	exitstat=$?
fi
echo ""
echo "Backup complete."
exit $exitstat
