#!/bin/sh

# try to figure out which syntax we need
# to prevent a linefeed at the end of echo
# Some versions of sh use -n as an option
# others use \c at the end of the line.
# This is just for cosmetic reasons...
A=`echo "\c"|grep c`
if test "$A" != "" ; then 
	A=`echo -n "k"|grep n` 
	if test "$A" = "" ; then 
		ECHO="echo -n "
		SUFFIX=""
	else 
		ECHO="echo"
		SUFFIX=""
	fi 
else  
	ECHO="echo"
	SUFFIX="\c"
fi

if test "${1}" = "-section" ; then
	SECTION=${2}
	shift 2
	echo " "
	echo "Executing tests for section ${SECTION}:"
fi


#
# if -continue is given (after -section XX !)
# the test does not abort after the first section 
# containing failing tests
#
if test "${1}" = "-continue" ; then
	ABORT_ON_FAIL=0
	shift
else
	ABORT_ON_FAIL=1
fi

if test "${1}" = "-info" ; then
	echo " "
	echo "Now running all class tests."
	echo " "
	echo "This test suite verifies that all classes included in the test"
	echo "could be correctly compiled and checks their basic functionality."
	echo "If problems occur for one of these tests, you might want to run"
	echo "this test program manually. Any failing test will be rerun with"
	echo "verbose output to identify the subtest that caused the problem."
	echo "You may run each test manually in the verbose mode by specifying -v"
	echo "on the command line."
	echo "Running these tests may take some time...."
	echo " "
	exit 0
fi

if test "${1}" = "-success" ; then
	echo " "
	echo "=============================================="
	echo "All tests passed. Congratulations!"
	echo " "
	exit 0
fi

# remember any failed tests by setting OK=false
OK=true

# remove any core in the directory and all backtrace files
rm core *.backtrace 2>/dev/null


# loop over all tests
COUNT=1
for i in $* ; do
	# print the name of the test running
	$ECHO " (${COUNT}) $i: $SUFFIX"
	
	# remove potential log files
	rm $i.log 2>/dev/null

	# run it and examine its return value
	if ./$i 2>/dev/null >/dev/null ; then
		# test passed - OK
		echo "OK"
	else 
		# test didn't pass
		OK=false
			
		# if backtracing suport is configured, try a stack backtrace
		if test "${BALL_BACKTRACE}" != "" -a -f core ; then
			${BALL_BACKTRACE} $i
			rm core 2>/dev/null
		fi
	
		# remember this test
		FAILED_TESTS="${FAILED_TESTS} $i"

		# rerun it in verbose mode to find out which
		# subtest failed
		echo "Failure in test $i. Please check log file $i.log."
		( ./$i -V 2>&1 |cat > $i.log ) 2>/dev/null
		if test -f core ; then
			rm core
		fi
	fi
	COUNT=`expr ${COUNT} + 1`
done

# print some summary for failed tests
if test "$OK" = true ; then
	exit 0
else
	echo "============================================"
	echo " "
	echo "The following subtests failed:"

	for i in ${FAILED_TESTS} ; do 
		echo " - $i"
	done

	echo " "
	echo "Please mail the output of this run to one of the developers"
	echo "and include a detailed description of the system you use."
	echo "It is also neccessary to include the files ../config.mak ../config.h"
	echo " "

	exit ${ABORT_ON_FAIL}
fi
