#!/bin/sh

case $LOGNAME in
rwscott)
	DELAY=0
	POSITION="+500+500"
	SAY='say'
	SAY='echo'
	FINISHED_CMD='say "I am dun now"'
	USE_FB='yes'
	;;
*)
	DELAY=31
	POSITION="+75+75"
	SAY='echo'
	;;
esac

if [ $# = 0 ]
then
    echo "usage: testall dirlist"
    exit
fi
if [ "$USE_FB" = "yes" ]
then
    nice Xvfb :1 -dpi 75 -fp "tcp/localhost:7000" 2>/dev/null &
    XVFB=$!
    sleep 5
    if kill -0 $XVFB 2>/dev/null
    then
	export DISPLAY=:1
	FINISHED_CMD=''
    else
	echo "Could not start Xvfb"
    fi
fi

echo "" >failed
TOTAL=0
FAILED=0
rm -f index.html
PREV=""
for i in $*
do
	if [ -d $i -a -f $i/Makefile ]
	then
                echo "Running tests in $i ================="
		$SAY $i >/dev/null
		rm -f $i/*.gif $i/*.html
		( cd $i ; make )
		for j in $i/test*[0-9]
		do
			TOTAL=`expr $TOTAL + 1`
			echo "<H1>$j</H1><HR><PRE>" >$j.html
			$j -xrm "*dumpFile: $j.gif" -xrm "*geometry: $POSITION" -xrm "*exitDelay: $DELAY" -xrm "*autoExit: True" >>$j.html 2>&1
			status=$?
			echo "</PRE>" >>$j.html
			echo "<IMG src="`basename $j`".gif><BR>" >>$j.html
			test -n "$PREV" && echo "<A href=../$PREV>&lt;previous&gt;</A>" >>$j.html
			if [ $status -ne 0 ]
			then
			    echo "<A href=$j.html>$j</A><BR>" >>index.html
			    test -n "$PREV" && echo "<A href=../$j.html>&lt;next&gt;</A>" >>$PREV
			    $SAY `basename $j` >/dev/null
			    echo "$j	failed $status"
			    echo "$j $status" >>failed
			    FAILED=`expr $FAILED + 1`
			    PREV="$j.html"
			else
			    echo "$j	okay"
			    rm -f $j.html
			fi
		done
	fi
done
kill $XVFB
echo ""
echo "$FAILED failed out of $TOTAL"
cmp -s failed failed.known
CMPSTAT=$?
if test $CMPSTAT -ne 0
then
    echo "< means recently pooched"
    echo "> means recently fixed"
    echo ""
    diff failed failed.known
else
    echo "No change in the tests"
fi
$FINISHED_CMD
exit $CMPSTAT
