#!/bin/sh
# $Id: manifest,v 1.23 2017/08/12 12:42:54 tom Exp $
#
# Construct a manifest from the RCS description lines for the selected
# (symbolic) version.  The script assumes that the following files are
# automatically generated from scripts:
#
#	MANIFEST - from this script
#	configure - from autoconf (configure.in, aclocal.m4)
#	config.h - from configure
#
# Other files are all managed via RCS, and are present in a release only if
# they have had symbolic revisions added for that release.
#
unset LC_ALL
DIRS=no
ECHO=
LINE=yes
if [ $# = 0 ]
then
	echo '? this requires a revision name (-r option)'
	exit 1
fi

while test $# != 0
do
	case $1 in
	-d)
		DIRS=yes
		;;
	-p)
		DIRS=no
		LINE=no
		;;
	-r*)
		REV="`echo $1 | sed -e 's/^-r//'`"
		;;
	-v)	ECHO=echo
		;;
	-*)
		echo '? expected an "-r" option'
		exit 1
		;;
	*)
		break;
		;;
	esac
	shift
done

if test -z "$REV" ; then
	echo '? expected an "-r" option'
	exit 1
fi

# The remaining arguments are the names of files that we don't lookup in RCS
# (i.e., automatically-generated files that are useful in a distribution).

ALL=/tmp/find$$
ANY=/tmp/keep$$
TMP=/tmp/list$$
WD=`pwd`
trap "rm -f $ALL $ANY $TMP; exit" 0 1 2 3 15

echo >$ANY

test "$#" != 0 && shift
while [ $# != 0 ]
do
	if [ -f $1 ]
	then
		echo $1 >>$ANY
	fi
	shift
done

##############################################################################
#
# Construct a list of the files that are present in the given release.  This
# would be faster, but the rcs programs don't have an easy way to find if a
# file has a specific version.  We do this by looking for the version in the
# list of symbolic revisions.
#
find . -type f -print | sed -e s@./@@ |csort >$ALL
fgrep -v / $ALL >$TMP
fgrep    / $ALL >>$TMP
mv $TMP $ALL

echo Generating MANIFEST for `basename $WD`, version $REV

if [ -f MANIFEST ]
then
	rm -f MANIFEST.bak
	mv MANIFEST MANIFEST.bak
fi

if test $LINE = yes
then
cat >MANIFEST <<EOF
MANIFEST for `basename $WD`, version $REV
--------------------------------------------------------------------------------
EOF
fi

TAB=32
echo "MANIFEST	this file" | expand -t $TAB >>MANIFEST

HAD_CFG=no
last=""
odir="."
while true
do
	TAB=32
	read name
	if [ -z "$name" -o "$name" = "$last" ]
	then
		break
	fi
	temp=`(rlog -h $name \
		| sed -e '1,/symbolic names:/d' \
		| sed -e '/^[^ 	]/,$d' \
		| fgrep ${REV}:) 2>/dev/null`
	if [ -z "$temp" ]
	then
		if  ( egrep '^'$name'$' $ANY >/dev/null 2>&1 )
		then
			echo "auto:$name"
			echo $name >>MANIFEST
		elif [ -f configure.in -a $name = config.guess ]
		then
			echo "$name	configure utility-script" | expand -t $TAB >>MANIFEST
		elif [ -f configure.in -a $name = config.sub ]
		then
			echo "$name	configure utility-script" | expand -t $TAB >>MANIFEST
		else
			[ -n "$ECHO" ] && echo "skip $name"
			last=$name
		fi
		continue
	fi
	CHR=`echo "$name" | wc -c | sed -e 's/[ 	]//g'`
	if test -n "$CHR" && test $CHR -gt $TAB
	then
		TAB=`expr $CHR + 1`
	fi
	[ -n "$ECHO" ] && echo "...>>$name"
	leaf=`basename $name`
	ndir=`dirname $name`
	if [ "$name" = configure ]
	then
		HAD_CFG=yes
	fi
	if [ "$ndir" != "$odir" ]
	then
		if [ $DIRS = yes ]
		then
			echo "$ndir	subdirectory" | expand -t $TAB >>MANIFEST
			odir=$ndir
		fi
	fi
	if [ "$leaf" = "config.h" -a -f configure -a -z "`rlog -t $name`" ]
	then
		[ -n "$ECHO" ] && echo '...ignored config.h (generated)'
	elif [ "$name" != "MANIFEST" ]
	then
		text=`rlog -t $name | sed -e '1,/description:/d' | head -1`
		echo "$name	$text" | expand -t $TAB >>MANIFEST
	fi
	if [ "$name" = "configure.in" -a -f configure -a $HAD_CFG = no ]
	then
		# Postpone til after the files that generate 'configure' are
		# listed, so we can use this as an input list for 'shar'.
		echo "configure	Configuration script for UNIX" | expand -t $TAB >>MANIFEST
	fi
	last=$name
done < $ALL
