#! /bin/sh

#	Copyright (c) 1988 AT&T
#	  All Rights Reserved

#	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
#	The copyright notice above does not evidence any
#	actual or intended publication of such source code.

#ident	"@(#)sccsdiff.sh	6.1"
#ident	"@(#)sccs:cmd/sccsdiff.sh	6.3"
#	DESCRIPTION:
#		Execute diff(1) on two versions of a set of
#		SCCS files and optionally pipe through pr(1).
#		Optionally specify diff segmentation size.
# if running under control of the NSE (-q option given), will look for
# get in the directtory from which it was run (if -q, $0 will be full pathname)
#
PATH=$PATH:/usr/ccs/bin

trap "rm -f /tmp/get[abc]$$;exit 1" 0 1 2 3 15

if [ $# -lt 3 ]
then
	echo "Usage: sccsdiff -r<sid1> -r<sid2> [-p] [-q] sccsfile ..." 1>&2
	exit 1
fi

nseflag=
rflag=
addflags=
get=get
for i in $@
do
	if [ "$addflags" = "yes" ]
	then
		flags="$flags $i"
		addflags=
		continue
	fi
	case $i in

	-*)
		case $i in

		-r*)
			if [ ! "$sid1" ]
			then
				sid1=`echo $i | sed -e 's/^-r//'`
				if [ "$sid1" = "" ]
				then
					rflag=yes
				fi	
			elif [ ! "$sid2" ]
			then
				sid2=`echo $i | sed -e 's/^-r//'`
				if [ "$sid2" = "" ]
				then 
					rflag=yes
				fi	
			fi
			;;
		-s*)
			num=`echo $i | sed -e 's/^-s//'`
			;;
		-p*)
			pipe=yes
			;;
		-q*)
			nseflag=-q
			get=`dirname $0`/get
			;;
		-[cefnhbwit]*)
			flags="$flags $i"
			;;
		-[CD])
			flags="$flags $i"
			addflags=yes
			;;
		*)
			echo "$0: unknown argument: $i" 1>&2
			exit 1
			;;
		esac
		;;
	[1-9]*)
		if [ "$rflag" != "" ]
		then
			if [ ! "$sid1" ]
			then
				sid1=$i
			elif [ ! "$sid2" ]
			then
				sid2=$i	
			fi
		fi
		rflag=	
		;;	
	*s.*)
		files="$files $i"
		;;
	*)
		echo "$0: $i not an SCCS file" 1>&2
		;;
	esac
done

for i in $files
do
	if $get $nseflag -s -p -k -r$sid1 $i > /tmp/geta$$
	then
		if $get $nseflag -s -p -k -r$sid2 $i > /tmp/getb$$
		then
			diff $flags /tmp/geta$$ /tmp/getb$$ > /tmp/getc$$
		fi
	fi
	if [ ! -s /tmp/getc$$ ]
	then
		if [ -f /tmp/getc$$ ]
		then
			echo "$i: No differences" > /tmp/getc$$
		else
			exit 1
		fi
	fi
	if [ "$pipe" ]
	then
		pr -h "$i: $sid1 vs. $sid2" /tmp/getc$$
	else
		cat /tmp/getc$$
	fi
done

trap 0
rm -f /tmp/get[abc]$$
