#! /bin/sh 

# mkpatch -- make patch file, for transforming xx.orig into xx
#
# $Id: mkpatch,v 1.3 1997/05/30 04:29:56 cdua Exp cdua $
# Carlos Duarte, 970514/970523

#trap -- 'rm -f /tmp/xpto' EXIT SIGINT

USAGE="\
usage: $0 [-hv] [-rm] dir

  -h      this help
  -v      be verbose (default)
  -v-     do not be verbose
  -rm     remove xx.orig, after making the patch 
  -p      print diffs to stdout

for all files xx.orig in dir, and subdirs, of DIR, make a patch
with 'diff xx.orig xx' and place that diff on current dir with
name xx.patch

example: $0 . 
"

V=yes 
while : ; do
	case x"$1" in 
	x-h)	echo "$USAGE"; exit 1 ;;
	x-v)	V=yes ;;
	x-v-)	V= ;;
	x-rm)	DORM=yes ;;
	x-p)	TO_STDOUT=yes ;;
	x--)	shift; break ;;
	x-?)	echo "invalid option -- `echo $1|cut -c2-`"
		echo "$USAGE"; exit 1 ;;
	*)	break ;;
	esac
	shift
done

if test $# -eq 0; then echo "$USAGE"; exit 1; fi

dir=$1
find $dir -type f -name \*.orig -print | while read orig; do 
	
	test -f $orig || continue
	new=`expr $orig : '\(.*\)\.orig$'`
	test x$V != x && echo "Patching $new..." 1>&2 
	if cmp $orig $new >/dev/null; then 
		test x$V != x && echo "  nothing to do..." 1>&2 
	else
		TARGET=`basename $new`
		VERSION=
		if test -f $TARGET.patch; then
			VERSION=2
			while test -f $TARGET.$VERSION.patch; do
				VERSION=`expr $VERSION + 1`
			done
			VERSION=.$VERSION
		fi
		test x$V != x && echo "  making diffs..." 1>&2 
		if test x$TO_STDOUT = xyes; then 
			diff -u $orig $new 
		else
			diff -u $orig $new > $TARGET$VERSION.patch
		fi
	fi

	if test x$DORM != x; then
		test x$V != x && echo "  deleting $orig..." 1>&2 
		rm $orig
	fi
done
exit
