#!/bin/bash


#######################
#
# purpose of svntexdiff is to upload revision n-1 (or a given revision)
# from the current svn server and produce a latex diff file from the
# current and that version for easier understanding of  differences
# as a latexd pdf file
#
#######################

if [ $# -ne 1 -a $# -ne 2 ]; then
 echo "Usage: $0  [revision]"
 exit 1
fi

#LATEXDIFFEXEC=/softs/latexdiff/1.1.1/bin/latexdiff
LATEXDIFFEXEC=latexdiff
FNAME=$1
REV=$2

# check this is svn file
if [ ! -f "./.svn/text-base/$FNAME.svn-base" ]; then
 echo "*** Error: ./.svn/text-base/$FNAME.svn-base file not found"
 exit 2
fi

# we want no modif in checkout local version
CK=$(svn status $FNAME | wc -l)
if [ $CK -ne 0 ]; then
  echo "*** Error: $FNAME locally modified."
  exit 3
fi

CURRENTREV=$(svn info $FNAME | grep '^Revision:' | cut -d' ' -f2)
if [ -n "$REV" ]; then
  PREVREV=$REV
else
  PREVREV=$(($CURRENTREV-1))
fi

svn cat -r $PREVREV $FNAME > previousrev
echo "Generating diff tex file for $FNAME..."
$LATEXDIFFEXEC previousrev $FNAME> diff.tex
echo "Generating diff pdf file for $FNAME..."
pdflatex -interaction=nonstopmode diff.tex >/dev/null
# twice since possible crossrefs
pdflatex -interaction=nonstopmode diff.tex >/dev/null 


# clean up!
#mv -f $FNAME.last $FNAME
rm -f previousrev diff.{tex,aux,log,out}

echo "File: diff.pdf generated: use evince diff.pdf"

exit 0