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

#	autoser
#		- executes floppy based serialization
#

: ${OK=0} ${FAIL=1} ${STOP=10}
PATH=/bin:/usr/bin:/etc

####################################################################
#
# Global Environment Variables
#
#	root:	directory to be used as root
#	mntdir:	directory where floppy is mounted
#	barc:	block device to be mounted with serialization info
#
####################################################################

root="/"
mntdir="/MITSERmnt"
barc="/dev/install"
pfile=
dfile= 
MITSERLOG=/etc/serialno

cd /

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
}

# Print an error message
error() {
	echo "\nError: $*" >&2
	return $FAIL
}

# mount the autoserial floppy
automount() {

	#this happens multiple times. Just want to make
	#sure that the directory is there
	[ -d "$mntdir" ] || {
 		mkdir $mntdir > /dev/null 2>&1
	}
	chmod 777 $mntdir > /dev/null 2>&1

	until 	mount $barc $mntdir 
	do 	fsck $barc || {
			error "Cannot mount autoserialization floppy."
			return $FAIL
		}
		getyn "Do you want to quit? (y/n)" && return $FAIL
	done
	return $OK
}

# umount the autoserial floppy
umountauto() {
	umount $barc || {
		error "Cannot unmount autoserialization floppy."
		sleep 6
		rm -rf $mntdir || echo "Warning: Unable to remove $mntdir"
		return $FAIL
	}
	sleep 6
	rm -rf $mntdir || echo "Warning: Unable to remove $mntdir"
	return $OK
}

# cleans up before exiting
cleanauto() {
	if [ "$1" = "0" ]
	then 	decauto && {
			# remove /tmp/ xsami files
			rm  ${root}/etc/xrelink /tmp/*.xsami 2> /dev/null 
			exit $OK
		}
	else 	# Remove all the successfully branded files
		# in the destination dir
		[ -r "/tmp/allauto.xsami" ] && {
			cd $root
			for serfile in `cat /tmp/allauto.xsami`
			do	rm -f $serfile 2> /dev/null
			done
		}
		rm -f /tmp/*.xsami 2> /dev/null
		exit $FAIL
	fi
}

# preps for cleanup if auto serialization fails
failauto() {
	umountauto 2> /dev/null
	cleanauto $FAIL
}

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

	echo "Date: \c" >> $MITSERLOG
	date >> $MITSERLOG
	echo "Product: $MITPRODUCT" >> $MITSERLOG
	echo "Serialization: MIT autoser floppy" >> $MITSERLOG
	echo "Serial Number: $mitser	Activation Key: $mitact" >> $MITSERLOG
	echo " " >> $MITSERLOG

	return $OK
}

# gets serial number/act key from autoserial floppy and serializes files
getser() {
	cd $root
	if [ -s "$mntdir/autoserial/${1}" ]
	then 	read ser key < $mntdir/autoserial/${1}
		# save info for brand in the .ser file
		echo "_serial=$ser _key=$key" > ${root}/tmp/${1}.ser

		MITPRODUCT=$1
		#production thinks theres an odtws and otdps
		#theres really only odtps with diff activation states
		#this will kludge a fix for us
		#if odtwd then copy to odtps
		if [ -r /tmp/odtws.ser ]
		then
			MITPRODUCT="odtps"
			copy -om /tmp/odtws.ser /tmp/odtps.ser
			rm -f /tmp/odtws.ser
		fi

		# update serialization log
		mitserlog $ser $key

	else	error "$mntdir/autoserial/${1} does not exist or is empty!"
		failauto
	fi
	cd /
	return $OK
}

# serialize all files designated in perm lists which exist on target disk
autoser() {
	automount || cleanauto $FAIL
	allprods=`/bin/ls ${mntdir}/autoserial | grep -v "[OS|HIST]"`
	for pfile in $allprods
	do	
		getser $pfile && echo $pfile >> /tmp/autoprod.xsami
	done
	umountauto || cleanauto $FAIL
	exit $OK
}

# decrement all floppy autoserial files as necessary
decauto() {
	automount || cleanauto $FAIL
	allprods=`/bin/ls ${mntdir}/autoserial | grep -v "[OS|HIST]"`
	for dfile in $allprods
	do sed "1d" < $mntdir/autoserial/${dfile} > /tmp/${dfile}.xsami || {
			error "Cannot decrement $dfile serialization file"
			cleanauto $FAIL
		}

	done
	echo "
Serialization Floppy Summary

product		    serial #'s
name		    remaining
-------------	    ----------"
	dfile=
	for dfile in $allprods
	do lines=`cat /tmp/${dfile}.xsami | wc -l | sed -e "s:^ *\(.*\):\\1:p"`
		echo "$dfile			$lines"
	done
	echo "\n"
	dfile=
	for dfile in $allprods
	do	mv -f /tmp/${dfile}.xsami $mntdir/autoserial/${dfile} || {
			date=`date`
			badprod=$dfile
			echo "
Error in autoserialization on $date.
Could not successfully decrement $dfile 
on the serialization floppy. 
" > /usr/lib/SAMI/errlog.xsami
			for badfile in `cat /tmp/autoprod.xsami`
			do	if [ "$badfile" = "$badprod" ]
				then 	break
				else	echo "Auto serial file $badfile incorrectly decremented" >> /usr/lib/SAMI/errlog.xsami
				fi
			done
		        echo "Cannot complete decrement of $dfile serialization file!
Check the auto serialization log /usr/lib/SAMI/erlog.$$ for details."
			failauto
		}
	done
	umountauto
}

# Put the autoserialization info into the 
# .ser file if the first pass (parm=1).
# Decrement the serial number files on the
# autoserial disk if the second pass (parm=0).

# Set a trap to cleanup and exit _without_ decrementing the 
# serialization files
trap 'failauto' 1 2 3 15

case $1 in
0)	cleanauto $OK
	;;
1)	autoser && exit $OK
	;;
esac
