#!/bin/sh


# tee output to testdetails.log
if ( test "$CHILD" != "1" )
then
	#FIFO=testall.fifo
	#rm -f $FIFO
	#mkfifo $FIFO
	#CHILD=1 $0 $* > $FIFO 2>&1 &
	CHILD=1 $0 2>&1 | tee testdetails.log &
	PID=$!
	#tee testdetails.log <$FIFO &
	wait $PID
	#rm -f $FIFO
	exit $?
fi



echo "SQL Relay test results:" > testresults.log
echo >> testresults.log

PREFIX=@prefix@

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

# really kill them
ps aux > /dev/null 2> /dev/null
if ( test "$?" = "0" )
then
	kill `ps aux | grep sqlr-connection | grep -v grep | cut -c10-15` 2> /dev/null
	kill `ps aux | grep sqlr-scaler | grep -v grep | cut -c10-15` 2> /dev/null
	kill `ps aux | grep sqlr-listener | grep -v grep | cut -c10-15` 2> /dev/null
else
	kill `ps -efa | grep sqlr-connection | grep -v grep | cut -c10-15` 2> /dev/null
	kill `ps -efa | grep sqlr-scaler | grep -v grep | cut -c10-15` 2> /dev/null
	kill `ps -efa | grep sqlr-listener | grep -v grep | cut -c10-15` 2> /dev/null
fi
sleep 2

# for each database/configuration...
for DB in  @TESTDBS@
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 the mysqlprotocol test, verify that we support the mysql
	elif ( test "$DB" = "mysqlprotocol" )
	then
		if ( test -z "`ls $PREFIX/lib*/sqlrelay/sqlrconnection_mysql.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`)..."
	echo "$DB..." >> testresults.log

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

		$PREFIX/bin/sqlr-start -config `pwd`/sqlrelay.conf -id routerslave -backtrace `pwd`
		sleep 2
	fi

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

	# make sure that the instance is up
	PING=""
	BYPASS="no"
	for attempts in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
	do
		echo
		echo "pinging $DB..."
		if ( test "$DB" = "tls" )
		then
			PING=`$PREFIX/bin/sqlrsh -host localhost -tls -tlscert $PREFIX/etc/sqlrelay.conf.d/client.pem -tlsca $PREFIX/etc/sqlrelay.conf.d/ca.pem -tlsvalidate ca -command ping 2>&1`
		elif ( test "$DB" = "extensions" )
		then
			PING=`$PREFIX/bin/sqlrsh -host localhost -user test -password test -command ping 2>&1`
		elif ( test "$DB" = "mysqlprotocol" )
		then
			PING="0: Couldn't connect to the listener."
			if ( test -n "`netstat -an 2> /dev/null | grep LISTEN | grep 3306`" )
			then
				PING="The database is up."
			fi
		else
			PING=`$PREFIX/bin/sqlrsh -id ${DB}test -command ping 2>&1`
		fi

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

		echo $PING

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

			# If ssl is sufficiently old then the ssl test will
			# fail like this.  Just bypass the test in that case.
			SSLPROBLEM=`echo "$PING" | grep "unknown message digest algorithm"`
			if ( test -n "$SSLPROBLEM" )
			then
				BYPASS="yes"
			fi
			break
		fi
	done

	# bypass the test if necessary
	if ( test "$BYPASS" = "no" )
	then

		# 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

			if ( test "$DB" = "mysqlprotocol" )
			then
				./dropin/mysql
				if ( test "$?" = "0" )
				then
					printf "    % 8s: success\n" "dropin" >> testresults.log
				else
					printf "    % 8s: failed\n" "dropin" >> testresults.log
				fi
			else
				./test.sh $DB
			fi
		else
			echo
			echo "failed to start ${DB}test"
			echo
			echo "     connect: failed" >> testresults.log
		fi
	fi

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

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

cat testresults.log

exit 0
