#! /bin/sh
# sccsdiff.sh.in: Part of GNU CSSC.
#
# Copyright (C) 1997, 1998, 2001, 2004, 2007, 2019 Free Software
# Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# This file is part of CSSC.
# # src/sccsdiff.sh.  Generated from sccsdiff.sh.in by configure.

# Usage:
# sccsdiff [-p] -rsid -rsid [diff-options] s.filename
#

# $Id: sccsdiff.sh.in,v 1.12 2007/12/17 21:59:50 jay Exp $
# LOG   $Log: sccsdiff.sh.in,v $
# LOG   Revision 1.12  2007/12/17 21:59:50  jay
# LOG   Migrate to GPL version 3
# LOG
# LOG   Revision 1.11  2007/06/19 22:41:15  james_youngman
# LOG   Included copyright notices in some files where these were missing
# LOG
# LOG   Revision 1.10  2004/10/17 11:45:13  james_youngman
# LOG   Fixed SourceForge bug 966010, sccs sccsdiff -r cannot have space
# LOG   before revision number.   We still will have similar problems
# LOG   elsewhere in the suite, I suspect.
# LOG
# LOG   Revision 1.9  2001/11/25 16:01:38  james_youngman
# LOG   Corrected a syntax error.
# LOG
# LOG   Revision 1.8  2001/11/25 14:27:54  james_youngman
# LOG   fixed Debian  Bug#120080: sccs sccsdiff doesn't work (sccsdiff assumes /usr/sccs symlink exists)
# LOG
# LOG   Revision 1.7  1998/03/10 00:22:00  james
# LOG   Support for filenames containing spaces.
# LOG
# LOG   Revision 1.6  1998/03/09 23:25:00  james
# LOG   Bug report from Richard Polton: IRIX's pr(1) requires a space between
# LOG   the "-h" and its argument.
# LOG
# LOG   Revision 1.5  1998/02/09 21:00:19  james
# LOG   Patch from Maurice O'Donnell <mod@tfn.com>: pass "-e" to sed before
# LOG   the sed commands.
# LOG
# LOG   Revision 1.4  1998/01/24 14:15:13  james
# LOG   SCCS compatibility fix -- When "get" fails, exit with value 1, not 2.
# LOG
# LOG   Revision 1.3  1997/12/19 20:40:35  james
# LOG   Modified version of sccsdiff arrived from Richard Polton; took the
# LOG   ideas onboard for a rewrite of sccsdiff.
# LOG
# LOG   Revision 1.2  1997/05/04 14:52:02  james
# LOG   Have to enclose the version string in single rather than double
# LOG   quotes, to protect the dollar signs from shell expansion.
# LOG
# LOG   Revision 1.1  1997/05/04 14:49:22  james
# LOG   Initial revision

# Values substituted by configure:-
pr=/usr/bin/pr			   # The location of the   pr(1) command
diff=/usr/bin/diff # The location of the diff(1) command

# Find the "get" command.  The last value tested is "get", and
# that's what we use if we can't find get in any other location.
if test x$get = x
then
    # The CSSC Makefile performs a sed substitution on the next line.
    for get in "/opt/local/libexec/cssc/get" /usr/sccs/get get
    do
	if [ -x "$get" ]
	then
	    break
	fi
    done
fi

version='$Revision: 1.12 $ $State: Exp $'
usage() {
cat <<EOF
$0 [-p] -rsid -rsid [diff-options] s.filename [s.secondfile...]
EOF
}

use_pr=false
first_sid=
second_sid=
diff_options=
sccs_files=

while test $# -gt 0
do
	case "$1" in
	    --help)
		usage
		exit 0
		;;
	    --version)
		echo "$version"
		exit 0
		;;
	    -p) use_pr=true
		shift
		;;
	    -r*)
		# Cope with "-r 1.1" or "-r1.1":
                if test x-r = x"$1"
		then
		    # SID is next argument.
		    arg=-r"$2"
		    shift;
		else
		    # SID imediately follow the "-r", in the same argument.
		    arg="$1"
		fi

		if test x"$first_sid" = x
		then
		    first_sid="$arg"
		else
		    if test x"$second_sid" = x
		    then
			second_sid="$arg"
		    else
			exec >&2
			usage
			echo "Too many -r options."
			exit 1
		    fi
		fi
		shift
		;;
	    -*)
		diff_options="${diff_options} $1"
		shift
		;;
	    *)
		# That is an SCCS file, leave it and everything
		# following it in $*.
		break
		;;
	esac
done

# Show what we got...
## echo "use_pr=" $use_pr
## echo "first SID=" $first_sid
## echo "second SID=" $second_sid
## echo "diff options=" $diff_options
## echo "SCCS files=" $*
## echo \$\# = $#

if test x$second_sid = x
then
    exec >&2
    echo Two SIDs must be specified with the -r option.
    usage
    exit 1
fi


# Remove the leading "-r" from $first_sid and $second_sid
# so that we can echo the correct string as the header line
# introducing the diff.
first_sid=`echo   $first_sid | sed -e 's/^-r//' `
second_sid=`echo $second_sid | sed -e 's/^-r//' `

getprefix=/tmp/get.$$.
g1="${getprefix}${first_sid}"
g2="${getprefix}${second_sid}"
dfile=${getprefix}d${first_sid}${second_sid}

while test $# -gt 0
do
    rm -f "$g1" "$g2" "$dfile"
    sccsfile="$1"
    shift
    base="`basename $sccsfile | sed -e 's/^s\.//'`"
    pr_header="$sccsfile: $first_sid vs. $second_sid"
    no_pr_header="
------- ${base} -------"

    ${get:-get} -r$first_sid -s -k "-G${g1}" "${sccsfile}" || {
	exec >&2
	echo "Failed to get version $first_sid from $sccsfile"
	exit 1
    }
    ${get:-get} -r$second_sid -s -k "-G${g2}" "${sccsfile}" || {
	rm -f "$g1"
	exec >&2
	echo "Failed to get version $second_sid from $sccsfile"
	exit 1
    }
    # in case noclobber is set...
    rm -f "$dfile"

    # Ignore return value of diff since it just tells us
    # if the two files are different.
    "$diff" $diff_options "$g1" "$g2" > "$dfile"
    if test -f "$dfile"
    then				  # Output file exists.
        if test -s "$dfile"
        then				  # Output file is not empty.
	    if $use_pr
	    then
		$pr -h "$pr_header"        < $dfile
	    else
		echo "$no_pr_header"
		cat "$dfile"
	    fi
        else				  # Output file is empty.
	    echo No differences.
	fi
    else				  # No output file at all!
        echo "$diff produced no output file." >&2
        exit 2
    fi
done					  # Loop over all named SCCS files.
rm -f "$g1" "$g2" "$dfile"
exit 0

# Local Variables:
# Mode: shell
# End:
