#! /bin/sh --
#
#	$NetBSD: checkflist,v 1.19 2003/07/10 03:19:15 lukem Exp $
#
# Verify output of makeflist against contents of $DESTDIR.

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

prog=${0##*/}

origin=.
tmpname=/tmp/_CHECK.$$

xargs=""
dargs=""
diffargs=""
metalog=

# handle args
while : ; do
	case $1 in
	-x11)
		xargs="-x"
		origin="./etc/X11 ./etc/fonts ./usr/X11R6"
		;;
	-both)
		xargs="-b"
		;;
	-u)
		diffargs="-u"
		;;
	-c)
		diffargs="-c"
		;;
	-M*)
		metalog=$2; shift
		;;
	-*)
		cat 1>&2 <<USAGE
Usage: ${prog} [-x11|-both] [-u|-c] [-M metalog]
	-x11		check only x11 lists
	-both		check netbsd + x11 lists
	-u		output differences in "unified diff" style
	-c		output differences in "context diff" style
	-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 > $tmpname

(
	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 | diff $diffargs $tmpname -
rv=$?

/bin/rm -f $tmpname

if [ $rv -ne 0 ]; then
	echo "${prog}: flist inconsistencies found"
	if [ -z "$diffargs" ]; then
		echo "${prog}: key to output:"
		echo "  <  file is in flist but missing from DESTDIR"
		echo "     (file wasn't installed ?)"
		echo "  >  file is in DESTDIR but missing from flist"
		echo "     (file is obsolete or flist is out of date ?)"
	fi
fi
exit $rv
