#!@SH@
#
# $NetBSD: pkgdiff,v 1.17 2009/12/10 20:26:33 abs Exp $
#
# Usage: pkgdiff newfile
#        pkgdiff oldfile newfile
#
# Will output a patch ready for the NetBSD Pkgs Collection (unified
# diff, plus no RCS IDs if possible). If only newfile is given,
# oldfile is assumed as newfile.orig.
#
# Copyright (c) 2000 by Hubert Feyrer <hubertf@netbsd.org>
# All Rights Reserved.  Absolutely no warranty.  
#

# Ensure we always use the same timezone to avoid spurious metadata diffs
export TZ=UTC

if [ $# -le 1 ]
then
	if [ -f "$1.orig" ]; then 
		old="$1.orig"
		new="$1"
	else
		echo $0: need at least one argument >&2
		exit 1;
	fi
else
	old="$1"
	new="$2"
fi
basename_new="`basename $new`"

dodiff() {
	case x"$basename_new" in
		xconfigure)
		@DIFF@ -I '\(echo .*as_me:[0-9][0-9]*:\|echo .*configure:[0-9][0-9]*:\|line [0-9][0-9]* "configure\)' $*
		;;
		*)
		@DIFF@ $*
	esac
}

case x"$PKGDIFF_FMT" in x)
lines=3
PKGDIFF_FMT="-p"
while [ `dodiff "$PKGDIFF_FMT" -U $lines "$old" "$new" | egrep -c '\\$(NetBSD|Author|Date|Header|Id|Locker|Log|Name|RCSfile|Revision|Source|State)(:.*)?\\$'`  !=  0 ]
do
	lines=`expr $lines - 1`
	if [ $lines = 0 ]; then
		echo "Cannot strip away RCS IDs, please handle manually!" >&2
		exit 1
	fi
done
PKGDIFF_FMT="${PKGDIFF_FMT} -U $lines"
;;
esac # PKGDIFF_FMT unset or null

if dodiff -q "$PKGDIFF_FMT" "$old" "$new" > /dev/null
then
	:
else
    echo '$'NetBSD'$'
    echo ''
    # Strip out the date on the +++ line to reduce needless
    # differences in regenerated patches
    dodiff "$PKGDIFF_FMT" "$old" "$new" | sed -e 's:^\(+++ [^	]*\)	.*:\1:'
fi
