#!/bin/sh -f

#	installit2
#
#	Derived from:	Template supplied by SGI for hot mix installation
#
#	Author:		Anthony Shaughnessy
#
#	Created on:	15th June 1993
#
#	Sccs ID:	@(#)installit2.sh	1.9 8/16/93
#
#	Coding Stds.:	2.0
#
#	Purpose:	To act as a wrapper for the installation process from
#			the hot mix CD. This script is called from InstallIt
#			which opens a window as root to run this script in.
#			This script asks various questions, calls instwrap
#			which runs inst and supplies its input, calls 
#			InstallLicense to create the demo license, and
#			can optionally check your floppy drive to see if it
#			is configured correctly for SoftPC - i.e. no msdosd
#			or mediad running on it.
#


# We must first find out whether to use msdosd or mediad for floppy status
# checks. We do this here so the getfloppy() function can print out the 
# correct message.

if test -x /usr/etc/mediad
then
	MEDIAPROG=/usr/etc/mediad
	MEDIAPROGNAME=mediad
elif test -x /usr/etc/msdosd
then
	MEDIAPROG=/usr/etc/msdosd
	MEDIAPROGNAME=msdosd
fi

# getyesno() function to get a yes or no answer and no other from the user.
# Loops until user has entered valid input. Returns the answer in $ANSWER
# as either yes or no.
# Accepts either y, n, yes, or no, in any mix of case.
getyesno(){
while read ANSWER
do
	ANSWER=`echo $ANSWER | tr '[A-Z]' '[a-z]'`
	case $ANSWER in
	y|yes)
		ANSWER=yes
		break
		;;
	n|no)
		ANSWER=no
		break
		;;

	*)
			echo "You must answer yes or no : \c"
	esac
done
}

# Exit this script, displaying the message passed in $1, and asking the user
# to press return before quitting.
exit_wrapper(){
	echo "\n$1"
	echo "\nPress enter to exit this window...\c "
	read ans
	exit 1
}

# Ask the user for a floppy device name. Allow a blank name - this means
# that no checking for msdosd or mediad will be done.
# Loops until either valid input is obtained, or a blank name is input.
# The user can enter an incomplete pathname, in which case the script will
# prepend first /dev, and then /dev/rdsk to try and find the file, which must
# be readable and be a character special file.
getfloppy(){

	# Check that the MEDIAPROG variable is set to something.
	if test -z "$MEDIAPROG"
	then
		return
	fi

	echo "\nTo use your floppy drive with SoftPC, the $MEDIAPROGNAME daemon must not be running
on that floppy drive. So that I can check whether the $MEDIAPROGNAME daemon is running
on the floppy drive, please enter the name of the floppy device you will be 
using with SoftPC and press enter. 
If you do not specify any device, then no checking will be done.
Enter /dev/rdsk/fds0d2.3.5hi for the middle slot,
or    /dev/rdsk/fds0d3.3.5hi for the top slot."

	while echo "Please enter the floppy device name : \c"
	do
		read FLOPPY

		# first test for blank entry - break if blank
		if test -z "$FLOPPY"
		then
			break
		fi

		# Look for the file. If you can't find it as $FLOPPY
		# then look for it as /dev/$FLOPPY, and then /dev/rdsk/$FLOPPY
		if test ! -r "$FLOPPY"
		then
			if test ! -r /dev/"$FLOPPY"
			then
				if test ! -r /dev/rdsk/"$FLOPPY"
				then
					echo "Floppy device $FLOPPY does not exist."
					continue
				else
					FLOPPY="/dev/rdsk/$FLOPPY"
				fi
			else
				FLOPPY="/dev/$FLOPPY"
			fi
		fi
		if test ! -c "$FLOPPY"
		then
			echo "The floppy device $FLOPPY must be a character special device."
		else
			echo "\nUsing device name $FLOPPY"
			break
		fi
	done
}


echo "Before continuing, you must have obtained a demo license from Insignia."
echo "Have you done this? y/n : \c"
getyesno

test $ANSWER = no && {
	exit_wrapper "You must obtain a demo license before installing SoftPC.
USA customers please call 800 848 7677.
European customers please call +44 494 459 426
All other customers please call +1 415 694 7677"
}

echo "\nIf you mount the SoftPC directory from a remote server to run SoftPC, you will 
need a license file on the local machine. To create a license file, login as
root, run the program /<nfs-mount-point>/usr/lib/SoftPC/bin/InstallLicense and
re-enter the license data supplied by Insignia."

