# /bin/sh -x

OK="\033[1;32m OK  \033[0m"
FAIL="\033[1;31m  FAILED  \033[0m"

file=$1
keep=$2

if test -x $file
   then
   	\rm -f $file.diff
	\rm -f testoutput/$file.tmp
	\rm -f testoutput/$file.err*
	\rm -f testoutput/$file.diff
   	echo -n "testing  $file "
	./$file > testoutput/$file.tmp 2> testoutput/$file.err.1
	diff -wb --ignore-matching-lines=".?*-annotation .?*" --ignore-matching-lines=".http://www.w3.org/1999.?*" --ignore-matching-lines=".*generator=.*" --ignore-matching-lines=".*datetime=.*" --ignore-matching-lines=".*uctodata.*" --ignore-matching-lines=".*folia_version=.*" --ignore-matching-lines=".*libfolia.*" --ignore-matching-lines=".*local/share.*" testoutput/$file.tmp $file.ok > testoutput/$file.diff 2>& 1
	if [ $? -ne 0 ];
	then sed 's/\/.*\///g' testoutput/$file.err.1 > testoutput/$file.err
	     diff testoutput/$file.err $file.ok > testoutput/$file.diff 2>& 1;
	     if [ $? -ne 0 ];
	     then
            echo -e $FAIL;
	     	echo "differences logged in testoutput/$file.diff";
	     	echo "stderr messages logged in testoutput/$file.err";
            exit 1
	     else
            echo -e $OK
	    if [ $keep == "" ];
	    then
              \rm -f testoutput/$file.diff
              \rm -f testoutput/$file.err
	    fi
            exit 0
	    fi
	else
	    \rm -f testoutput/$file.diff
	    echo -e $OK
            exit 0
	fi
fi
