:
#######################################################
# fcdiff: show the last two changes in an fchange log 
# file.  changes begin with the #_change tag.

test "$debugxv" && set -xv # primitive debugger

fchff=fchange.files
logdir=logs

test -f $fchff || {
    echo "Error: could not find $fchff"
    echo "you must be in fchangedir"
    exit 1
}

#######################################################
#                      syntax
#######################################################

syntax()
{
  echo "\
Usage: `basename $0` [-n log number] [-f filename]"
  exit 1
}
# syntax()

#######################################################

test $# = 0 && syntax

# process the command-line
set -- `getopt n:f: $*` || syntax
 
unset lognum filename
while [ $# -gt 0 ]
do
  case "$1" in
    -n) lognum="$2" ; shift ;;
    -f) filename="$2" ; shift ;;
    --) shift ; break ;;
     *) echo "illegal option: $1"; exit -1 ;;
  esac
  shift
done

test "$lognum" || test "$filename" || {
    echo "Error: must specify number or filename"
    exit 1
}

unset trackn
test "$lognum" && trackn=${logdir}/${lognum}.log.track || {

    grep "$filename" $fchff
    exit 0
}

test -f $trackn || {
    echo "Error: couldn't find: $trackn"
    exit 1
}

filea=/tmp/junk1 ; > $filea
fileb=/tmp/junk2 ; > $fileb

lines=`awk '/^#_change/ {print NR}' $trackn | tail -2`

# $lines will equal something like "45 100".  the for x 
# that follows simply sets a=45 and b=100.

for x in $lines
do
    test "$a" && b=$x || a=$x
done

echo "start=$a end=$b"

# print out the first section, which starts at the first 
# line number and ends at before the second line number.

awk '{ if (NR >= linea && NR < lineb ) { print }
}' linea=$a lineb=$b $trackn > $filea

# print out the last entry, which begins at the second 
# line number.
awk '{ if (NR >= lineb) { print } }' lineb=$b $trackn \
  > $fileb

# show what has changed
diff $filea $fileb

rm -f $filea $fileb
exit 0