echo "\nInstalling SoftPC. This may take a little while. 
Please wait until prompted before typing anything."


# Find the version of insight installed on the machine.
INSIGHT_LINE=`versions -n insight.sw.client | grep insight.sw.client`
if test $? = 0
then
	INSIGHT_VER=`echo $INSIGHT_LINE | awk '{print $3}'`
	if [ $INSIGHT_VER -le 1006000223 ]
	then
		INSIGHT_VER=1
	elif [ $INSIGHT_VER -ge 1006000300 ]
	then
		INSIGHT_VER=2
	else
		# Some funny version between the two. As the inst package
		# specifies that the version must be one of the two above,
		# then if we get a version in between, we won't be able to
		# install it. We should never get a version in between anyway.
		INSIGHT_VER=0
	fi
else
	# No version installed - don't install the books
	INSIGHT_VER=0
fi

# Call instwrap which will run inst and supply the input to inst so that the
# user doesn't have to type anything.
$SOFTPCDIR/instwrap $SOFTPCDIR $INSIGHT_VER 
ERRCODE=$?
test $ERRCODE -ne 0 && {
	exit_wrapper "SoftPC was not installed correctly"
}


# Make sure that SoftPC is there now - we can't trust the error code returned
# by instwrap, so we double check
# Check each package in turn. If the core package is not installed, then exit.
# If any of the others are not there, then just give a message.

if versions softpc.sw.eoe | grep "Nothing satisfies" > /dev/null
then
        exit_wrapper "The SoftPC core package was not installed correctly.
You will not be able to run SoftPC."
fi

MISSING_PACKAGE=false
if versions softpc.man.flexlm | grep "Nothing satisfies" > /dev/null
then
	echo "The FLEXlm manual pages have not been installed."
	MISSING_PACKAGE=true
fi

if versions softpc.sw.flexlm | grep "Nothing satisfies" > /dev/null
then
	echo "The FLEXlm binaries have not been installed."
	MISSING_PACKAGE=true
fi

if versions softpc.man.relnotes | grep "Nothing satisfies" > /dev/null
then
	echo "The SoftPC release notes package was not installed.
See the READ.ME file in /usr/lib/SoftPC for release notes."
	MISSING_PACKAGE=true
fi

if versions softpc.sw.windows | grep "Nothing satisfies" > /dev/null
then
	echo "The MS Windows installation files have not been installed.
You will still be able to use the pre-installed Windows disk to run MS Windows."
	MISSING_PACKAGE=true
fi

# Even if we don't request installation of the books, we will still get an
# NFS error reported by inst if /usr/Insight is mounted as NFS. So we have to
# see if the books are not there because we didn't want them to be, or because
# of the NFS error.
if versions softpc.man.insight$INSIGHT_VER | grep "Nothing satisfies" > /dev/null
then
	echo "The Insight help books for SoftPC have not been installed."
	if test $INSIGHT_VER -ne 0
	then
		# There must have been an NFS error
		MISSING_PACKAGE=true
	else
		echo "This is because the Insight viewer is not installed on your system.\n"
	fi
fi

if test $MISSING_PACKAGE = true
then
	echo "\nNot all of the ancillary SoftPC packages have been installed,
but the core package has been installed successfully, and you will be able to 
run SoftPC with Windows."
fi


# Now install a demo license - user input will be required.
$SPCHOME/bin/InstallLicense
ERRCODE=$?
test $ERRCODE -ne 0 && {
	exit_wrapper "InstallLicense returned an error code of $ERRCODE"
}

# We can't trust the return value of InstallLicense, so we have to ask the
# user for confirmation that everything went OK.
echo "\nPlease confirm that a SoftPC license is now available (y/n): \c"

getyesno

test $ANSWER = no && {
	exit_wrapper "You must obtain a demo license before installing SoftPC."
}

