#! /bin/sh

# psdiff --- Print diff in a nice way

# Copyright (c) 1988, 89, 90, 91, 92, 93 Miguel Santana
# Copyright (c) 1995, 96, 97, 98 Akim Demaille, Miguel Santana

# $Id: psdiff.in,v 1.3 1998/04/10 16:18:43 demaille Exp $

# 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 2, 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, you can either send email to this
# program's maintainer or write to: The Free Software Foundation,
# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.

# Commentary:

# Author: Akim Demaille <demaille@inf.enst.fr>

# In the interest of general portability, some common bourne shell
# constructs were avoided because they weren't guaranteed to be available
# in some earlier implementations.  I've tried to make this program as
# portable as possible.  Welcome to unix, where the lowest common
# denominator is rapidly diminishing.
#
# Among the more interesting lossages I noticed with some bourne shells
# are:
#     * No shell functions.
#     * No `unset' builtin.
#     * `shift' cannot take a numeric argument, and signals an error if
#       there are no arguments to shift.

# Code:

# Minimal path.  It must be able to see wdiff and GNU diff
PATH=/usr/local/bin:$PATH
export PATH

# Get the name of the program
program=`echo $0 | sed 's#.*/##g'`

# Local vars
a2ps=${A2PS:-a2ps}
a2ps_options=
debug=
diff_on=words
diff_prog=${DIFF:-diff}
diff_options='-u'
file=
output=
tmpdir=/tmp/$program.$$
verbose=echo
wdiff_prog=${WDIFF:-wdiff}
wdiff_options='-w[wd- -x-wd] -y{wd+ -z+wd}'
# The version/usage strings
version="psdiff 0.2 (a2ps 4.10.2)

Copyright (c) 1988, 89, 90, 91, 92, 93 Miguel Santana
Copyright (c) 1995, 96, 97, 98 Akim Demaille, Miguel Santana
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Written by <Akim.Demaille@inf.enst.fr> and <Miguel.Santana@st.com>
News, updates and documentation: visit http://www.inf.enst.fr/a2ps/"

usage="\
Usage: $program FILE1 FILE2
Pretty print the differences between FILE1 and FILE2.
Options:
 -h, --help           display this help and exit
 -v, --version        display version information and exit
 -q, --quiet          don't print informational messages
 -l, --lines          search for line differences (\`diff')
 -w, --words          search for word differences (\`wdiff')

Unrecognized options are passed to a2ps.  Arguments cannot be
separated from the options.

Report bugs to <a2ps-bugs@inf.enst.fr>"

help="Try \`$program --help' for more information."

# Parse our command line options once, thoroughly.
while test $# -gt 0
do
  arg="$1"
  shift
  
  case "$arg" in
    -*=*) optarg=`echo "$arg" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
    *) optarg= ;;
  esac
  
  # If the previous option needs an argument, assign it.
  if test -n "$prevopt"; then
    optarg="$arg"
    arg="$prevopt="
    prevopt=
  fi
  
  # Have we seen a non-optional argument yet?
  case "$arg" in
    --help|-h)
      echo "$usage"
      exit 0
      ;;
    
    --version|-v)
      echo "$version"
      exit 0
      ;;
    
    -s|-q|--quiet|--silent) message=: ;;
    # Delay debugging so that options parsing does not appear
    -D|--debug) debug=: ;;
    
    --output|-o) prevopt="--output" ;;
    --output=*) 
      output=$optarg
      ;;
    
    -l|--lines) diff_on=lines;;
    -w|--words) diff_on=words;;

    -*)
      a2ps_options="$a2ps_options $arg"
      ;;
    *)
      nonopt="$nonopt $arg"
    ;;
  esac
done

if test -n "$prevopt"; then
  exec 1>&2
  echo "$program: option \`$prevopt' requires an argument"
  echo "$help"
  exit 1
fi

case `echo "$nonopt" | wc -w | sed -e 's/[\t ]//g'` in
  2) file1=`set -- $nonopt && echo $1`
     file2=`set -- $nonopt && echo $2`
     ;;

  0|1) exec 1>&2
       echo "$program: not enough arguments"
       echo "$help"
       exit 1
       ;;
  
  *)  exec 1>&2
      echo "$program: too many arguments"
      echo "$help"
      exit 1
      ;;
esac

# Set the titles
a2ps_options="--left-title=$file1 --right-title=$file2 $a2ps_options"
a2ps_options="--center-title $a2ps_options"

# Use the right prologue
a2ps_options="--prolog=diff $a2ps_options"

# Set -x now if debugging
test $debug && set -x

# Call the correct diffing program, and pipe into a2ps
case $diff_on in
  words) # Word differences
    $wdiff_prog $wdiff_options $file1 $file2	\
       | $a2ps -Ewdiff $a2ps_options || exit 1
    ;;

  lines) # Line differences
    # We need the total number of lines
    lines=`wc -l $file1 $file2 | sed -n 3p`
    lines=`set -- $lines && echo $1`
    $diff_prog $diff_options -$lines $file1 $file2	\
       | $a2ps -gEudiff $a2ps_options || exit 1
    ;;
esac

exit 0

