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

#
#   genperms - generate list of permlists to feed to /etc/serialize
#

# Create temperary files and cast variables

: ${OK=0} ${FAIL=1} ${CONTINUE=2} ${STOP=10} ${HALT=11}

TEMP_1=/tmp/genp1$$
TEMP_2=/tmp/genp2$$

> $TEMP_1
> $TEMP_2

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

# Append to that list all the files in perm lists that aren't part
#    of a bundled system.

append_non_bundled() 
{
	for i in /etc/perms/*
	do
		[ -f "$i" ] && {
		    grep $i $TEMP_2 > /dev/null || {
			ser=
			eval `sed -n '/^#ser=/s/#//p' $i` 2> /dev/null || {
			    echo "serialization gathering failure with $i" >&2
			    cleanup $FAIL
			}
			[ -n "$ser" ] && echo $i >> $TEMP_2
		    }
		}
	done
}


# Cleanup - remove temp files and exit

cleanup() 
{
	case $1 in
		$OK) cat $TEMP_2
	esac
	rm $TEMP_1 $TEMP_2
	exit $1
}


# Make sure the file exists 

checkfiles() 
{
	for i in `cat $TEMP_1`
	do
		[ -f "$i" ] && {

# Stupid bundle lists often list a package with a serialization string
# even when they don't have any serialized files.

			ser=
			eval `sed -n '/^#ser=/s/#//p' $i` 2> /dev/null || {
			    echo "serialization gathering failure with $i" >&2
			    cleanup $FAIL
			}
			[ -n "$ser" ] && echo $i
		}
	done > $TEMP_2
}


# main 

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

[ -d /etc/perms/bundle ] || {
	append_non_bundled
	cleanup $OK
}

for i in /etc/perms/bundle/*
do
	awk '

# Weed out the comments in the bundle lists and string the lines of
# text ended with a backslash together to form one big line.

		$0 !~ "^#" {
			pos=index($0, "\\")
			if (pos == 0)
				printf("%s\n", $0)
			else
				printf("%s", substr($0, 1, pos - 1))
	} ' $i | awk 'BEGIN{FS=":"}

# Use only the lines which have serialization strings in them, all the
# other ones are useless. Then find the perms list in that line.

	$0 ~ "serial=\"" {
		if ($0 !~ "serial=\"\"")
		    for (i=1;i<= NF; i++)
			if ($i ~ "perms")
			    printf("%s\n", substr($i, index($i, "=") + 1))
	} ' | sed '
		s|^\.||g
		s| ||g
		s|	||g
		s|/tmp/|/etc/|g
	'
# Then send it through the sed script above so we can get rid of the .s, 
# tabs and spaces; as well as turn /tmp/perms/<prd> into /etc/perms/<prd>

done | sort -u > $TEMP_1

checkfiles
append_non_bundled
cleanup $OK
