: "@(#)ctbackup	5.1	6/6/86"
#
# CTBACKUP SCRIPT
#
# This script accepts any number of filenames or directories from
# the command line, which are then used by the cpio -ovB command.
# The tape will always be rewound on completion.
# 
# USAGE
#	ctbackup [-v] [-a] [-n] args
#
#	-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.
#
#	-a	Append this archive.  Tape will automatically position
#		to the end of recorded media before writing.
#		Default is to write archive at beginning of tape.
#
#	-n	Do not rewind tape before or after saving.  Use this option
#		if you wish to save multiple files onto the tape without
#		re-positioning overhead.  NOTE - because of hardware
#		constraints, this option will not allow the verify option.
#		It is highly recommended that the user verify the tape
#		after saving with the ctlist command.
#
#	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
#
#	ctbackup		After logging in to system, saves all
#				of user's files onto cartridge tape.
#
#	ctbackup -v 		Tape will rewind and display it's table
#				of contents
#
#	ctbackup -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: ctbackup [-v] [-a] [-n] filenames'

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

set -- `getopt van $*`

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

if [ $? != 0 ]
then
	echo $USAGE
	exit 2
fi
 
# These are the defaults, can be altered with command-line options.

VERIFY=no
DEVICE=/dev/nct0
REWIND=yes
FREWIND=no

# Now translate the command line arguments, until a '--' is found.
# Each time through, the remaining unprocessed arguments are shifted left
# one position.

for i in $*
do
	case $i in
	-v)	VERIFY=yes; FREWIND=yes; shift;;
	-a)	APPEND=yes; FREWIND=yes; shift;;
	-n)	REWIND=no; shift;;
	--)	shift; break;;
	esac
done

# If the no rewind option was selected, disable the verify command.

if [ $REWIND = no ]
then
	if [ $VERIFY = yes ]
	then
	  VERIFY=no
	  echo "CTBACKUP: verify disabled, can't be used with -n option"
	fi
fi
	
# 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 "CTBACKUP - can't read "$t
		exit 2
	fi
done

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

skipnum=0
if [ "$APPEND" ]
then

#       OK, rewind tape to beginning before attempting to position.

	echo "CTBACKUP: Rewinding tape.."
	mt -f $DEVICE rew

	echo "CTBACKUP: Positioning tape.."
	while true
	do
		mt -f $DEVICE fsf >/dev/null 2>&1
		if [ $? != 0 ]
		then
			break
		else
			skipnum=`expr $skipnum + 1`
		fi
	done
else

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

	if [ $FREWIND = yes ]
	then
		echo "CTBACKUP: Rewinding tape.."
		mt -f $DEVICE rew
	else
		skipnum=-1
	fi
fi
 
# If all goes smoothly, the files are saved.
 
filenum=`expr $skipnum + 1`
if [ $filenum = 0 ]
then
	filenum="current position"
fi
echo "CTBACKUP: writing to file number = "$filenum

find $* -print -cpio $DEVICE

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

if [ $VERIFY = yes ]
then
	echo "CTBACKUP: Rewinding tape"
  	mt -f $DEVICE rew
	if [ $APPEND ]
	then
		echo "CTBACKUP: Positioning tape to file "$filenum" for verify"
		mt -f $DEVICE fsf $skipnum
		if [ $? != 0 ]
		then
			echo "CTBACKUP: unable to reposition tape"
			exit 2
		fi
	fi
	cpio -itvB < $DEVICE
fi

# Rewind tape when done (if not disabled).

if [ $REWIND = yes ]
then
	echo "CTBACKUP: rewinding tape.."
	mt -f $DEVICE rew
fi
