:
#	%Z% %M% %I% %E% %Q%
#
#	Copyright (C) The Santa Cruz Operation, 1988, 1989, 1990.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, and should be treated as Confidential.
#

#	setall
#	runs the system configuration tools
#

PATH=/bin:/usr/bin:/etc:/.mitdir:/MIT/.mitdir:.
export PATH

# System configuration scripts.  Add script name
# to SCRIPTS variable list and store file in 
# /MIT/protodist/MIT/.mitdir on the MIT workbench, or in
# /MIT/.mitdir on the prototype machine.  Give root
# ownership and 755 permissions modes.

SCRIPTS="setdate setname setserial"

REBOOT="n"
ALLDONE="n"
REMOVELIST=
NEWREMOVELIST=

# Set the standard exit values
: ${OK=0} ${FAIL=1} ${STOP=10}

# Prohibit deleting out or killing program
trap '' 1 2 3 15

#
# setall internal functions cannot be modified
#

getyn() {
	while	echo "\n$* (y/n) \c">&2
	do	read yn rest
		case $yn in
		[yY])	return $OK 			;;
		[nN])	return $FAIL			;;
		*)	echo "Please answer y or n" >&2	;;
		esac
	done
}

runscript ()
{
script=$1

	$script
	retval=$?
	case $retval in
		0)	# If script ran with a return code of OK,
			# place the script in the removal list.
			REMOVELIST="$REMOVELIST $1"
			return $OK
			;;
		10)	return $OK
			;;
		*)	echo "Warning:	$1 failed with return code $?"
			echo "		$1 not removed."
			echo "Press <Return> to continue. \c"
			read a
			return $FAIL
			;;
	esac
	return $OK
}

# Ask configuration questions
answerconfig() {
	# Loop that asks the questions
	for script in ${SCRIPTS}
	do
		# If the shell script exists, run it
		if [ -r "$script" ]
		then
			runscript $script
		fi
	done

	# Check that questions are answered
	if checkempty
	then
		return $FAIL
	else
		return $OK
	fi
}

listanswers() {
	echo " "
	echo "The following questions have been answered"
	echo " (remaining questions are requested later):"
	echo " "
	for script in $REMOVELIST
	do
		COUNT=`expr $COUNT + 1`
		echo "${COUNT}) \c"
		case $script in
			setname)
				echo "System name is: \c"
				uname
			;;
			setpasswd)
				echo "System root password has been set."
			;;
			setdate)
				echo "System time information is as follows:"
				echo "\tTimezone is: \c"
				. /etc/TIMEZONE
				echo "$TZ"
				echo "\tDate is: \c"
				date
			;;
			setserial)
				echo "Serialization complete."
			;;
			*) echo	"$script execution complete."
			;;
		esac
	done
	return $OK
}

pickchange() {
		echo " "
		echo "Would you like to change your answer to any"
		echo "of these questions ?  Enter the number"
		echo "to change or q to quit. \c"
		read PICK
		if [ "$PICK" = "q" ]
		then
			return $FAIL
		fi
		return $OK
}

delremoveentry() {
DELENTRY=$1
	# Make a new list counting the chosen
	# question as not being answered
	NEWREMOVELIST=
	for s in $REMOVELIST
	do
		if [ "$s" = "$DELENTRY" ]
		then
			:
		else
			NEWREMOVELIST="$NEWREMOVELIST $s"
		fi
	done
	REMOVELIST=$NEWREMOVELIST
	return $OK
}

checkempty()
{
	if [ -n "$REMOVELIST" ]
	then
		return $FAIL
	else
		return $OK
	fi
}

# Start the config loop for choice and modifications
changeconfig()
{
	# Loop forever until we are ready to quit out
	while :
	do
		# Check for empty list
		checkempty && return $OK
			
		# Numerically list the answers to questions
		COUNT=0
		listanswers

		# Ask if any of the answers are being changed
		PICK="q"
		pickchange || return $OK

		# Determine what number represents the question to change
		COUNT=0
		for script in $REMOVELIST
		do
			COUNT=`expr $COUNT + 1`
			if [ "$COUNT" = "$PICK" ]
			then
				# Delete entry for the remove list
				delremoveentry $script
				# Ask the chosen question again
				runscript $script
			fi
		done
	done
	return $OK
}

