#! /bin/sh -
#
# $Id: runtests,v 1.2 2003/09/07 17:45:57 norman Exp $
#
# Run a series of tests on LaTeX files.  The directory contains a collection
# of documents named `t<number>.tex', and these are processed in turn and their
# output diffed against `t<number>.correct.dvi'.  Exit status is the
# number of failures.
#
# Run without an argument, and if there is no file runtests.filelist,
# this runs all the tests in the directory; run with an argument, it
# runs only the specified tests.
#
# If there is no argument, but a file runtests.filelist exists, use
# that file's contents as the list of tests to run.  This means that
# we can make it easy to do a subset of the tests repeatedly while
# testing.
#
# The only we we really have of testing whether the binary DVI files
# differ is by comparing them using cmp, which simply reports the
# byte-offset where they differ.  This isn't terribly helpful, and
# even examining the files using dv2dt doesn't help much.  It's also
# probably rather sensitive to things like the version of hyperref
# which is being used.  Also, since TeX puts a timestamp in DVI files, 
# we have to ignore the first 50 bytes or so (see a thread with the
# subject `diff for dvi files', in comp.text.tex, in May 2001.
#
# If the option --keep is present, then it doesn't delete temporary files.

UsageString="$0 [--keep] [filename...]"
deletetemp=true
LS=/bin/ls


filelist=""

while [ $# -gt 0 ]; do
    case $1 in
    --keep) deletetemp=false ;;
    --*) echo "Usage: $UsageString"
	 exit 1
	 ;;
    *) filelist="$filelist $1"
	 ;;
    esac
    shift
done

# If filelist is null, and a file runtests.filelist exists, use that
# file's contents as the value of filelist.
if [ -z "$filelist" -a -f runtests.filelist ]; then
    echo "Reading filelist from runtests.filelist"
    filelist=`cat runtests.filelist`
fi

# Check filelist is non-null, and make it t* if it is.
if [ -z "$filelist" ]; then
    filelist=`$LS | grep '^t[0-9]*\.tex$' | sed 's/\.tex//'`
fi



nfailures=0

for name in $filelist
do
	echo -n "$name... "
	# Make sure we run twice, if there's no preexisting aux file.
	test -f $name.aux || latex $name.tex >$name.stdout 2>$name.stderr
	latex $name.tex >$name.stdout 2>$name.stderr
	testval=$?
	#test -f $name.dvi && mv $name.dvi $name.dvi.tmp

	if [ $testval != 0 ]; then
	    echo "failed (exited with error status $testval)"
	    nfailures=`expr $nfailures + 1`
	    $deletetemp && rm -f $name.stdout $name.stderr $name.dvi $name*.tmp
	elif [ -r "$name.correct.dvi" ]; then
	    cmp -i50 $name.dvi $name.correct.dvi >$name.diff
	    rval=$?
	    if [ $rval != 0 ]; then
		echo "failed (results in $name.diff)"
		nfailures=`expr $nfailures + 1`
	    else
		echo "ok"
		$deletetemp && rm -f $name.diff $name.stdout $name.stderr $name.dvi $name*.tmp
	    fi
	else
		echo "apparently OK, but no correct results to compare"
		mv $name.dvi $name.correct.dvi
		echo "    (now in $name.correct.dvi)"
		$deletetemp && rm -f $name*.tmp
	fi
done

echo "$nfailures failed tests"

exit $nfailures
