#!/bin/sh

PREFIX=/usr/local/firstworks

# stop any existing instances
$PREFIX/bin/sqlr-stop
sleep 2

# for each database/configuration...
for DB in  firebird freetds informix mdbtools mysql oracle tls krb extensions postgresql sqlite sap router db2
do

	# for tls, krb, and extensions tests, verify that we support oracle
	if ( test "$DB" = "tls" -o "$DB" = "krb" -o "$DB" = "extensions" )
	then
		if ( test -z "`ls $PREFIX/lib*/sqlrelay/sqlrconnection_oracle.so 2> /dev/null`" )
		then
			echo "skipping $DB..."
			echo
			echo "================================================================================"
			echo
			continue
		fi

	# for other tests, verify that we support the database
	else
		if ( test -z "`ls $PREFIX/lib*/sqlrelay/sqlrconnection_$DB.so 2> /dev/null`" )
		then
			echo "skipping $DB..."
			echo
			echo "================================================================================"
			echo
			continue
		fi
	fi

	# for router tests, also verify that we support mysql
	if ( test "$DB" = "router" )
	then
		if ( test -z "`ls $PREFIX/lib*/sqlrelay/sqlrconnection_mysql.so 2> /dev/null`" )
		then
			echo "skipping $DB..."
			echo
			echo "================================================================================"
			echo
			continue
		fi
	fi

	# testing...
	echo "testing $DB (from `hostname`)..."

	# for the router test, start the master/slave instances
	if ( test "$DB" = "router" )
	then
		$PREFIX/bin/sqlr-start -id routermaster -backtrace `pwd`
		sleep 2

		$PREFIX/bin/sqlr-start -id routerslave -backtrace `pwd`
		sleep 2
	fi

	# start the instance
	$PREFIX/bin/sqlr-start -id ${DB}test -backtrace `pwd`
	sleep 2

	# make sure that the instance is up
	PING=""
	for attempts in 1 2 3 4 5 6 7 8 9 10
	do
		echo
		echo "pinging $DB..."
		if ( test "$DB" = "tls" )
		then
			PING=`$PREFIX/bin/sqlrsh -host localhost -tls -tlscert $PREFIX/etc/client.pem -tlsca $PREFIX/etc/ca.pem -tlsvalidate ca -command ping`
		elif ( test "$DB" = "extensions" )
		then
			PING=`$PREFIX/bin/sqlrsh -host localhost -user test -password test -command ping`
		else
			PING=`$PREFIX/bin/sqlrsh -id ${DB}test -command ping`
		fi

		# (this collapses whitespace)
		PING=`echo $PING`

		echo $PING

		if ( test "$PING" = "0: Couldn't connect to the listener." )
		then
			sleep 2
		else
			break
		fi
	done

	# run the tests
	if ( test "$PING" = "The database is up." )
	then

		# kinit for krb tests
		if ( test "$DB" = "krb" )
		then
			kdestroy
			kinit
		fi

		echo
		echo "success..."
		echo
		./test.sh $DB
	else
		echo
		echo "failed to start ${DB}test"
		echo
		echo "hit enter to continue or ctrl-c to stop..."
		if ( test "$DB" = "krb" )
		then
			read -t 20 PROMPT
		else
			read PROMPT
		fi
	fi

	# shut down the instance(s)
	if ( test "$DB" = "router" )
	then
		sleep 2
		$PREFIX/bin/sqlr-stop -id routermaster
		sleep 2
		$PREFIX/bin/sqlr-stop -id routerslave
	fi
	sleep 2
	$PREFIX/bin/sqlr-stop -id ${DB}test
	sleep 2

	echo
	echo "================================================================================"
	echo
done
