:
#	@(#) serialize 22.1 90/03/19 
#
#	Copyright (C) The Santa Cruz Operation, 1989.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, and should be treated as Confidential.
#
#	/etc/serialize <permsfile>
#		-serializes a list of files obtained from a perms list
#

# set the environment variables
PATH=/etc:/bin:/usr/bin
workingdir=/

# set variables
remap=
use_super=

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

# Standard functions

# print restricted rights legend on user screen
rights()
{
	echo "

   ------------------------------------------------------------------------
  |                                                                        |
  |                       RESTRICTED RIGHTS LEGEND                         |
  |                                                                        |
  |   Use, duplication, or disclosure by the U.S. Government is subject    |
  |   to the restrictions as set forth in subparagraph (c)(1) of           |
  |   FAR 52.227-19 - Commercial Computer Software -- Restricted Rights,   |
  |   or  subparagraph (c)(1)(ii)  of  DFAR 52.227-7013 - Rights in        |
  |   Technical Data and Computer Software. \"Contractor/Manufacturer\" is   |
  |                      Altos Computer Systems, Inc.                      |
  |                      2641 Orchard Parkway                              |
  |                      San Jose, CA                                      |
  |                      95134, USA.                                       |
  |                                                                        |
   ------------------------------------------------------------------------

"
}

# Prompt with mesg, return non-zero on q
prompt() {
	while	echo "\n${mesg}or enter q to quit: \c" >&2
	do	read cmd
		case $cmd in
		+x|-x)	set $cmd					;;
		Q|q)	return $FAIL					;;
		!*)	eval `expr "$cmd" : "!\(.*\)"`			;;
		"")	# If there is an argument use it as the default
			# else loop until 'cmd' is set
			[ "$1" ] && { 
				cmd=$1
				return $OK
			}
			: continue
			;;
		*)	return $OK					;;
		esac
	done
}

# Prompt for yes or no answer - returns non-zero for no
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 1
}

# /etc/serialize functions

# build a variable $serfiles, from /etc/perms/this_product
set_serfiles () {
	eval `sed -n '/^#ser=/s/#//p' $permsfile` 2> /dev/null || {
		echo "\nError. Failed to get list of files to serialize from \
$permsfile" 
		exit $FAIL
        }
	[  "$ser" = "" ] && {
		echo "$permsfile does not have any files that require \
serialization."
		return 1
	}
	for i in $ser
	do
		[ -f "$i" ] && serfiles="$serfiles $i"
	done
	
	# make sure there are files to serialize on the hard disk
	[ "$serfiles" ] || exit $OK

	return 0
}

# return 0 if bundle;  return 1 if not bundle 
is_bundle () {
	[ -d /etc/perms/bundle ] || return 1

	cd /etc/perms/bundle
	bundle_files=`echo *`
	cd $workingdir
	[ "$bundle_files" = "*" ] && return 1

	# grep for /tmp/perms/[product] in bundle list
	bund_perm="./tmp/perms/`basename $permsfile`"

	for i in $bundle_files
	do
		if grep "perms=$bund_perm[: 	]" /etc/perms/bundle/$i > /dev/null 2>&1
		then
			# this product is part of a bundle product
			this_bundle=/etc/perms/bundle/$i
			tmppermsflag=true
			# $set gets bundle= value, for use by brand
			get_bundle_set
			return 0
		fi
	done
	# check for any mdperms 
	for i in $bundle_files
	do
		if grep "mdperms=$permsfile" /etc/perms/bundle/$i > /dev/null 2>&1
		then
			this_bundle=/etc/perms/bundle/$i
			get_bundle_set
			return 0
		fi
	done
	# product is not in any bundle list
	return 1
}

# set $compprd value. Return 0 is serialization is remapped; return 1 if not
is_serial () {
	permsrch=".$permsfile"
	[ $tmppermsflag ] && permsrch="$bund_perm"

	srch_bundle=$this_bundle

	# set compprd= to prd= field in record containing $permsrch
	exec 3>&0 < $srch_bundle
	while read bline
	do
		echo $bline |
		if grep -s "$permsrch[: 	]" > /dev/null
		then
        		expr "$bline" : ".*prd=\([^: 	]*\)" > /tmp/serial2.$$
			echo $bline |
			if grep -s "serial=" > /dev/null
			then
				> /tmp/serial1.$$
			fi
		fi
	done
	exec 0<&3 3>&-
	compprd=`cat /tmp/serial2.$$`
	rm -f /tmp/serial2.$$
	[ -f /tmp/serial1.$$ ] && {
		rm /tmp/serial1.$$
		return 0
	}
	return 1
}
	
