#!/bin/ksh

WIDTH=170
VI_WIDTH=175

if [ $# = 0 ]
then
	echo "usage: $0 [ -a ] [ -ps ] [ -vi ] [ newfile oldfile ] [ newfile oldfile ]"
	exit 0
fi

function do_ps
{
	ASC=$1
	PS=$2
	enscript -fCourier7 -FCourier-Bold7 -r -p - $ASC |
awk '{
	if ( substr($2,index($2,"(")+1,1) == ">")
	{
		print "1 F"
		print $0
		print "0 F"
	}
	else
	if ( match($0,"<  \\)B$"))
	{
		print "1 F"
		print $0
		print "0 F"
	}
	else
	if ( match($0,"  \\|  "))
	{
		print "1 F"
		print $0
		print "0 F"
	}
	else
	{
		print $0
	}
}' > $PS
}

function dif
{
	FILE1=/tmp/dif1$$
	FILE2=/tmp/dif2$$
	sed -e '/^#pragma/d' < $1 | expand -4 > $FILE1
	sed -e '/^#pragma/d' < $2 | expand -4 > $FILE2
	FILE3=$3
	
	sdiff -w $WIDTH $FILE1 $FILE2 | awk '{
	++i
	if (i >= 10000)
		s=""
	else if (i >= 1000)
		s=" "
	else if (i >= 100)
		s="  "
	else if (i >= 10)
		s="   "
	else
		s="    "
	print s i " " $0
 }' > $FILE3
 rm $FILE1 $FILE2
}

BASEPJ=${BASEPJ:=/$CMVC_RELEASE}
CMVC_PDDIR=${CMVC_PDDIR:=.}
DO_PS=0
DO_VI=0

while true
do
    case $1 in
	"")
		exit 0;;
    -a)
		# Get list of checked out files
		FILES=`fo|awk '{ print $3 }'`
		echo "Diffing all checked out files ..."
		for i in $FILES
		do
			ARGS=$ARGS" "$CMVC_PDDIR/$i" "$BASEPJ/$i
		done
		shift
		$0 $* $ARGS
		exit $?;;

	-ps)
		DO_PS=1
		echo "Using PostScript ..."
		shift;;

	-vi)
		DO_VI=1
		echo "Running vi after diffing ..."
		shift;;

    *)
		FILE1=$1
		shift
		FILE2=$1
		shift
		FILE3=`basename $FILE2`.asc
		dif $FILE1 $FILE2 $FILE3

		if [ $DO_VI = 1 ]
		then
                        echo "Search string => /    [|<>]"
			xterm -T "vi $FILE3" -fn courr8 -geometry "$VI_WIDTH"x40+0+0 -e vi -c"/    [|<>]" +1 $FILE3
		fi

		if [ $DO_PS = 1 ]
		then
			PS=`basename $FILE2`.ps
			do_ps $FILE3 $PS
			rm -f $FILE3
		fi

		continue;;

	esac
	ARGS=$ARGS" "$1
done


