#! /bin/sh
#
#	readFUNtape
#
#	Copyright (c) 1988 by Frame Technology Corp.
#	Bill Rose
#	Version 1.3
#
#
# This shell script creates an exclude file that is used with
# the tar command to reduce the amount of disk space that
# Frame takes up. It prompts the user to exclude unnecessary
# files. If the user wants, it will automatically tar the 
# Frame files from the 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

# anyone can remove temporary files
umask 022

# DISKUSE is basemaker stuff
DISKUSE=100
SUN3USE=1000
SUN4USE=1050
SUN386USE=150

scriptname=`basename $0`

EXCLUDEFILE="/tmp/excluded"
SUN3EXC="./.readFUNtape/sun3.exclude"
SUN4EXC="./.readFUNtape/sun4.exclude"
SUN386EXC="./.readFUNtape/sun386.exclude"
TAROPTIONS="-xBpfbX"
BS=400
THISHOST=`hostname`
THISMACH=`/bin/arch`

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 "
If you wish to be kept informed by e-mail of the latest Frame
developments, please send an e-mail message to sun!frame!comments and
ask to be added to the user mailing list.  If there are several Frame
users at your site, we request that you ask your system administrator
to set up a mailing alias, for example: \"sun!mycompany!frame-users\"
and send us the address."
}

rm -f $EXCLUDEFILE
touch $EXCLUDEFILE

echo "
Frame provides the option in its products to run on several
Sun workstation models.

If you don't need to run on several models, you can save disk space by
excluding some files from the installation.

This script asks you which options you want to install. If you
choose not to install some options, this script will install only
the files you need, saving you disk space.

You can also use this script to read the remaining files from the
tape (or you can type the necessary commands yourself).

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

echo "
Do you wish to install:
"
# evaluate response to previous question (xor)
ans(){
	status=$?
	[ $status -eq 0 -a "$1" = "y" -o $status -ne 0 -a "$1" = "n" ]
}

if [ -d ./bin/bin.sun3 ]; then
	DEF="y"; ALT="n"
else
	DEF="n"; ALT="y"
fi
ask "Sun2/Sun3 files ($SUN3USE Kbytes)?" "$DEF" "$ALT"
if ans $DEF; then
	DISKUSE=`expr $DISKUSE + $SUN3USE`
else
	if [ -r $SUN3EXC ]; then
		cat $SUN3EXC >> $EXCLUDEFILE
	else
		echo "Cannot find file: $SUN3EXC"
		exit 1
	fi
fi

if [ -d ./bin/bin.sun4 ]; then
	DEF="y"; ALT="n"
else
	DEF="n"; ALT="y"
fi
ask "Sun4 files ($SUN4USE Kbytes)?" "$DEF" "$ALT"
if ans $DEF; then
	DISKUSE=`expr $DISKUSE + $SUN4USE`
else
	if [ -r $SUN4EXC ]; then
		cat $SUN4EXC >> $EXCLUDEFILE
	else
		echo "Cannot find file: $SUN4EXC"
		exit 1
	fi
fi

if [ -d ./bin/bin.sun386 ]; then
	DEF="y"; ALT="n"
else
	DEF="n"; ALT="y"
fi
ask "Sun386 files ($SUN386USE Kbytes)?" "$DEF" "$ALT"
if ans $DEF; then
	DISKUSE=`expr $DISKUSE + $SUN386USE`
else
	if [ -r $SUN386EXC ]; then
		cat $SUN386EXC >> $EXCLUDEFILE
	else
		echo "Cannot find file: $SUN386EXC"
		exit 1
	fi
fi

AVAIL=`df . | tr '\12' ' ' | awk -e '{print $11}'`
echo
df .
echo "
In the configuration you specified, FUN 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 install 
the FUN software.  Please make more space and reinstall."
	exit 1
fi
if [ $DISKUSE -eq 0 ]; then
	echo "
You have not selected any optional features to read from the tape.
Please run $scriptname again if you intended to.
"
	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
      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

#  Check for remote installs
#
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"
					BS=400
	        	                break ;;
	                	"ar")
					TAPE="ar"
					BS=400
		                        break ;;
				*)
	        	                echo "$scriptname: invalid tape type $TAPE" ;;
		        esac
		done
	else
		TAPE="mt"
		BS=50
	fi
else
	if [ "$DRIVE" = "quarter" ]; then
		if [ $ST -eq 1 ]; then TAPE="st"; fi
		if [ $AR -eq 1 ]; then TAPE="ar"; fi
		BS=400
	else
		TAPE="mt"
		BS=50
	fi
fi

#
# 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 the 
script to finish the installation:
"
	if [ "$REMOTE" = "" ] ; then
		echo "
    mt -f $DEVICE rewind
    mt -f $DEVICE fsf 1
    tar $TAROPTIONS $DEVICE $BS $EXCLUDEFILE
    ./bin/FUNinstall"
	else
		echo "
    $REMOTE mt -f $DEVICE rewind
    $REMOTE mt -f $DEVICE fsf 1
    rcp $EXCLUDEFILE $TAPEHOST:$EXCLUDEFILE
    $REMOTE dd if=$DEVICE bs=${BS}b | tar $TAROPTIONS - $BS $EXCLUDEFILE
    ./bin/FUNinstall"
	fi

if ask "
Would you like this script to perform the above 
commands in $CURRENTDIR?" "y" "n"; then
	continue
else
	echo "
The file: $EXCLUDEFILE  has been created by this script.

FUN software has NOT been installed. If you wish 
to install it, type the above commands."
	finalmsg
	exit 1
fi

#
#	  Check for the tape
#

while true; do
	if [ "$REMOTE" = "" ]; then
		mt -f $DEVICE rewind 2>/dev/null
		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   
#
#	Skip over first tar file on tape
#
if [ "$REMOTE" = "" ]; then
	mt -f $DEVICE fsf 1
	status=$?
else
	status=`$REMOTE "sh -c 'mt -f $DEVICE fsf 1 2>/dev/null; echo \\$?'"`
fi
if [ "$status" != "0" ]; then
	echo "$scriptname: Could not read tape."
	exit 1
fi

#
#         Read in Frame files excluding extractions
#
#
if [ "$REMOTE" != "" ]; then
	rcp $EXCLUDEFILE $TAPEHOST:$EXCLUDEFILE
	if [ $? -ne 0 ] ; then
		echo "$scriptname: Could not access (rcp) remote host $TAPEHOST"
		exit 1
	fi
fi
sleep 5

echo "Reading the tape.  This will take several minutes..."
if [ "$REMOTE" = "" ]; then
	tar $TAROPTIONS $DEVICE $BS $EXCLUDEFILE
	status=$?
else
	status=`$REMOTE dd if=$DEVICE bs=${BS}b | tar $TAROPTIONS - $BS $EXCLUDEFILE; echo $?`
fi
if [ "$status" != "0" ]; then
	echo "$scriptname: Could not read tape."
	exit 1
fi
./bin/FUNinstall

echo "
$scriptname is now finished.
"
finalmsg