if test -n "$MEDIAPROG"
then
	# Get the floppy drive device name. Allow blank entry
	getfloppy

	SKIP=false

	# If floppy device name entered, then do msdosd or mediad checks.
	if test $FLOPPY
	then
		# Get the floppy status with msdosd -q or mediad -q
		# The return status is a bit mask. See the msdosd or
		# mediad man page.

		$MEDIAPROG -q $FLOPPY
		RETVAL=$?

		# The bit program will set environment variables 
		# called BIT[0123]SET to either true or false depending 
		# on whether the corresponding bit is set.
		eval `./bit $RETVAL`

		# We now have the variables BIT[0123]SET set to either true 
		# or fale

		# Bit 0 means error occurred
		if test $BIT0SET = true
		then

	echo "\nAn error occurred while investigating the status of the floppy drive.
	No checking on the floppy status will be done.
	You may need to kill the $MEDIAPROGNAME daemon manually."

		elif test $RETVAL -ne 0
		then

			# Bit 1 means msdosd or mediad is running for that 
			# device
			if test $BIT1SET = true
			then
	echo "\nDo you want the $MEDIAPROGNAME daemon attached to $FLOPPY to be killed? (y/n): \c"
				getyesno
				if test $ANSWER = yes
				then
					if test $MEDIAPROG = /usr/etc/msdosd
					then
						$MEDIAPROG -k $FLOPPY
					else
						$MEDIAPROG -k
					fi
				else
					SKIP=true
				fi
			fi

			if test $SKIP = false
			then
			# We have to test bit 3 before bit 2, because if the
			# floppy has been mounted manually, then bit 2 will not
			# be set even if there is an fsd.auto entry. So we
			# do another msdosd -q to see if the floppy has been
			# unmounted, and use the return value of that to check
			# for the fsd.auto entry.

			# Bit 3 means the floppy has been mounted manually
				if test $BIT3SET = true
				then
	echo "\nThe floppy device $FLOPPY has been mounted manually.
	Would you like it unmounted? (y/n): \c"
					getyesno
					if test $ANSWER = yes
					then
						umount $FLOPPY
						$MEDIAPROG -q $FLOPPY
						RETVAL=$?
						eval `./bit $RETVAL`
						if test $BIT3SET = true
						then
	echo "\nThe unmount of the floppy device $FLOPPY failed.
	You will need to unmount the floppy manually to use it with SoftPC,
	or you may access it as an FSA drive instead."
							SKIP=true
						fi
					else
	echo "\nIf you wish to use this floppy drive with SoftPC, you must either
	unmount it manually, or you may use it as an FSA drive instead."
						SKIP=true
					fi
				fi

				if test $SKIP = false
				then
				# Bit 2 means there is an /etc/fsd.auto entry
					if test $BIT2SET = true
					then
	echo "\nAn entry exists for this floppy device in /etc/fsd.auto. Do you wish
	to remove this entry? (y/n): \c"
						getyesno
						if test $ANSWER = yes
						then
							$MEDIAPROG -r $FLOPPY
						fi
					fi

				fi	# SKIP = false

			fi	# SKIP = false
		else
			echo "\nThere is no $MEDIAPROGNAME running on this floppy device."
		fi	# ERR_CHECK = -1

	fi	# test $FLOPPY
fi


# All installed OK.

echo "\nDo you wish to run SoftPC now? (y/n): \c"
getyesno
if test "$ANSWER" = yes
then
	# If the .spc files weren't there to start with, then remove them
	# afterwards, because otherwise they will be owned by root.
	# Don't really want to do this if $HOME is not set, so don't try.
	# Some login environments don't set $HOME.
	if test -f $HOME/.spcconfig || test -z "$HOME"
	then
		SPCCONFFILE=exists
	fi
	SPCHOME=/usr/lib/SoftPC export SPCHOME
	/usr/lib/SoftPC/bin/SoftPC
	if test "$SPCCONFFILE" != "exists"
	then
		rm -f $HOME/.spcconfig $HOME/.spccmosram $HOME/.spcprofile
	fi
fi

echo "\nTo run SoftPC, log out and back in again, start the WorkSpace and 
double-click on the SoftPC icon. 
If you wish to run SoftPC without logging out, type the following from a
shell window:
	in a c-shell:				in a bourne shell:
	setenv SPCHOME /usr/lib/SoftPC		SPCHOME=/usr/lib/SoftPC
	$SPCHOME/bin/SoftPC &			export SPCHOME
						$SPCHOME/bin/SoftPC &

To use your floppy drive with SoftPC, from the SoftPC Options menu select
Disk Drives > Open Drive... In either of the floppy drive A: or B: Device File
Name boxes enter /dev/rdsk/fds0d2.3.5hi for the middle slot or
/dev/rdsk/fds0d3.3.5hi for the top slot.
This information is also in the file /usr/lib/SoftPC/READ.ME
Press enter to exit this window.\c"

read ans

exit 0
