#!/bin/csh -f
#
# Make a diff file for a patch.
# Syntax: make_patch old_directory new_directory outfile
# (prompted for if omitted)
#
# We will attempt to construct a Prereq: PREREQ from a string of the form
# static char version[] = ... version PREREQ ...
# in file old_directory/AA_Version.h
#
# After the file is made, you'll be asked which files that only appear
# in the new directory you'd like created in the new, and these are added
# to the end of the diff file
#
if($#argv < 1) then
	echo "Old directory?" ; set argv=$<
endif
if($#argv < 2) then
	echo "New directory?" ; set argv=( $argv $< )
endif
echo Don\'t forget to update the patch level after this\!
if($#argv < 3) then
	echo "Diff file to produce?" ; set argv=( $argv $< )
endif
if(-e $3) then
	echo "Diff file already exists -- continue? [n/y]"
	set cont=$<
	if($cont !~ y) exit
endif
set prereq=`grep "static char version_string\[\] = " $1/AA_Version.h | sed -e "s/.*version \([^ ]*\).*/\1/"`
if($prereq == "") then
	echo "No Prereq found: enter one (or nothing if none is desired)"
	set prereq=$<
else
	echo Prerequisite = $prereq
endif
if($prereq != "") then
	echo Prereq: $prereq > $3
else
	cp /dev/null $3
endif
#
# Run diff to make the raw patch file, remove references to files that
# I don't care about (and don't want created at the bottom of the file),
# then use awk to remove the diffs to files such as index.tex. I couldn't
# figure out how to do this with sed, at least not without lots of grief
#
diff -c -r $1 $2 | \
	    sed -e '/^Only in .*\.o/d' \
	        -e '/^Only in .*\.a/d' \
	        -e '/^Only in .*\.rej/d' \
	        -e '/^Only in .*\.orig/d' \
	        -e '/^Only in .*\.dvi/d' \
	        -e '/^Only in .*\.log/d' \
	        -e '/^Only in .*~/d' \
		-e '/^Binary files .*differ$/d' \
		-e '/Common subdirectories: /d' | \
	awk 	'BEGIN		{pr=1} \
		/^diff -c -r/ { \
			if($5 == "sm/doc/index.tex") { \
			   pr=0; \
			} else { \
			   pr=1; \
			} \
		} \
		{ \
			if(pr) print; \
		}' >> $3
#
# See if any files must be created
#
foreach f (`sed -n -e "/^Only in/s|^Only in \([^:]*\): \(.*\)|\1/\2|p" $3`)
	if($f =~ *sm/compile_g	|| $f =~ *sm/fonts.bin || \
	   $f =~ *sm/get_grammar	|| $f =~ *sm/read_fonts || \
	   $f =~ *sm/patch		|| $f =~ *sm/rasterise || \
	   $f =~ *sm/src/TAGS	|| $f =~ *sm/src/control.c || \
	   $f =~ *sm/src/control.h  || $f =~ *sm/patch/patch || \
	   $f =~ *sm/src/makeyyl	|| $f =~ *sm/src/sm || \
	   $f =~ *sm/src/yylex.c	|| $f =~ *sm/src/bison/bison || \
	   $f =~ *sm/doc/index	|| $f =~ *sm/doc/index1.tex || \
	   $f =~ *sm/doc/contents.tex || $f =~ *sm/doc/contents1.tex || \
	   $f =~ *sm/.sm*ist* || $f =~ *sm/.hostname) \
							continue
	echo "Create file $f? [n/y]"
	set yn=$<
	if($yn =~ y*) then
		diff -c /dev/null $f >> $3
	endif
end
