#! /bin/sh
#
#	readtape
#
#	Copyright (c) 1988, 1989 by Frame Technology Corp.
#	Bill Rose, Eddie Kessler
#	Version 2.0
#
#
# 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
# 1010 Rincon Circle		script, contact Frame Tech Support.
# San Jose, CA 95131		internet: comments@frame.com
# (408)433-3311, (800)U4-FRAME	uucp: {riacs,ames,vsi1}!frame!comments
#-------------------------------------------------------------------------------

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

# 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=5500
SUN3USE=6000
SUN4USE=6800
SUN386USE=2300
INTLUSE=4800
LWPUSE=3500
SAMPLEUSE=2300

scriptname=`basename $0`

OPTIONFILE="/tmp/options"
EXCLUDEFILE="/tmp/excluded"
BASE="baseframe"
BASEEXC="./.readtape/$BASE.exclude"
SUN3="sun3"
SUN3EXC="./.readtape/$SUN3.exclude"
SUN4="sun4"
SUN4EXC="./.readtape/$SUN4.exclude"
SUN386="sun386"
SUN386EXC="./.readtape/$SUN386.exclude"
INTRNTNL="international"
INTLEXC="./.readtape/$INTRNTNL.exclude"
LWPLUS="lwplus"
LWPEXC="./.readtape/$LWPLUS.exclude"
SAMPLE="samples"
SAMPLEEXC="./.readtape/$SAMPLE.exclude"

VERSION="Version 2.0"
TAROPTIONS="-xBpfbX"
BS=400
THISHOST=`hostname`
THISMACH=`arch`
LWP=0
DOBASE=1

if [ "$PWD" = "" ]; then
	CURRENTDIR=`pwd`
else
	CURRENTDIR=$PWD
fi

# Routine to ask a question with default passed and check for typos
# $?=1 for default response
# $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(){
rm -f $EXCLUDEFILE $OPTIONFILE
	echo "
If you wish to be kept informed by e-mail of the latest Frame
developments, please send an e-mail message to comments@frame.com
or {riacs,ames,vsi1}!frame!comments and ask to be added to our 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: \"frame-users@mycompany.com\" and send us the
address."
}

rm -f $EXCLUDEFILE $OPTIONFILE
touch $EXCLUDEFILE $OPTIONFILE

echo "
Installing FrameMaker $VERSION from Frame Technology Corporation.
Frame provides many options in its products, including the options to:
	- run on several Sun workstation models
	- use Frame's multilingual features (in the international version)
	- use full 35 LaserWriter Plus compatible fonts
	- optional sample documents

If you don't need all the options, 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

if [ -d ./.fminit2.0.ps ]; then
	VERFILES=`ls ./.VERSION* 2>/dev/null | wc -l`
	if [ $VERFILES -eq 1 ]; then
		INSTVERSION=`cat ./.VERSION*`
		if [ "$VERSION" = "$INSTVERSION" ]; then
			echo "$VERSION is already installed."
			if ask "Would you like to add options to $INSTVERSION?" "y" "n"; then
				if [ -r $BASEEXC ]; then
					cat $BASEEXC >> $EXCLUDEFILE
					DISKUSE=0
					DOBASE=0
				else
					echo "Cannot find file: $BASEEXC"
					exit 1
				fi
			else
				finalmsg
				exit 1
			fi
		else
			echo " 
$scriptname: You are trying to overwrite $INSTVERSION with
$VERSION. This will not work. Please remove $INSTVERSION, 
saving any configuration files you may have, and try again."
			exit 1
		fi
	else
		if [ $VERFILES -eq 0 ]; then
			echo "
$scriptname: You have an older version of Frame software in this
directory.  Please remove it and run $scriptname again. Be sure
to save any configuration files you may have.
"
			exit 1
		else
			echo "

$scriptname: There are multiple versions of Frame software in this
directory.  Please remove these and run $scriptname again.  Be sure 
to save any configuration files you may have.
"
			exit 1
		fi

	fi
else
	if [ -d ./.makerinit ]; then
		echo "
$scriptname: You have an older version of Frame software in this
directory.  Please remove it and run $scriptname again. Be sure
to save any configuration files you may have.
"
		exit 1
	fi
fi

echo "$BASE" >> $OPTIONFILE

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 [ "$THISMACH" = "sun3" ]; then
	DEF="y"; ALT="n"
else
	DEF="n"; ALT="y"
fi
ask "Sun3 files ($SUN3USE Kbytes)?" "$DEF" "$ALT"
if ans $DEF; then
	DISKUSE=`expr $DISKUSE + $SUN3USE`
	echo "$SUN3" >> $OPTIONFILE
else
	if [ -r $SUN3EXC ]; then
		cat $SUN3EXC >> $EXCLUDEFILE
	else
		echo "Cannot find file: $SUN3EXC"
		exit 1
	fi
fi

