#!/bin/sh

passedfile=$1
basename=$2
testcmd="$3"

if [ -f $passedfile ] ; then
	if [ `grep -c "^$testcmd\$" $passedfile` -gt 0 ] ; then
		echo "$2: already passed"
		exit 0
	fi
fi
$testcmd > $2.r1 2> $2.r2
res=$?
if [ $res -ge 128 ] ; then
	echo "$2: aborted with exit status $res"
	exit 1
fi
cat $2.r1 > $2.r
if [ -s $2.r2 ] ; then
	echo "/* -- errors: --" >> $2.r
	cat $2.r2 >> $2.r
	echo "*/" >> $2.r
fi
diff -c $2.out $2.r
res=$?

if [ "$4" = "update" ] ; then
	cp $2.r $2.out
	echo "$2: updated"
	echo $3 >> $passedfile
	exit 0
fi
if [ $res = 0 ] ; then
	echo "$2: passed"
	echo $3 >> $passedfile
else
	echo "$2: failed"
	echo "testcmd=\"$3\""
	exit 1
fi
