#########################################################################
# Invocation arguments are:						#
#	-v: verbose, print the compilation command			#
#	-c: continue testing after a test fails				#
#	-src: testing the source compatibility package			#
#	-bin: testing the binary compatibility package (default)	#
#	-native: test the native stdio					#
#	-whatever_else: bad argument, will be ignored.			#
#	tests: source of tests to be run.				#
#									#
# Below are a few example test runs:					#
#	runtest:							#
#		tests the uni-threaded binary compatibility package.	#
#	runtest -src -mt -lpthread:					#
#		tests the multi-threaded source compatibility package	#
#		on a Linux/Irix/Solaris platform.			#
#	runtest -mt -lcma:						#
#		tests the multi-threaded binary compatibility package	#
#		on a HPUX platform (-lcma defines the thread library).	#
#									#
# Written by Kiem-Phong Vo 						#
#########################################################################

verbose=0
exit_on_fail=1
files=""
LIBS=""
HDRS=""
DEFS=""
XLIBS=""

tsttype="binary"	# test binary emulation by default
envtype="ut"		# test uni-threaded by default

while test "$1" != ""
do
	case $1 in
	-l*)	XLIBS="$XLIBS $1";
		;;
	-v)	verbose=1;
		;;
	-c)	exit_on_fail=0;
		;;
	-src)	tsttype="source";
		;;
	-bin)	tsttype="binary";
		;;
	-native) tsttype="native";
		;;
	-mt)	envtype="mt";
		;;
	-ut)	envtype="ut";
		;;
	-*)	echo "Hmm, unknown argument: $1";
		;;
	*)	files="${files} $1";
		;;
	esac
	shift
done

if test "$CC" = ""; then CC="cc"; fi

if test "$files" = ""
then
	files="*.c"
fi

if test $tsttype = source
then
	HDRS="-I../Stdio_s"
	if test $envtype = "mt"
	then	DEFS="-Dvt_threaded=1";
		LIBS="../libsfio-mt.a ../../vthread/libvthread.a"
	else	DEFS="-Dvt_threaded=0"
		LIBS="../libsfio.a"
	fi
elif test $tsttype = binary
then
	HDRS=""
	if test $envtype = "mt"
	then	DEFS="-Dvt_threaded=1";
		LIBS="../Stdio_b/libstdio-mt.a ../libsfio-mt.a ../../vthread/libvthread.a"
	else	DEFS="-Dvt_threaded=0"
		LIBS="../Stdio_b/libstdio.a ../libsfio.a"
	fi
else
	HDRS=""
	if test $envtype = "mt"
	then	DEFS="-Dvt_threaded=1";
		LIBS="../../vthread/libvthread.a"
	else	DEFS="-Dvt_threaded=0"
		LIBS=""
	fi
fi

LIBS="$LIBS $XLIBS"

if test "$verbose" = 1
then echo "Compile: $CC -g -I. -I.. -I../../vthread $HDRS $DEFS whatever.c $LIBS -o t"
fi

for i in $files
do
	echo "-- $i:"
	status=""
	if $CC -g -I. -I.. -I../../vthread $HDRS $DEFS $i $LIBS -o t
	then
		if ./t
		then	rm t; status=Passed
		else	status=Failed
		fi
	else	status="Not compiled"
	fi
	echo "	$status"
	if test "$status" != "Passed"
	then if test "$exit_on_fail" = "1"; then exit; fi
	fi
done
