#!/bin/sh

INITDIR=/etc/rc.d/init.d
HTTPDINIT=$INITDIR/httpd
PHPDIR=/var/www/html
DB2USER=db2inst3

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:@prefix@/java/com/firstworks/sqlrelay
export CLASSPATH=$CLASSPATH:./:@prefix@/java/sqlrelay.jar

DB=$1
API=$2

if ( test "$DB" = "db2" )
then
	ARGS="localhost 8001 \"\" db2test db2test"
	PHPARGS="host=localhost&port=8001&socket=&user=db2test&password=db2test"
	sudo $INITDIR/db2 start
	sudo su -l $DB2USER -c "sqlr-start -id db2test"
fi
if ( test "$DB" = "freetds" )
then
	ARGS="localhost 8002 \"\" freetdstest freetdstest"
	PHPARGS="host=localhost&port=8002&socket=&user=freetdstest&password=freetdstest"
	sudo $INITDIR/sybase start
	sleep 5
	sudo sqlr-start -id freetdstest
fi
if ( test "$DB" = "interbase" )
then
	ARGS="localhost 8003 \"\" interbasetest interbasetest"
	PHPARGS="host=localhost&port=8003&socket=&user=interbasetest&password=interbasetest"
	sudo $INITDIR/firebird start
	sudo sqlr-start -id interbasetest
fi
if ( test "$DB" = "msql" )
then
	ARGS="localhost 8005 \"\" msqltest msqltest"
	PHPARGS="host=localhost&port=8005&socket=&user=msqltest&password=msqltest"
	sudo $INITDIR/msql start
	sudo sqlr-start -id msqltest
fi
if ( test "$DB" = "mysql" )
then
	ARGS="localhost 8006 \"\" mysqltest mysqltest"
	PHPARGS="host=localhost&port=8006&socket=&user=mysqltest&password=mysqltest"
	sudo $INITDIR/mysqld start
	sudo sqlr-start -id mysqltest
fi
if ( test "$DB" = "oldmysql" )
then
	ARGS="localhost 8006 \"\" mysqltest mysqltest"
	PHPARGS="host=localhost&port=8006&socket=&user=mysqltest&password=mysqltest"
	sudo $INITDIR/mysqld start
	sudo sqlr-start -id mysqltest
fi
if ( test "$DB" = "oracle7" )
then
	ARGS="localhost 8008 \"\" oracle7test oracle7test"
	PHPARGS="host=localhost&port=8008&socket=&user=oracle7test&password=oracle7test"
	sudo $INITDIR/oracle start
	sleep 5
	sudo sqlr-start -id oracle7test
fi
if ( test "$DB" = "oracle8" )
then
	ARGS="localhost 8009 \"\" oracle8test oracle8test"
	PHPARGS="host=localhost&port=8009&socket=&user=oracle8test&password=oracle8test"
	sudo $INITDIR/oracle start
	sleep 5
	sudo sqlr-start -id oracle8test
fi
if ( test "$DB" = "oracle8i" )
then
	ARGS="localhost 8009 \"\" oracle8test oracle8test"
	PHPARGS="host=localhost&port=8009&socket=&user=oracle8test&password=oracle8test"
	sudo $INITDIR/oracle start
	sleep 5
	sudo sqlr-start -id oracle8test
fi
if ( test "$DB" = "postgresql6" )
then
	ARGS="localhost 8010 \"\" postgresql7test postgresql7test"
	PHPARGS="host=localhost&port=8010&socket=&user=postgresql7test&password=postgresql7test"
	sudo $INITDIR/postgresql start
	sudo sqlr-start -id postgresql7test
fi
if ( test "$DB" = "postgresql7" )
then
	ARGS="localhost 8010 \"\" postgresql7test postgresql7test"
	PHPARGS="host=localhost&port=8010&socket=&user=postgresql7test&password=postgresql7test"
	sudo $INITDIR/postgresql start
	sudo sqlr-start -id postgresql7test
fi
if ( test "$DB" = "postgresql8" )
then
	ARGS="localhost 8010 \"\" postgresql8test postgresql8test"
	PHPARGS="host=localhost&port=8010&socket=&user=postgresql8test&password=postgresql8test"
	sudo $INITDIR/postgresql start
	sudo sqlr-start -id postgresql8test
fi
if ( test "$DB" = "sqlite" )
then
	ARGS="localhost 8011 \"\" sqlitetest sqlitetest"
	PHPARGS="host=localhost&port=8011&socket=&user=sqlitetest&password=sqlitetest"
	sudo sqlr-start -id sqlitetest
fi
if ( test "$DB" = "sybase" )
then
	ARGS="localhost 8012 \"\" sybasetest sybasetest"
	PHPARGS="localhost 8012 \"\" sybasetest sybasetest"
	PHPARGS="host=localhost&port=8012&socket=&user=sybasetest&password=sybasetest"
	sudo $INITDIR/sybase start
	sleep 5
	sudo sqlr-start -id sybasetest
