: "@(#)mtrestore	5.1	6/6/86"
#
# MTRESTORE SCRIPT
#
# This script is used to restore cpio archive tapes previously created 
# with the 'mtbackup' script.  The tape density and rewind options are
# compatible with those of mtbackup.  When restoring, files from the
# saved archive will be restored into the user's currect directory unless
# the archive was originally created with full path names.  This may
# be determined by listing the contents of an archive with the 'mtlist'
# script.  If the saved files do not start with / they will be restored
# into the user's current directory.
#
# USAGE
#	mtrestore [-n] [-h] [-f number] patterns
#
#	-n	Do not rewind tape when restore is complete.  This is
#		useful if you wish to restore multiple archives from a tape.
#		The archives must be sequential on the tape.  See the -f
#		option below to restore archives out of sequence.
#		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).
#
#	-f	Position the tape to file 'number' before restoring.  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.
#
#      patterns If any filename/directory patterns are specified, only those
#		files/directories matching will be restored from the tape.
#		The DEFAULT is to RESTORE ALL files previously saved.  This
#		option requires care because any wildcard characters specified
#		must be quoted to avoid expansion by the shell.  See sh(1)
#		and cpio(1) for more information on patterns.

# String to be echoed if user enters incorrect command

USAGE='usage: mtrestore [-n] [-h] [-f number] patterns'

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

set -- `getopt nhf: $*`

# 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.

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

# 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
	-n)	REWIND=no; shift;;
	-h)	DEVICE=/dev/rmt16; shift;;
	-f)	REWIND=no; FREWIND=yes; FILE=$2; shift 2;;
	--)	shift; break;;
	esac
done

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

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

#       Error code 2 returned if $FILE is not numeric

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

#       Make sure that skip count is not negative.

	if [ `expr $SKIP \< 0` != 0 ]
	then
		echo "MTRESTORE: -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 "MTRESTORE: 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 "MTRESTORE: Skipping to file "$FILE".."
		mt -f $DEVICE fsf $SKIP
		if [ $? != 0 ]
		then
			echo "MTRESTORE: Unable to position tape to requested file"
			exit 2
		fi
	fi
fi

# Finally, restore the archive.

cpio -ivdmB $* < $DEVICE

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

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