#!/bin/sh

if ( test -z "$1" )
then
	echo "usage: test database"
	exit
fi

for DIR in @TESTAPIS@
do
	echo "testing $1 in $DIR"
	echo
	cd $DIR

	TEST=""
	TESTFILE=""
	case "$DIR" in
		c)
			TEST="./$1"
			TESTFILE="$1"
			;;
		c++)
			TEST="./$1"
			TESTFILE="$1"
			;;
		cs)
			mono --version > /dev/null 2>&1
			if ( test "$?" = "0" )
			then
				TEST="mono $1.exe"
				TESTFILE="$1.exe"
			fi
			;;
		java)
			java -h > /dev/null 2>&1
			if ( test "$?" = "0" )
			then
				TEST="./run $1"
				TESTFILE="$1.class"
			fi
			;;
		nodejs)
			node --version > /dev/null 2>&1
			if ( test "$?" = "0" )
			then
				TEST="node $1.js"
				TESTFILE="$1.js"
			fi
			;;
		perl)
			perl --version > /dev/null 2>&1
			if ( test "$?" = "0" )
			then
				TEST="perl $1.pl"
				TESTFILE="$1.pl"
			fi
			;;
		perldbi)
			perl --version > /dev/null 2>&1
			if ( test "$?" = "0" )
			then
				TEST="perl $1.pl"
				TESTFILE="$1.pl"
			fi
			;;
		php)
			php --version > /dev/null 2>&1
			if ( test "$?" = "0" )
			then
				TEST="php $1.php"
				TESTFILE="$1.php"
			fi
 			;;
		phppdo)
			php --version > /dev/null 2>&1
			if ( test "$?" = "0" )
			then
				TEST="php $1.php"
				TESTFILE="$1.php"
			fi
			;;
		python)
			@PYTHON@ --version > /dev/null 2>&1
			if ( test "$?" = "0" )
			then
				TEST="@PYTHON@ $1.py"
				TESTFILE="$1.py"
			fi
			;;
		pythondb)
			@PYTHON@ --version > /dev/null 2>&1
			if ( test "$?" = "0" )
			then
				TEST="@PYTHON@ $1.py"
				TESTFILE="$1.py"
			fi
			;;
		ruby)
			ruby --version > /dev/null 2>&1
			if ( test "$?" = "0" )
			then
				TEST="ruby $1.rb"
				TESTFILE="$1.rb"
			fi
			;;
		tcl)
			echo 'puts $tcl_version;exit 0'|tclsh > /dev/null 2>&1
			if ( test "$?" = "0" )
			then
				TEST="tclsh $1.tcl"
				TESTFILE="$1.tcl"
			fi
			;;
	esac


	if ( test -r "$TESTFILE" )
	then
		$TEST
		if ( test "$?" -ne "0" )
		then
			echo
			echo
			echo "$1 failed in $DIR"
			echo
			printf "    % 8s: failed\n" "$DIR" >> ../testresults.log
		else
			echo
			echo "test complete"
			printf "    % 8s: success\n" "$DIR" >> ../testresults.log
		fi
	else
		echo "no test found for $1 in $DIR"
	fi

	echo
	echo "================================================================================"
	echo

	cd ..
done
