#! /bin/sh
# -*- ksh -*-
# 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: pdiff.in,v 1.4 1998/08/05 15:04: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 (GNU a2ps 4.12)

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: http://www.inf.enst.fr/~demaille/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 command line arguments.
option_without_arguments='vhsqDlw'
while test $# -gt 0; do
  
  # Handle --option=value by splitting apart and putting back on argv.
  case "$1" in
    --*=*)
      opt=`echo "$1" | sed -e 's/=.*//'`
      val=`echo "$1" | sed -e 's/[^=]*=//'`
      shift
      set - "$opt" "$val" "$@"
      ;;

    -[$option_without_arguments]?*)
      opt=`echo "$1" | sed -e 's/-\\(.\\).*/-\1/'`
      rest=`echo "$1" | sed -e 's/-.\\(.*\\)/-\1/'`
      shift
      set - "$opt" "$rest" "$@"
      ;;

    # This case needs to be protected so that the case `-??*' does
    # not split long options without arguments
    --*)
      ;;

  esac
  
  # This recognizes --heltch as --help.  So what.
  case "$1" in
    --h*|-h)  echo "$usage";   exit 0  ;;
    --v*|-v)  echo "$version"; exit 0  ;;
    -s|-q|--q*|--s*) verbose=: ;;
    -D|--debug) debug=: ;;
    
    --output|-o) shift; output=$1 ;;    
    -l|--lines) diff_on=lines;;
    -w|--words) diff_on=words;;

    -*)
      a2ps_options="$a2ps_options $1"
      ;;
    *)
      nonopt="$nonopt $1"
    ;;
  esac
  shift
done

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

