#! /bin/sh --
#
#	$NetBSD: checkflist,v 1.20 2003/08/12 04:20:08 lukem Exp $
#
# Verify output of makeflist against contents of $DESTDIR.

if [ -z "$DESTDIR" ]; then
	echo "DESTDIR must be set"
	exit 1
fi

prog=${0##*/}


# Make sure we don't loop forever if mkdir will always fail.
if [ ! -d /tmp ]; then
	echo /tmp is not a directory
	exit 1
fi

if [ ! -w /tmp ]; then
	echo /tmp is not writable
	exit 1
fi

SDIR_BASE=/tmp/checkflist.$$
SDIR_SERIAL=0

while true; do
	SDIR=${SDIR_BASE}.${SDIR_SERIAL}
	mkdir -m 0700 ${SDIR} && break
	SDIR_SERIAL=$((${SDIR_SERIAL} + 1))
done    

es=0
cleanup()
{
	/bin/rm -rf $SDIR
	if [ $es -gt 255 ] ; then
		es=255
	fi
	exit $es
}
trap cleanup 0 2 3 13		# EXIT INT QUIT PIPE


origin=.
xargs=""
dargs=""
metalog=

# handle args
while : ; do
	case $1 in
	-x11)
		xargs="-x"
		origin="./etc/X11 ./etc/fonts ./usr/X11R6"
		;;
	-both)
		xargs="-b"
		;;
	-M*)
		metalog=$2; shift
		;;
	-*)
		cat 1>&2 <<USAGE
Usage: ${prog} [-x11|-both] [-M metalog]
	-x11		check only x11 lists
	-both		check netbsd + x11 lists
	-M metalog	metalog file
USAGE
		exit 1
		;;
	*)
		break
		;;
	esac
	shift
done

if [ -n "$metalog" ]; then
	case "$metalog" in
	${DESTDIR}/*)
		# Metalog would be noticed, so make sure it gets
		# ignored.
		metalog="./${metalog#${DESTDIR}/}"
		;;
	*)
		metalog=""
	esac
fi


sh makeflist $xargs $dargs > $SDIR/flist

(
	cd $DESTDIR
	find $origin \( -type d -o -type f -o -type l \)
) | (
	while read line; do
		case "$line" in
		$metalog)
			;;
		*)
			echo $line
			;;
		esac
	done
) | sort > $SDIR/files

comm -23 $SDIR/flist $SDIR/files > $SDIR/missing
comm -13 $SDIR/flist $SDIR/files > $SDIR/extra

if [ -s $SDIR/extra ]; then
	echo ""
	echo "============  extra files  ==============="
	echo "Files in DESTDIR but missing from flist."
	echo "File is obsolete or flist is out of date ?"
	echo "This is non-fatal."
	echo "------------------------------------------"
	cat $SDIR/extra
	echo "=========  end of extra files  ==========="
	echo ""
fi

if [ -s $SDIR/missing ]; then
	echo ""
	echo "===========  missing files  =============="
	echo "Files in flist but missing from DESTDIR."
	echo "File wasn't installed ?"
	echo "------------------------------------------"
	cat $SDIR/missing
	echo "========  end of missing files  =========="
	echo ""
	es=1
fi

exit 0		# cleanup will exit with $es
