#! /bin/sh
#
#	writeFUNmastertape
#
#	Copyright (c) 1988 by Frame Technology Corp.
#	Eddie Kessler
#	Version 1.3
#
#
# In order to use this script, you need to have previously run
# readFUNmastertape.  writeFUNmastertape can then be used to create
# Frame Users Network tapes for 1/4 in. cartridge or 1/2 in. mag tapes.
#
#-------------------------------------------------------------------------------
#-----  Frame Technology	If you encounter any difficulties with this
#-----  2911 Zanker Road	script, contact Frame Tech Support.
#FRAME  San Jose, CA 95134	
#=====  (408) 433-3311		uucp: sun!frame!comments
#-------------------------------------------------------------------------------

# make sure that we have good stuff on our path
PATH=/bin:/usr/bin:/usr/ucb

scriptname=`basename $0`
THISHOST=`hostname`

if [ ! \( -r tar1.st -a -r tar.generic -a -r tar1.mt \) ]; then
	echo "To create a master tape, you must first run readFUNmastertape."
	exit 1
fi
# Routine to ask a question with default passed and check for typos
# $1=question, $2=default, $3=opposite of $2
ask(){
        answer=undefined
        echo -n "$1 [$2]: "
        read answer
        until [ "$answer" = "" -o "$answer" = "$2" -o "$answer" = "$3" ];
        do
		echo "Please type '$2' or '$3'."
	        echo -n "$1 [$2]: "
	        read answer
        done
        [ "$answer" = "$2" -o "$answer" = "" ]
}

# Routine to print out final message before exiting
finalmsg(){
	echo "
"
}

echo "
$scriptname, when run after readFUNmastertape, can produce copies of
Frame Users Network installation tapes on a local tape drive.
"
if [ -r /dev/nrst0 -o -r /dev/nrar0 ]; then
	if [ -r /dev/nrmt0 ]; then
		DRIVE="both"
	else
		DRIVE="quarter"
	fi
else	if [ -r /dev/nrmt0 ]; then
		DRIVE="half"
	else
		echo "
Cannot detect tape drive connected to your local machine.
$scriptname must be run from a machine with a local tape drive."
		exit 1
	fi
fi

if [ "$DRIVE" = "both" ]; then
	if ask "Do you want to create a 1/4\" cartridge or 1/2\" reel tape?" "cartridge" "reel"; then
		DRIVE="quarter"
	else
		DRIVE="half"
	fi
fi

#    TAPE WRITER SECTION  
#

#  Find tape devices on local machine
#
if [ "$DRIVE" = "quarter" ]; then
      DEVCNT=0
      if [ -r /dev/nrst0 ]; then ST=1; DEVCNT=`expr $DEVCNT + 1`; else ST=0; fi
      if [ -r /dev/nrar0 ]; then AR=1; DEVCNT=`expr $DEVCNT + 1`; else AR=0; fi
else
      if [ -r /dev/nrmt0 ]; then DEVCNT=1; else DEVCNT=0; fi
fi

if [ $DEVCNT -eq 0 ]; then
	echo "
Cannot detect $DRIVE inch tape drive connected to your local machine.
$scriptname must be run from a machine with a local tape drive."
	exit 1
fi
#
#  Check for tape type
#
if [ $DEVCNT -gt 1 ]; then
	if [ "$DRIVE" = "quarter" ]; then
		while true; do
			echo -n "
Now choose the type of tape drive where the
FUN tape will be created:
(st) SCSI 1/4 inch cartridge tape drive (typical)
(ar) Archive 1/4 inch cartridge tape drive (old-style)

What tape type? ( st | ar ) [st]: "
			read TAPE;
			case "$TAPE" in
		                "st" | "")
					TAPE="st"
	        	                break ;;
	                	"ar")
					TAPE="ar"
		                        break ;;
				*)
	        	                echo "$scriptname: invalid tape type $TAPE" ;;
		        esac
		done
	else
		TAPE="mt"
	fi
else
	if [ "$DRIVE" = "quarter" ]; then
		if [ $ST -eq 1 ]; then TAPE="st"; fi
		if [ $AR -eq 1 ]; then TAPE="ar"; fi
	else
		TAPE="mt"
	fi
fi

if [ "$DRIVE" = "quarter" ]; then
      BS1=20
      BS2=400
      FILE1="tar1.st"
      FILE3="tar1.mt"
else
      BS1=20
      BS2=50
      FILE1="tar1.mt"
      FILE3="tar1.st"
fi

#
# Using QIC-11 to tar the tape
#
DEVICE="/dev/nr${TAPE}0"

echo "Using $DRIVE inch tape drive on machine $THISHOST: $DEVICE"
echo -n "
The following commands will be performed by the 
script to create a duplicate FUN tape:
"
echo "
    mt -f $DEVICE rewind
    dd if=$FILE1 of=$DEVICE bs=${BS1}b
    dd if=tar.generic of=$DEVICE bs=${BS2}b
    dd if=$FILE3 of=$DEVICE bs=${BS1}b"

if ask "
Would you like this script to perform the above commands?" "y" "n"; then
	continue
else
	echo "
"
	finalmsg
	exit 1
fi

#
#	  Check for the tape
#

while true; do
	mt -f $DEVICE rewind 2>/dev/null
	if [ "$?" = "0" ]; then
		break
	else
		echo "Tape drive $DEVICE not ready."
		echo
		echo -n "Insert a blank tape, then press Return to continue: "
		read x
	fi
done   
#
#         Write the FUN tape
#
echo "Writing the tape.  This will take several minutes..."
dd if=$FILE1 of=$DEVICE bs=${BS1}b 2> /dev/null
if [ "$?" != "0" ]; then
	echo "$scriptname: Could not write tape."
	exit 1
fi
dd if=tar.generic of=$DEVICE bs=${BS2}b 2> /dev/null
if [ "$?" != "0" ]; then
	echo "$scriptname: Could not write tape."
	exit 1
fi
dd if=$FILE3 of=$DEVICE bs=${BS1}b 2> /dev/null
if [ "$?" != "0" ]; then
	echo "$scriptname: Could not write tape."
	exit 1
fi

TARtOPS=tBbf
# echo Now check that we can read the contents of the tape
mt -f $DEVICE rewind
tar $TARtOPS $BS1 $DEVICE
if [ $? -ne 0 ]; then exit 1; fi
mt -f $DEVICE fsf 1
tar $TARtOPS $BS2 $DEVICE
if [ $? -ne 0 ]; then exit 1; fi
mt -f $DEVICE fsf 1
tar $TARtOPS $BS1 $DEVICE
if [ $? -ne 0 ]; then exit 1; fi
mt -f $DEVICE rewind
echo "
$scriptname is now finished."
if [ "$DRIVE" = "quarter" ]; then
	echo "Please be sure to write-protect the tape by turning the SAFE switch."
else
	echo "Please be sure to write-protect the tape by removing the write ring."
fi

finalmsg