# parse the fields in the bundle record, determine if supercede= is set
# returns 0 if a) supercede= is found, and b) if the file it refers to 
# is installed, c) the component is referenced in the superceding bundle
# list.  Otherwise, returns 1. 
#
is_supercede () {
	if sup_record=`grep -s "supercede=" $this_bundle` 
	then
		supercede=`expr "$sup_record" : ".*supercede=\([^: 	]*\)"`
		[ -f /etc/perms/bundle/$supercede ] || return 1

		super_bundle=/etc/perms/bundle/$supercede
		super_perms=$permsfile
		[ $tmppermsflag ] && super_perms=$bund_perms
		return 0
	else
		# no bundle lists supercede this bundle list
		return 1
	fi
}

# get the prd= & set= value out of the bundle= line in the bundle list
get_bundle_set () {
	srch_bundle=$this_bundle
	[ $use_super ] && srch_bundle=$super_bundle

	exec 3>&0 < $srch_bundle
	while read bline
	do
		echo $bline |
		if grep -s "bundle=" > /dev/null
		then
        	      expr "$bline" : ".*bundle=\"\([^\"]*\)" > /tmp/serial3.$$
        	      expr "$bline" : ".*prd=\([^: 	]*\)" > /tmp/serial4.$$
		fi
	done
	exec 0<&3 3>&-
	set=`cat /tmp/serial3.$$`
	prd=`cat /tmp/serial4.$$`
	rm -f /tmp/serial[34].$$
}

# this function used when product is not part of a bundle list
get_set_prd () {
	eval `grep "#prd=" $permsfile | sed "s/#//"`
	eval `grep "#set=" $permsfile | sed "s/#//"`
}

# Brand files, checking for already-defined serialno/actkey in /tmp/$prd.ser
# Usage: serialize filename...
# Dependencies: prompt(), getyn(), rights(), error() 
# Returns 0 if the files are successfully branded or 1 upon failure
#
serialize () {
	_file="$*"
	if	[ -s ./tmp/$prd.ser ] 	# serialization occurred previously
	then	. ./tmp/$prd.ser 	# so, find out old serialization values
	else	
		rights
		echo "${set:-Product} Serialization.

When prompted, use the serial number and activation key included 
with the ${set:-Product} distribution."
	fi
	until	[ "$_serial" -a "$_key" ] || {
			mesg="Enter your serial number "
			prompt || return 1
			_serial=$cmd
			mesg="Enter your activation key " 
			prompt || return 1
			_key=$cmd
		}

		if [ "$remap" ]
		then
		     /etc/brand -b $compprd $bundlelist $_serial $_key $_file
		else
		     /etc/brand -q $_serial $_key $_file
		fi

	do	case $? in
		1)	error "please try again"	;;
		2)	error "Invalid activation key"	;;
		3)	error "$_file is already serialized"
			break 				;;
		esac
		_serial= _key=
		getyn "Do you wish to try activation again?" && continue
		error "
	${set:-This product} will not work correctly without proper
	serialization.  Contact your support center to obtain a
	valid activation key and re-install."
		return 1
	done
	unset _file
	echo "_serial=$_serial _key=$_key" > ./tmp/$prd.ser 
	return 0
}

usage () {
	echo "\n\tUsage:\t/etc/serialize <permsfile>\n\t\
<permsfile> is the permissions file (e.g. /etc/perms/unixrts)\n\tthat \
contains a list of files to be serialized.\n"
	exit $FAIL
}


####  begin main  ####

cd $workingdir

[ $# -ne 1 ] && usage

# permsfile is expected to be "/etc/perms/[product]"
permsfile=$1
[ -f "$permsfile" ] || {
	echo "Error. $permsfile does not exist."
	exit $FAIL
}

# this is an environment variable that means delay serialization 
[ "$_SERIALIZE" ] && exit $OK

# if there are files to serialize, proceed; otherwise exit
set_serfiles || exit $OK

# determine if this product is part of a bundle
if is_bundle 
then
	# set $compprd & determine if remapping is necessary
	if is_serial
	then
		remap=yes
		# is this bundle list superceded by any other bundle list?
		if is_supercede
		then
			# $set gets the bundle= value from the supercede bundle 
			use_super=true
			# reset $compprd (likely to be identical)
			is_serial
		fi

		# $set gets bundle= value from appropriate bundle list
		# $prd gets bundle prd= value from appropriate bundle list
		get_bundle_set

		# set $bundlelist for use by brand
		bundlelist=$this_bundle
	else
		# bundle, but no remapping; get set= & prd= from perms list 
		get_set_prd
		compprd=$prd
	fi

else
	# product is not part of a bundle; get set & prd from product perms file
	get_set_prd
fi

# serialize the files
serialize $serfiles || exit $FAIL

exit $OK