checkrelink()
{
	NAMEFLAG="n"
	SERFLAG="n"

	# If in the removelist, then set flags to yes
	for file in $REMOVELIST
	do
		[ "$file" = "setname" ] && NAMEFLAG="y"
		[ "$file" = "setserial" ] && SERFLAG="y"
	done
	[ "$NAMEFLAG" = "n" ] && [ "$SERFLAG" = "n" ] && return $OK

	# if NAMEFLAG is set to yes and
	# If SER was not in the remove list but has already been
	# done (that is, if seriaization occurred), set SERFLAG
	# to yes, so the name change can relink.
	[ "$SERFLAG" = "n" ] && [ "$NAMEFLAG" = "y" ] && [ -r ./setserial ] || SERFLAG="y"

	#if the system name is currently being set 
	#and we have not are are currently not serilizeing 
	#the system then we cannot relink because of a name change
	[ "$NAMEFLAG" = "y" ] && [ "$SERFLAG" = "n" ] && {
		echo " "
		echo "Warning:"
		echo "The system name changed, but system relinking cannot"
		echo "proceed until the system has been reserialized."
		echo " "
		echo "Press <Return> to continue. \c"
		read a
		return $OK
	}

	#if SERFLAG is yes we can relink the system
	[ "$SERFLAG" = "y" ] && {
		if mitrelink
		then
			REBOOT="y"
		else
			echo "Error:  Kernel relink failed."
			echo "Press <Return> to continue. \c"
			read a
			REBOOT="n"
		fi
	}
	return $OK
}

checkdone() 
{
	for file in $SCRIPTS
	do
		[ -r "$file" ] && 
		{
			ALLDONE="n"
			return $FAIL
		}
	done
	ALLDONE="y"
	return $OK
}

infonote() {
	echo "\nThis machine has been installed with the"
	echo "Mass Installation Toolkit."
	echo "\nYou are asked to supply system information that"
	echo "must be entered before the target machine is used"
	echo "in multiuser mode.  You have two choices for entering"
	echo "this information, doing so now or waiting until the"
	echo "next time the system is rebooted.\n"
	return $OK
}

removelist()
{
	echo " "
	echo "Removing: $REMOVELIST"
	echo " "
	rm -f $REMOVELIST > /dev/null 2>&1
	checkrelink
	return $OK
}

checkreboot() {
	if [ "$FLINSTTIME" = "yes" ]
	then
		return $OK
	fi
	[ "$REBOOT" = "y" ] && {
		echo "Shutting down the system.  When you reboot,"
		echo "reserialization is complete."
		getyn "Are you ready to shutdown ?" && haltsys
	}
	return $OK
}

finalclean() {

	# Go to root to remove the .mitdir directory
	cd /

	echo "Updating swconfig file."
	echo "date=\"`date`\" \\" >> /usr/lib/custom/history
	echo "prd=mitsys rel=NA upd= typ=u386 set=\"Installed Using SCO MIT\" \\" >> /usr/lib/custom/history
	echo "action=Installation status=\"Successful\" \\" >> /usr/lib/custom/history
	echo "pkgs=\"ALL\"" >> /usr/lib/custom/history
	echo " " >> /usr/lib/custom/history

	echo "\nMass installation is complete."
	echo "Removing mass installation tools."
	if grep setall /.profile > /dev/null 2>&1
	then
		copy -om /.mitdir/.profilesave /.profile
	fi
	rm -f /etc/rc2.d/S02MITSYSINDIV > /dev/null 2>&1
	rm -rf /.mitdir > /dev/null 2>&1

	return $OK
}

prep() {
	echo "Mounting filesystems for MIT configuration..."
	# Mount all file systems,
	# but not for a hard disk pump
	/usr/bin/mnt -rc
	return $OK
}

#main

if [ "$1" = "-HD" ]
then
	HDBASED=yes
fi

# Prepare the system
# If hard disk-based do not run the prep stuff
[ "$HDBASED" = "yes" ] || prep

# Move to /.mitdir
cd /.mitdir

# fixpermissions
mitfixperm

# Check if all done
checkdone && finalclean

# Echo information
infonote

# Answer configuration questions
answerconfig && changeconfig

# Remove files if REMOVELIST is non-null
if checkempty
then
	echo " "
	echo "No questions answered."
else
	removelist
fi

# fixpermissions
mitfixperm

# Clean system if all done
if checkdone
then
	finalclean
else
	echo " "
	echo "Questions remain that need to be answered."
fi

# Check for reboot if we are not pumping disk->disk
[ "$CUTHD" = "active" ] || checkreboot

exit $OK 
