:
#ident	"@(#)mkspec	25.1	11/27/91 Copyright (c) 1991 by Arix Corp."
# mkspec
#
#	usage: mkspec root|usr|full spec_file [spec_file [spec_file [...]]]
#
#	collect spec_files, massage, concatenate and sort
#	on success print the massaged output and return 0
#
#	on errors, print error messages and return non 0.
#
#	turn multiple white space fields into single tabs
#	check directories for "." as a source
#	check for duplicate targets with differencing sources or attributes.
#	prepend paths to each spec_file onto non directory sources.
#
#	d. stone 9/12/90

###################################
####  error checking routines  ####
###################################

# dircheck: 	check that all directories have "." as their source.
#
#		FIX THIS: would be nice to see that all required 
#			  directories are explicitly called out.
dircheck()
{
	if  nawk 'BEGIN { invalid = 0 }
	{
		# if this is a directory entry
		if (substr($5, 0, 1) == "d" && $1 != ".") {
			printf("mkspec: invalid directory entry in %s (%s)\n",\
			  "'$1'", $0);
			invalid++;
		}
	}
	END { exit invalid }' < $1
	then
		return 1		# success
	else
		return 0		# failure
	fi
}

dup_target_check()
{
	if awk 'BEGIN { duplicates = 0; holdtarget = ""; holdline = ""; }
	{
		if ($2 == holdtarget) {
			printf("mkspec: duplicate target:\n%s\n%s\n", \
			  holdline, $0);
			duplicates++;
		}
		holdtarget = $2;
		holdline = $0;
	} END { exit duplicates }' < $1
	then
		return 1		# success
	else
		return 0		# failure
	fi
}

##############################
####  mkspec starts here  ####
##############################

usage="usage: mkspec root|usr|full spec_file [spec_file [spec_file [...]]]"
tmpfile=/tmp/mkspec.$$

#	trap SIGHUP, SIGINT, SIGQUIT, SIGTERM
trap "rm -f $tmpfile;echo mkspec interrupted 1>&2; exit 98" 1 2 3 15

# check the number of arguments
if [ $# -lt 2 ]
then
	echo $usage 1>&2
	exit 99
fi

case $1 in			# are we making a root or usr list?
root|usr|full)
	fs=$1; export fs;;
*)
	echo $usage 1>&2; exit 100;;
esac

# check for readability of spec_file argurment list

shift			# get rid of usr|root arg

for spec_file in $*
do
	if [ ! -r $spec_file ]
	then
		echo "mkspec: can not read '$spec_file'" 1>&2
		echo $usage 1>&2
		exit 101
	elif [ ! -f $spec_file ]
	then
		echo "$spec_file" not a regular file 1>&2
		echo $usage 1>&2
		exit 102
	fi

	if dircheck $spec_file 1>&2
	then
		exit 103
	fi
done

# generate a collated spec_file to the tmp file

for spec_file in $*
do
	# change all white space to single tabs
	cat $spec_file | sed -e 's: :	:g' -e 's:[	]	*:	:g' | \
	nawk ' BEGIN { prepend = "'`dirname $spec_file`'"; }
	{
		offset = 0;

		# handle leading ./ in pathnames
		if (substr($2, offset, 2) == "./")
			offset = 3;

		if (("'$fs'" == "root") && (substr($2, offset, 4) == "usr/"))
			continue;

		# if we are working on usr and this a 
		# usr entry, strip off "usr/" from target
		if ("'$fs'" == "usr") {
			if (substr($2, offset, 4) != "usr/")
				continue;
			else
				sub("usr/", "", $2);
		}

		# do not prepend current path to "." entries
		if ($1 == ".")
			printf ("%s\n", $0);
		else
			printf ("%s/%s\n", prepend, $0);
	}'
done | sort -b +1 -2 | uniq > $tmpfile

if dup_target_check $tmpfile 1>&2
then
	exit 104
fi

# generate normal output
cat $tmpfile
rm -f $tmpfile
exit 0			# success