if [ "$THISMACH" = "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`
	echo "$SUN4" >> $OPTIONFILE
else
	if [ -r $SUN4EXC ]; then
		cat $SUN4EXC >> $EXCLUDEFILE
	else
		echo "Cannot find file: $SUN4EXC"
		exit 1
	fi
fi

if [ "$THISMACH" = "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`
        echo "$SUN386" >> $OPTIONFILE
else
	if [ -r $SUN386EXC ]; then
		cat $SUN386EXC >> $EXCLUDEFILE
	else
		echo "Cannot find file: $SUN386EXC"
		exit 1
	fi
fi

echo "
If you plan to run the International version of FrameMaker (imaker)
you should install the multilingual features.
"

if ask "Files for multilingual features ($INTLUSE Kbytes)?" "n" "y"; then
	if [ -r $INTLEXC ]; then
		cat $INTLEXC >> $EXCLUDEFILE
	else
		echo "Cannot find file: $INTLEXC"
		exit 1
	fi
else
	DISKUSE=`expr $DISKUSE + $INTLUSE`
        echo "$INTRNTNL" >> $OPTIONFILE
	INTL=1
fi

echo "
The minimum FrameMaker configuration includes the Times, Helvetica,
Courier, and Symbol fonts.  The Avant Garde, Bookman, New Century Schoolbook,
Palatino, Zapf Chancery, and Zapf Dingbats fonts are optional.
"

if ask "All 35 LaserWriter Plus compatible fonts ($LWPUSE Kbytes)?" "y" "n"; then
	DISKUSE=`expr $DISKUSE + $LWPUSE`
        echo "$LWPLUS" >> $OPTIONFILE
	LWP=1
else
	if [ -r $LWPEXC ]; then
		cat $LWPEXC >> $EXCLUDEFILE
	else
		echo "Cannot find file: $LWPEXC"
		exit 1
	fi
fi

echo "
This tape includes optional documents containing clipart, math samples,
and sample demonstration documents.
"

if ask "Optional sample documents ($SAMPLEUSE Kbytes)?" "y" "n"; then
	DISKUSE=`expr $DISKUSE + $SAMPLEUSE`
        echo "$SAMPLE" >> $OPTIONFILE
else
	if [ -r $SAMPLEEXC ]; then
		cat $SAMPLEEXC >> $EXCLUDEFILE
	else
		echo "Cannot find file: $SAMPLEEXC"
		exit 1
	fi
fi

AVAIL=`df . | tr '\12' ' ' | awk -e '{print $11}'`
echo
df .
echo "
In the configuration you specified, Frame 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 Frame 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/nrst8 ]; then ST=1; DEVCNT=`expr $DEVCNT + 1`; else ST=0; fi
      if [ -r /dev/nrar8 ]; then AR=1; DEVCNT=`expr $DEVCNT + 1`; else AR=0; fi
else
      if [ -r /dev/nrmt8 ]; 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 Frame 
distribution 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-24 to tar the tape
#
DEVICE="/dev/nr${TAPE}8"

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
    dd if=$DEVICE bs=${BS}b | uncompress | tar $TAROPTIONS - $BS $EXCLUDEFILE"
	else
		echo "
    $REMOTE mt -f $DEVICE rewind
    $REMOTE mt -f $DEVICE fsf 1
    $REMOTE dd if=$DEVICE bs=${BS}b | uncompress | tar $TAROPTIONS - $BS $EXCLUDEFILE"
	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.

Frame 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 Frame distribution 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
#
#

echo "Reading the tape.  This will take about 30 minutes..."
if [ "$REMOTE" = "" ]; then
	dd if=$DEVICE bs=${BS}b 2> /dev/null | uncompress | tar $TAROPTIONS - $BS $EXCLUDEFILE
	status=$?
else
	status=`$REMOTE dd if=$DEVICE bs=${BS}b 2> /dev/null | uncompress | tar $TAROPTIONS - $BS $EXCLUDEFILE; echo $?`
fi

if [ "$status" != "0" ]; then
	echo "$scriptname: Could not read tape."
	exit 1
fi

if [ "$REMOTE" = "" ]; then
	mt -f $DEVICE rewind 2>/dev/null &
else
	$REMOTE mt -f $DEVICE rewind 2>/dev/null &
fi

if [ "$LWP" = "1" ]; then
	( cd .fminit2.0/fontdir/; rm -f fontlist;
	  ln -s fontlist.lwpstd fontlist )
elif [ "$DOBASE" = "1" ]; then
	( cd .fminit2.0/fontdir/; rm -f fontlist;
	  ln -s fontlist.lwstd fontlist )
fi

touch .readtape/options
sort -u -o .readtape/options $OPTIONFILE .readtape/options

echo "
$scriptname is now finished. You must now run fmlicense on the 
license server host (unless you are installing a demonstration 
version). The license server host is the machine that runs the 
license server process and is typically the machine on which you 
have installed the Frame software."

echo "
If you are installing a demonstration version of Frame software, you
have completed the installation process. Under suntools, to run
the demonstration version of FrameMaker from this directory, type:

	bin/demomaker

Please refer to \"Where to go from here\" in the Installation and 
Printing Instructions for other demonstration version startup commands."

if [ "$INTL" = "1" ]; then
	echo "
Please alter your .fminit2.0/languages file to set the appropriate
default document language. When you run International FrameMaker for
UK English, the document language in the languages file should be
UKEnglish.  Please refer to Appendix D of your Reference Manual or the
\"International FrameMaker 2.0 Release Notes\" for more information."
fi
finalmsg