fi
if ( test "$DB" = "router" )
then
	ARGS="localhost 9000 \"\" pgsqlrouter pgsqlrouter"
	PHPARGS="localhost 9000 \"\" pgsqlrouter pgsqlrouter"
	PHPARGS="host=localhost&port=9000&socket=&user=pgsqlrouter&password=pgsqlrouter"
	sudo $INITDIR/postgresql start
	sudo $INITDIR/oracle start
	sleep 5
	sudo sqlr-start -id pgsqldb1
	sudo sqlr-start -id pgsqldb2
	sudo sqlr-start -id oracle8test
	sudo sqlr-start -id pgsqlrouter
fi


if ( test "$API" = "c" -o -z "$API" )
then
	echo
	echo "C:"
	cd c
	./$DB $ARGS
	rm -f cachefile* sqlnet.log
	cd ..
fi

if ( test "$API" = "c++" -o -z "$API" )
then
	echo
	echo "C++:"
	cd c++
	./$DB $ARGS
	rm -f cachefile* sqlnet.log
	cd ..
fi

if ( test "$API" = "java" -o -z "$API" )
then
	if ( test "@HAVE_JAVA@" = "yes" )
	then
		echo
		echo "Java:"
		cd java
		java $DB $ARGS
		rm -f cachefile* sqlnet.log
		cd ..
	fi
fi

if ( test "$API" = "perl" -o -z "$API" )
then
	if ( test "@HAVE_PERL@" = "yes" )
	then
		echo
		echo "Perl:"
		cd perl
		perl $DB.pl $ARGS
		rm -f cachefile* sqlnet.log
		cd ..
	fi
fi

if ( test "$API" = "python" -o -z "$API" )
then
	if ( test "@HAVE_PYTHON@" = "yes" )
	then
		echo
		echo "Python:"
		cd python
		python $DB.py $ARGS
		rm -f cachefile* sqlnet.log
		cd ..
	fi
fi

if ( test "$API" = "ruby" -o -z "$API" )
then
	if ( test "@HAVE_RUBY@" = "yes" )
	then
		echo
		echo "Ruby:"
		cd ruby
		ruby $DB.rb $ARGS
		rm -f cachefile* sqlnet.log
		cd ..
	fi
fi

if ( test "$API" = "php" -o -z "$API" )
then
	if ( test "@HAVE_PHP@" = "yes" )
	then
		echo
		echo "PHP:"
		cd php
		sudo $HTTPDINIT start
		sleep 1
		chmod 755 $DB.php
		sudo cp $DB.php $PHPDIR
		lynx -dump -source "http://localhost/$DB.php?$PHPARGS"
		sudo $HTTPDINIT stop
		sudo rm -f /tmp/cachefile* sqlnet.log
		cd ..
	fi
fi

if ( test "$API" = "tcl" -o -z "$API" )
then
	if ( test "@HAVE_TCL@" = "yes" )
	then
		echo
		echo "TCL:"
		cd tcl
		tclsh $DB.tcl $ARGS
		rm -f cachefile* sqlnet.log
		cd ..
	fi
fi


if ( test "$DB" = "db2" )
then
	sudo su -l $DB2USER -c "sqlr-stop"
	sudo $INITDIR/db2 stop
else
	sudo sqlr-stop
fi
if ( test "$DB" = "freetds" )
then
	sudo $INITDIR/sybase stop
fi
if ( test "$DB" = "interbase" )
then
	sudo $INITDIR/firebird stop
fi
if ( test "$DB" = "msql" )
then
	sudo $INITDIR/msql stop
fi
if ( test "$DB" = "mysql" )
then
	sudo $INITDIR/mysqld stop
fi
if ( test "$DB" = "oldmysql" )
then
	sudo $INITDIR/mysqld stop
fi
if ( test "$DB" = "oracle7" )
then
	sudo $INITDIR/oracle stop
fi
if ( test "$DB" = "oracle8" )
then
	sudo $INITDIR/oracle stop
fi
if ( test "$DB" = "oracle8i" )
then
	sudo $INITDIR/oracle stop
fi
if ( test "$DB" = "postgresql6" )
then
	sudo $INITDIR/postgresql stop
fi
if ( test "$DB" = "postgresql7" )
then
	sudo $INITDIR/postgresql stop
fi
if ( test "$DB" = "postgresql8" )
then
	sudo $INITDIR/postgresql stop
fi
if ( test "$DB" = "sybase" )
then
	sudo $INITDIR/sybase stop
fi
if ( test "$DB" = "router" )
then
	sudo $INITDIR/postgresql stop
	sudo $INITDIR/oracle stop
fi
