:
#	%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.
#

#
#	setserial - the high level program that reserializes a system
#

: ${OK=0} ${FAIL=1} ${STOP=10} 

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

MITSERLOG=/usr/lib/custom/keys
PWD=`pwd`
TMP=/tmp/setser$$
rm -f $TMP        
touch $TMP        

TRAPMSG="<Del> key pressed or program killed.  Exiting."

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
}

doserial()
{
	list=`genperms`
	for permlist in $list
	do
		echo $permlist >> $TMP        
		case `basename $permlist` in
			inst) 	;;
			*) 	/etc/serialize $permlist
				;;
		esac
	done
	return $OK
}

cleanserial()
{
	rm -f $TMP        
	rm -f /tmp/*.ser
	if [ "$AUTOSER" = "yes" ]
	then
		autoser 0 
		echo "You may now remove the serialization floppy."
	fi
}

doauto() 
{
	echo " "
	echo "If you have the optional MIT floppy-based"
	echo "serialization feature, insert the floppy into the"
	echo "primary drive at this time and close the drive door."
	echo " "
	echo "At the prompt, enter y if you have floppy-based"
	echo "serialization, or n if you do not.  If you do not"
	echo "have floppy-based serialization, get your activation"
	echo "keys and serial numbers ready to be entered manually.\n"
	getyn "Execute floppy-based serialization?" && {
		echo "Floppy-based serialization begins..."
		autoser 1 || {
			echo "Error: floppy-based serialization failed."
			return $FAIL
		}
		AUTOSER="yes"
		return $OK
	}
	return $FAIL
}

updateserno()
{
	[ -r /tmp/*.ser ] && {
		for i in /tmp/*.ser
		do
			#ser=product string
			ser=
			eval `sed -n '/^#ser=/s/#//p' $i` 2> /dev/null || {
	    			echo "Serialization gathering failure with $i" >&2
	    			return $FAIL
			}
			file=`echo $i | sed 's/.ser$//'`
			. $i
			MITPRODUCT="`basename $file`"
			mitserlog $_serial $_key
		done
	}
	return $OK
}

viewserlog() {
	if [ -r "$MITSERLOG" ]
	then		
		echo " "
		echo "A log of all serialization activities on"
		echo "this system was added to ${MITSERLOG}."
		echo "Do you want to examine this log now, or"
		echo "review the log later?"
		getyn "View serialization log now?" && pg $MITSERLOG
	fi
	return $OK
}

mitserlog ()
{
	mitser=$1
	mitact=$2

	NOPERMS="yes"
	[ -r "/etc/perms/$MITPRODUCT" ] && NOPERMS="no"
	[ -r "/etc/perms/bundle/$MITPRODUCT" ] && NOPERMS="no"
	if [ "$NOPERMS" = "yes" ]
	then
		echo " "
		echo "================== WARNING ===================="
		echo " "
		echo "The serialization floppy contains a key for"
		echo "$MITPRODUCT.  However, this product is not"
		echo "on your system.  Because this instance of the"
		echo "activation key and serial number for this product"
		echo "were removed from the floppy, write down the"
		echo "information now so that you can enter it manually"
		echo "when $MITPRODUCT is installed later."
		echo "Serial Number: $mitser	Activation Key: $mitact"
		echo " "
		echo "Press <Return> to continue. \c"
		read a
		echo "Serialization continues..."
		return $OK
	fi

	[ -f $MITSERLOG ] || echo "" >$MITSERLOG
	echo "MIT Serialization on `date`" >>$MITSERLOG
	echo "prd=$MITPRODUCT _serial=$mitser _key=$mitact" >>$MITSERLOG
	echo " " >> $MITSERLOG
	return 0
}

#main

trap 'echo "$TRAPMSG" >&2 ; exit $FAIL' 1 2 3 15

getyn "Do you wish to serialize the system at this time ? " || exit $STOP

echo "Executing MIT serialization..."

doauto 
doserial 
updateserno
cleanserial
[ -n "$DEBUG" ] && viewserlog

echo "MIT serialization complete.\n"
exit $OK
