#!/bin/sh

# Derived from gtkdiff-rcs, modified by Ahmad Baitalmal <ahmadb@bitbuilder.com>

progname=$0
version=1.1.0
usage="\

Usage: $progname file
This is a modified version of gtkdiff-rcs by 
	Ahmad Baitalmal <ahmadb@bitbuilder.com>
	
A simple shell script that launches gtkdiff for use with CVS.
  -r N, --revision N         specifies revision numbers.
                             space between -r and N can be omitted.
                             The meaning follows rcsdiff(1).
  -3, --diff3                Use diff3(1).						 
  -h, --help                 print this help and exit
  -v, --version              print version information and exit

Example:
	\$ gtkdiff-cvs GnewsAlert.c
	( diff with latest on CVS repository ) 

	\$ gtkdiff-cvs -r1.1 GnewsAlert.c
	( diff with revision 1.1 on CVS repository )

	\$ gtkdiff-cvs -r1.1 -r1.2 GnewsAlert.c
	( diff between revisions 1.1 and 1.2 on CVS repository )

	\$ gtkdiff-cvs -3 -r1.1 GnewsAlert.c
	( diff3 among current, latest and revision 1.1 on CVS repository )

	\$ gtkdiff-cvs -3 -r1.1 -r1.2 GnewsAlert.c
	( diff3 among current, and revisions 1.1 and 1.2 on CVS repository )
	
	\$ gtkdiff-cvs -r1.1 -r1.2 -r1.3 GnewsAlert.c
	( diff3 among revisions 1.1, 1.2 and 1.3 on CVS repository )
"

# Command path
CVS=/usr/bin/cvs
RM=/bin/rm
GTKDIFF=/usr/X11R6/bin/gtkdiff

# Temporary files information
: ${TMPDIR=/tmp}
TMP_SUFFIX=@gtkdiff$$
TMP_REV1_FILE=
TMP_REV2_FILE=
TMP_REV3_FILE=

# variables
rev1=
rev2=
rev3=
use_diff3=0

rm_tmpfiles () {
	[ -n "$TMP_REV1_FILE" -a -f $TMP_REV1_FILE ] && $RM -f $TMP_REV1_FILE
	[ -n "$TMP_REV2_FILE" -a -f $TMP_REV2_FILE ] && $RM -f $TMP_REV2_FILE
	[ -n "$TMP_REV3_FILE" -a -f $TMP_REV3_FILE ] && $RM -f $TMP_REV3_FILE
}

finalize () {
	rm_tmpfiles
	exit 1
}
trap finalize INT QUIT HUP TERM

#
# Option processing
#
while [ "$1" != "" ]
do
	case "$1" in
	-r | --revision)
		shift
		test $# -eq 0 && { echo "$usage"; exit 0; }
		[ -z "$rev1" ] && { rev1="$1"; shift; continue; }
		[ -z "$rev2" ] && { rev2="$1"; shift; continue; }
		[ -z "$rev3" ] && rev3="$1";;
	-r*)
		[ -z "$rev1" ] && { rev1=`expr $1 : '-r\(.*\)'`; shift; continue; }
		[ -z "$rev2" ] && { rev2=`expr $1 : '-r\(.*\)'`; shift; continue; }
		[ -z "$rev3" ] && rev3=`expr $1 : '-r\(.*\)'`;;
	-3 | --diff3)
		use_diff3=1;;
	-h | --help) 
		echo "$usage"; exit 0;;
	-v | --version | --v*)
		echo "$progname $version"; exit 0;;
	--) # Stop option processing.
		shift; break ;;
	*)
		break ;;
	esac
	shift
done

if [ -z "$1" ]
then
	echo "$usage"
	exit 1
fi

#
# Check necessary files
#
if [ ! -x "$GTKDIFF" ]
then
	echo "Warning:"
	echo "You can't execute $GTKDIFF."
	echo "Are you sure you have GtkDiff installed ?"
	exit 0
fi

if [ ! -x "$CVS" ]
then
	echo "Warning:"
	echo "You can't execute $CVS."
	echo "Are you sure you have CVS installed ?"
	exit 0
fi

if [ ! -f "$1" ]
then
	echo "Warning:"
	echo "$1 not found"
	exit 0
fi

#
# Create temporary revision files
#
TMP_REV1_FILE=$TMPDIR/`basename ${1}`"("${rev1}")"${TMP_SUFFIX}
TMP_REV2_FILE=$TMPDIR/`basename ${1}`"("${rev2}")"${TMP_SUFFIX}
TMP_REV3_FILE=$TMPDIR/`basename ${1}`"("${rev3}")"${TMP_SUFFIX}

if [ -n "$rev1" ]
then
	$CVS -z3 -Q update -r$rev1 -p $1 > $TMP_REV1_FILE
else
	TMP_REV1_FILE=$TMPDIR/`basename ${1}`"("latest")"${TMP_SUFFIX}
	$CVS -z3 -Q update -p $1 > $TMP_REV1_FILE
	$GTKDIFF $TMP_REV1_FILE $1
	rm_tmpfiles
	exit 0
fi

if [ -n "$rev2" ]
then
	$CVS -z3 -Q update -r$rev2 -p $1 > $TMP_REV2_FILE
else
	if [ $use_diff3 -eq 0 ]; then
		$GTKDIFF $TMP_REV1_FILE $1
		rm_tmpfiles
		exit 0
   	else
		TMP_REV2_FILE=$TMPDIR/`basename ${1}`"("latest")"${TMP_SUFFIX}
		$CVS -z3 -Q update -p $1 > $TMP_REV2_FILE
		$GTKDIFF $TMP_REV1_FILE $TMP_REV2_FILE $1
		rm_tmpfiles
		exit 0
	fi
fi

if [ -n "$rev3" ]
then
	$CVS -z3 -Q update -r$rev3 -p $1 > $TMP_REV3_FILE
	$GTKDIFF $TMP_REV1_FILE $TMP_REV2_FILE $TMP_REV3_FILE
else
	if [ $use_diff3 -eq 0 ]; then
		$GTKDIFF $TMP_REV1_FILE $TMP_REV2_FILE
	else
		$GTKDIFF $TMP_REV1_FILE $TMP_REV2_FILE $1
	fi
fi

rm_tmpfiles
exit 0


