: "@(#)mtbackup	5.1	6/6/86"
#
# MTBACKUP SCRIPT
#
# This script accepts any number of filenames or directories from
# the command line, which are then used by the cpio -ovB command.
# 
# USAGE
#	mtbackup [-n] [-h] [-v] [-f number] args
#
#	-n	Do not rewind tape when backup is complete.  This is
#		useful if you wish to save multiple archives onto a tape.
#		When restoring, the '-f number' command may be used to
#		position the tape to the proper archive. (see mtrestore)
#		The default is to rewind when complete.
#
#	-h	Set high-density mode (3200 or 6250 depending on drive model).
#		The default is low-density (usually 1600 BPI).
#
#	-v	Verify tape.  Since cpio does not have a built-in verify
#		option, simulate this by rewinding to the beginning of this
#		archive and list the directory back in verbose mode.
#		Cpio will report an error if it can't re-read the archive.
#
#	-f	Position the tape to file 'number' before saving archive. The
#		tape will be rewound first to insure correct positioning.
#		This option implies no-rewind (no need to specify -n).
#		A non-numeric, zero or negative number results in an error.
#		This option requires that the tape already contain number-1
#		files. All files following the one to be written now will
#		become unaccessible.
#
#	args	A string of file or directory names to save.  Wildcards
#		may be used.  If no arguments are entered, the current
#		directory is saved.

# EXAMPLES
#
#	mtbackup		After logging in to system, saves all
#				of user's files onto mag tape at 1600 BPI
#
#	mtbackup -vh		Same as above except 3200BPI.  Tape will
#				rewind and display it's table of contents
#
#	mtbackup -v dir*	User has two subdirectories, dira & dirb,
#				in his current directory.  This will save
#				both onto a single tape file and verify it. 

# String to be echoed if user enters incorrect command

USAGE='usage: mtbackup [-n] [-h] [-v] [-f number] filenames'

# Decode the parameters using getopt(1).  All '-' parameters will be
# first, followed by '--' and the remaining arguments.

set -- `getopt nhvf: $*`

# If 'getopt' failed ( illegal argument ) print USAGE info and exit.

if [ $? != 0 ]
then
	echo $USAGE
	exit 2
fi
 
# Now translate the command line arguments, until a '--' is found.
# Each time through, the remaining unprocessed arguments are shifted left
# one position. Set default values first.

DEVICE=/dev/rmt0
REWIND=yes
FREWIND=no

for i in $*
do
	case $i in
	-n)	REWIND=no; shift;;
	-h)	DEVICE=/dev/rmt16; shift;;
	-v)	VERIFY=yes; FREWIND=yes; shift;;
	-f)	REWIND=no; FREWIND=yes; FILE=$2; shift 2;;
	--)	shift; break;;
	esac
done
	
# If no files or directories specified, default to current directory.	
 
if [ $# = 0 ]
	then
		set .
	fi
 
# If the name of a file or directory is entered incorrectly or it cannot 
# be read, then an error message is generated and the script is exited.
 
for t in $*
do
	if test ! -r $t 
	then
		echo "MTBACKUP - can't read "$t
		exit 2
	fi
done

# Position the tape to the selected archive, if requested.

if [ "$FILE" ]
then
	SKIP=`expr $FILE - 1`

#       Error code 2 returned if FILE was not numeric.

	if [ $? = 2 ]
	then
		echo "MTBACKUP: -f filenumber : filenumber must be numeric"
		exit 0
	fi

#       Make sure that skip count is not negative.

	if [ `expr $SKIP \< 0` != 0 ]
	then
		echo "MTBACKUP: -f filenumber : filenumber must be > 0"
		exit 2
	fi

#	Rewind it before writing to the first file (if not disabled).

	if [ $FREWIND = yes ]
	then
		echo "MTBACKUP: Rewinding tape.."
		mt -f $DEVICE rew
	else
		skipnum=-1
	fi

#       If first file not requested (skip count !=0) adjust tape position.

	if [ $SKIP != 0 ]
	then
		echo "MTBACKUP: Skipping to file "$FILE".."
		mt -f $DEVICE fsf $SKIP
		if [ $? != 0 ]
		then
			echo "MTBACKUP: Unable to position tape to requested file"
			exit 2
		fi
	fi
fi
 
# If all goes smoothly, the files are saved.
 
find $* -print | cpio -ovB > $DEVICE

# If verify selected, rewind the tape and list the contents in verbose mode.

if [ $VERIFY ]
then
	echo "MTBACKUP: Verifying tape"
 	mt -f $DEVICE bsf 1
	if [ $? = 0 ]
	then
		cpio -itvB < $DEVICE
	else
		echo "MTBACKUP: unable to backspace file"
	fi
fi

# Rewind tape unless -n (NO REWIND) was specified.

if [ $REWIND = yes ]
then
	mt -f $DEVICE rew
fi
