#! /bin/sh
#
#	readFUNmastertape
#
#	Copyright (c) 1988 by Frame Technology Corp.
#	Eddie Kessler
#	Version 1.3
#
#
# This shell script reads the standard Frame Users Network tape into
# 3 tar image files.  writemastertape will then create any number of exact
# copies of the tape to 1/2 in. or 1/4 in. tape.
#
#-------------------------------------------------------------------------------
#-----  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
#-------------------------------------------------------------------------------

# DRIVE=quarter for 1/4" tape, =half for 1/2" tape #### modify this
DRIVE=quarter

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

scriptname=`basename $0`
DISKUSE=2200
BS=400
THISHOST=`hostname`
umask 022

if [ "$PWD" = "" ]; then
	CURRENTDIR=`pwd`
else
	CURRENTDIR=$PWD
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 "
This script is to be used to read a Frame Users Network tape for the purpose
of making subsequent copies which are exact duplicates.  The files produced
by this script can be used to generate either 1/4 in. cartridge or 1/2 in.
reel tapes.

When this script asks you questions, a default answer 
is shown in brackets next to the question.  To use the 
default answer, press Return in response to the question. 
Otherwise, type one of the other suggested responses. 
([n] means 'no'; to answer 'yes' type 'y'.)
"
if ask "Would you like to continue?" "y" "n"; then
	echo
else
	exit 1
fi

AVAIL=`df . | tr '\12' ' ' | awk -e '{print $11}'`
echo
df .
echo "
The FUN tape files will take up $DISKUSE Kbytes.
You have $AVAIL Kbytes available on this file system."

if [ $AVAIL -lt $DISKUSE ]; then
	echo "
$scriptname: There is not enough room on this file system to read 
the Frame software.  Please make more space and run $scriptname again."
	exit 1
fi

if ask "
Would you like to continue?" "y" "n"; then
	continue
else
	finalmsg
	exit 1
fi

#    TAPE READER SECTION  
#

#  Find tape devices on local machine
#
if [ "$DRIVE" = "quarter" ]; then
	BS1=20
	BS2=400
	DEVCNT=0
	FILE1="tar1.st"
	FILE3="tar1.mt"
	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
	BS1=20
	BS2=50
	FILE1="tar1.mt"
	FILE3="tar1.st"
	if [ -r /dev/nrmt0 ]; then DEVCNT=1; else DEVCNT=0; fi
fi

#  Check for remote tapes
#
TAPEHOST="UNKNOWN"
while [ $TAPEHOST = "UNKNOWN" ] ; do
echo 
if [ $DEVCNT -eq 0 ]; then
	echo "Cannot detect tape drive connected to your local machine."
		echo -n "Which remote machine's tape drive will you be using? "
		read TAPEHOST;
		rsh $TAPEHOST ls / > /dev/null 2>&1
		if [ $? -eq 0 ]; then
                       	REMOTE="rsh $TAPEHOST -n"
		else
			echo "$scriptname: Could not access (rsh) remote host $TAPEHOST"
			if ask "Would you like to continue?" "y" "n"; then
				continue
			else
				exit 1
			fi
			TAPEHOST="UNKNOWN"
		fi
	else
		echo -n "Which machine's tape drive will you be using? [$THISHOST]: "
		read TAPEHOST;
		if [ "$TAPEHOST" = "" ]; then
			TAPEHOST=$THISHOST
		fi
		if [ "$TAPEHOST" = "$THISHOST" ]; then
			REMOTE=""
		else
			rsh $TAPEHOST ls / > /dev/null 2>&1
			if [ $? -eq 0 ]; then
	                       	REMOTE="rsh $TAPEHOST -n"
			else
				echo "$scriptname: Could not access (rsh) remote host $TAPEHOST"
				if ask "Would you like to continue?" "y" "n"; then
					continue
				else
					exit 1
				fi
				TAPEHOST="UNKNOWN"
			fi
		fi
	fi
done

#
#  Check for tape type
#
if [ $DEVCNT -gt 1 -o "$TAPEHOST" != "$THISHOST" ]; then
	if [ "$DRIVE" = "quarter" ]; then
		while true; do
			echo -n "
Now choose the type of tape drive where the
FUN tape is inserted:
(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

rm -f tar1.st tar1.mt tar.generic

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

echo "Using tape device: $DEVICE"
echo -n "
The following commands will be performed by $scriptname to 
prepare for creating duplicates of the FUN tape:

    $REMOTE mt -f $DEVICE rewind
    $REMOTE dd if=$DEVICE bs=${BS1}b > $FILE1
    $REMOTE dd if=$DEVICE bs=${BS2}b > tar.generic
    $REMOTE dd if=$DEVICE bs=${BS1}b > $FILE3
"

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

#
#	  Check for the tape
#

while true; do
	if [ "$REMOTE" = "" ]; then
		mt -f $DEVICE rewind
		status=$?
	else
		status=`$REMOTE "sh -c 'mt -f $DEVICE rewind 2>/dev/null; echo \\$?'"`
	fi
	if [ "$status" = "0" ]; then
		break
	fi
	if [ "$status" = "" ]; then
		echo "$scriptname: Could not access (rsh) remote host $TAPEHOST"
		exit 1
	else
		echo "Tape drive $DEVICE on machine $TAPEHOST not ready."
		echo
		echo -n "Insert the FUN tape, then press Return to continue: "
		read x
	fi
done   
#
#         Read in the 3 tar images
#
#
echo "Reading the tape.  This will take several minutes..."
if [ "$REMOTE" = "" ]; then
        dd if=$DEVICE bs=${BS1}b of=$FILE1 2>/dev/null
	status=$?
else
	status=`$REMOTE "sh -c '(2>/dev/null dd if=$DEVICE bs=${BS1}b 1>&2; echo \\$?)'" 2>$FILE1`
fi
if [ "$status" != "0" ]; then
	echo "$scriptname: Could not read tape."
	rm -f tar1.st
	exit 1
fi
if [ "$REMOTE" = "" ]; then
        dd if=$DEVICE bs=${BS2}b of=tar.generic 2>/dev/null
	status=$?
else
	status=`$REMOTE "sh -c '(2>/dev/null dd if=$DEVICE bs=${BS2}b 1>&2; echo \\$?)'" 2>tar.generic`
fi
if [ "$status" != "0" ]; then
	echo "$scriptname: Could not read tape."
	rm -f tar.generic
	exit 1
fi
if [ "$REMOTE" = "" ]; then
        dd if=$DEVICE bs=${BS1}b of=$FILE3 2>/dev/null
	status=$?
else
	status=`$REMOTE "sh -c '(2>/dev/null dd if=$DEVICE bs=${BS1}b 1>&2; echo \\$?)'" 2>$FILE3`
fi
if [ "$status" != "0" ]; then
	echo "$scriptname: Could not read tape."
	rm -f tar1.mt
	exit 1
fi

echo "
$scriptname is now finished. You must now run writeFUNmastertape
to create duplicates of the FUN installation tapes."

finalmsg
