:
#	@(#) serialize 23.12 91/11/03 
#
#	Copyright (C) 1989-1991 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
#
#	/etc/serialize <permsfile>
#		-serializes a list of files obtained from a perms list
#

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

# set variables
remap=
use_super=
_serial= 
_key=

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

# Standard functions

cleanup()
{
	[ $OASH ] && {
		oadeinit
		clear
	}
	exit $1
}

# print restricted rights legend on user screen
rights()
{
	[ $OASH ] && 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 
                        The Santa Cruz Operation, Inc.                   
                        400 Encinal Street                              
                        Santa Cruz, CA                                 
                        95061, USA.                                   

	" || 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   |
  |                      The Santa Cruz Operation, Inc.                    |
  |                      400 Encinal Street                                |
  |                      Santa Cruz, CA                                    |
  |                      95061, USA.                                       |
  |                                                                        |
  +------------------------------------------------------------------------+

"
}

# Prompt with mesg, return non-zero on q
prompt() {
	[ $OASH ] && {
		wmopen 2 4 50 19 15 
		wmtitle "Serialization"
		form $formdir/ser.form "((ser_form"
		[ "$FIELD0" = "q" -o "$FIELD0" = "Q" ] && return $FAIL	
		[ "$FIELD1" = "q" -o "$FIELD1" = "Q" ] && return $FAIL	
		wmove 1
		werase 2 
		wmclose 2
		_serial="$FIELD0"
		_key="$FIELD1"
		return $OK
	} || {
	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
}

#update the system serialization log
serlog() {
LOGSERNUMBER=$1
LOGPRD=$2
LOGFILE=/etc/serialno
	echo "Date: \c" >> $LOGFILE
	date >> $LOGFILE
	echo "Product prd value: $LOGPRD" >> $LOGFILE
	echo "Serialization: Standard" >> $LOGFILE
	echo "Serial Number: $LOGSERNUMBER" >> $LOGFILE
	echo " " >> $LOGFILE
	return 0
}
	
# /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" 
		cleanup $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" ] || cleanup $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=`basename $permsfile`
	case $bund_perm in
	inst|extmd)	bund_perm=./tmp/perms/ext		;;
	*)		bund_perm=./tmp/perms/$bund_perm	;;
	esac

	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"

	# join together the separate lines of each entry
	while read bline
	do	echo $bline
	done < $this_bundle > /tmp/serial1.$$

	# set compprd= to prd= field in record containing $permsrch
	bline=`grep -s "$permsrch[: 	]" /tmp/serial1.$$`
       	compprd=`expr "$bline" : ".*prd=\([^: 	]*\)"`
	rm -f /tmp/serial1.$$

	# Return 0 if serialization is remapped
	serial=`expr "$bline" : ".*serial=.*"`
	[ $serial -ne 0 ] && 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_perm
		return 0
	else
		# no bundle lists supercede this bundle list
		return 1
	fi
}

# check the bundle list to determine whether it is tape, ie. mediatype=T
# returns 0 if present, returns 1 if not
is_tape () {
	grep mediatype=T $this_bundle > /dev/null 2>&1
	return $?
}

# 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

	# join together the separate lines of each entry
	while read bline
	do	echo $bline
	done < $srch_bundle > /tmp/serial2.$$

	bline=`grep -s "bundle=" /tmp/serial2.$$`
        set=`expr "$bline" : ".*bundle=\"\([^\"]*\)"`
	prd=`expr "$bline" : ".*prd=\([^: 	]*\)"`
	rm -f /tmp/serial2.$$
}

# 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 () {
	MANUALSERIAL=no
	_file="$*"
	if	[ -s ./tmp/$prd.ser ] 	# serialization occurred previously
	then	. ./tmp/$prd.ser 	# so, find out old serialization values
	else	
		[ $OASH ] || {
		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" ] || {

			[ $OASH ] && {
				rights && prompt || return 1
			} || {
				mesg="Enter your serial number "
				prompt || return 1
				_serial=$cmd
				mesg="Enter your activation key " 
				prompt || return 1
				_key=$cmd
			}

			MANUALSERIAL=yes
		}
		if [ "$remap" ]
		then
		     /etc/brand -b $compprd $bundlelist $_serial $_key $_file \
				>/dev/null 2>/dev/null
		else
		     /etc/brand -q $_serial $_key $_file >/dev/null 2>/dev/null
		fi

	do	stat=$?	
		
		case $stat in
		1)	error "please try again"	;;
		2)	error "Invalid activation key"	;;
		3)	error "$_file is already serialized"
			break 				;;
		esac
		
		[ $OASH ] && {
			wmopen 2 4 50 19 15 
			wmtitle "Serialization"
		}
		_serial= _key=
		getyn "Do you wish to try activation again?" && continue
		[ $OASH ] && {
			error "This product will not work correctly without proper serialization."
			return 1
		} || {
			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

	# Code added for 3.2.4 OS cuts, whose M01 may contain fixes to
	# /etc/conf/pack.d/kernel/io.a and /etc/conf/pack.d/kernel/os.a.
	# The entire archives are too big to fit, so the variant objects
	# must be archived in now that os.a has been serialized; io.a should
	# really be updated within custom, but safer not to do that just now.
	#
	fix=/usr/lib/custom/fix
	ker=./etc/conf/pack.d/kernel
	for f in $_file
	do	[ "$f" = "$ker/os.a" ] && {
			[ -x /bin/idar ] && AR=/bin/idar || AR=ar
			for d in io.a os.a
			do [ -d $fix/$ker/$d ] &&
			   ( cd $fix/$ker/$d && $AR ru $workingdir/$ker/$d *.o )
			done
			break
		}
	done >/dev/null 2>&1
	unset fix
	unset ker

	unset _file
	echo "_serial=$_serial _key=$_key" > ./tmp/$prd.ser 
#if serialization is manual then update the serialization log 
	[ "$MANUALSERIAL" = "yes" ] && serlog $_serial $prd
	MANUALSERIAL="no"
	return 0
}

usage () {
	echo "\n\tUsage:\t/etc/serialize <permsfile>\n\t\
<permsfile> is the permissions file (e.g. /etc/perms/rtsmd)\n\tthat \
contains a list of files to be serialized.\n"
	cleanup $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."
	cleanup $FAIL
}

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

# if there are files to serialize, proceed; otherwise exit
set_serfiles || cleanup $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
# find out if this script is run via oash
eval oainit 2>/dev/null && OASH=1 || OASH=

[ $OASH ] && {
	werase
	wrefresh
	dateline $0
	# open main window
	wmopen 1 21 80 4 0
}

serialize $serfiles || cleanup $FAIL

cleanup $OK
