#!/bin/csh -f
#
#  Update_DB
#
# A wrapper for the PIRL Java Database updater.
#
# (see http://pirlwww.lpl.arizona.edu/software/PIRL_Java_Packages/PIRL/Database/Update_DB.html)
#
#	Environment variables used and their default values:
#
#	PIRL_JAVA_HOME - /opt/java
#		This may be a directory pathname where the PIRL subdirectory and
#		all its class files is located; or it may be the pathname to the
#		PIRL.jar file containing all dependencies. On Darwin systems the
#		~/Library/Java/Extensions and /Library/Java/Extensions directory
#		will be checked for the PIRL.jar file.
#
#	The following are used only if PIRL_JAVA_HOME is not a jar file.
#
#	MySQL_JDBC - $PIRL_JAVA_HOME/mysql-connector/mysql-connector.jar
#		The pathname to the MySQL JDBC driver jar file or the root
#		directory where its class files are located.
#		(http://dev.mysql.com/downloads/connector/j/)
#
#	PostgreSQL_JDBC - $PIRL_JAVA_HOME/PostgreSQL/postgresql.jar
#		The pathname to the PostgreSQL JDBC driver jar file or the root
#		directory where its class files are located.
#		(http://jdbc.postgresql.org/)
#
#	JCM - $PIRL_JAVA_HOME/jcm/jcm_data.jar
#		The pathname to the Java Components for Mathematics jar file or
#		the root directory where its class files are located.
#		(http://math.hws.edu/javamath)
#
#	CVS ID: Update_DB,v 1.1 2008/10/14 03:35:06 castalia Exp

set PIRL_root			= /opt/java
set Darwin_Extensions	= Library/Java/Extensions
set MySQL_JDBC_dir		= mysql-connector
set MySQL_JDBC_jar		= mysql-connector.jar
set PostgreSQL_JDBC_dir	= PostgreSQL
set PostgreSQL_JDBC_jar	= postgresql.jar
set JCM_dir				= jcm
set JCM_jar				= jcm_data.jar

set OS = `uname -s`


#	Location of the PIRL Java Packages.
if (! $?PIRL_JAVA_HOME) then
	if (-e $PIRL_root/PIRL) then
		set PIRL_JAVA_HOME = $PIRL_root
	else if ($OS == "Darwin") then
		if (-e ~/$Darwin_Extensions/PIRL.jar) then
			set PIRL_JAVA_HOME = ~/$Darwin_Extensions/PIRL.jar
		else if (-e /$Darwin_Extensions/PIRL.jar) then
			set PIRL_JAVA_HOME = /$Darwin_Extensions/PIRL.jar
		endif
	endif
	if (! $?PIRL_JAVA_HOME) then
		echo "Set the PIRL_JAVA_HOME environment variable"
		echo "to the location of the PIRL Java Packages."
		exit 1
	endif
endif
if (! -e $PIRL_JAVA_HOME) then
	echo "No such file or directory: $PIRL_JAVA_HOME"
	echo "The PIRL Java Packages are required."
	exit -1
endif
set classpath = $PIRL_JAVA_HOME


if ($PIRL_JAVA_HOME !~ *.jar) then

	#	Database drivers:

	#	Location of the MySQL JDBC driver.
	if (! $?MySQL_JDBC) then
		if (-e $PIRL_JAVA_HOME/$MySQL_JDBC_dir/$MySQL_JDBC_jar) then
			set MySQL_JDBC = $PIRL_JAVA_HOME/$MySQL_JDBC_dir/$MySQL_JDBC_jar
		else if ($OS == "Darwin") then
			if (-e ~/$Darwin_Extensions/$MySQL_JDBC_jar) then
				set MySQL_JDBC = ~/$Darwin_Extensions/$MySQL_JDBC_jar
			else if (-e /$Darwin_Extensions/$MySQL_JDBC_jar) then
				set MySQL_JDBC = /$Darwin_Extensions/$MySQL_JDBC_jar
			endif
		endif
		if ($?MySQL_JDBC) then
			set classpath = ${classpath}:$MySQL_JDBC
		endif
	else
		if (! -e $MySQL_JDBC) then
			echo "No such file or directory: $MySQL_JDBC"
			echo "MySQL database support is not available."
			unset MySQL_JDBC
			unsetenv MySQL_JDBC
		else
			set classpath = ${classpath}:$MySQL_JDBC
		endif
	endif

	#	Location of the PostgreSQL JDBC driver.
	if (! $?PostgreSQL_JDBC) then
		if (-e $PIRL_JAVA_HOME/$PostgreSQL_JDBC_dir/$PostgreSQL_JDBC_jar) then
			set PostgreSQL_JDBC = $PIRL_JAVA_HOME/$PostgreSQL_JDBC_dir/$PostgreSQL_JDBC_jar
		else if ($OS == "Darwin") then
			if (-e ~/$Darwin_Extensions/$PostgreSQL_JDBC_jar) then
				set PostgreSQL_JDBC = ~/$Darwin_Extensions/$PostgreSQL_JDBC_jar
			else if (-e /$Darwin_Extensions/$PostgreSQL_JDBC_jar) then
				set PostgreSQL_JDBC = /$Darwin_Extensions/$PostgreSQL_JDBC_jar
			endif
		endif
		if ($?PostgreSQL_JDBC) then
			set classpath = ${classpath}:$PostgreSQL_JDBC
		endif
	else
		if (! -e $PostgreSQL_JDBC) then
			echo "No such file or directory: $PostgreSQL_JDBC"
			echo "PostgreSQL database support is not available."
			unset PostgreSQL_JDBC
			unsetenv PostgreSQL_JDBC
		else
			set classpath = ${classpath}:$PostgreSQL_JDBC
		endif
	endif

	if (! $?MySQL_JDBC && ! $?PostgreSQL_JDBC) then
		echo "Database support is required."
		echo
		echo "One or both of the following will provide database support:"
		echo
		echo "Use the MySQL_JDBC environment variable"
		echo "to set the location of the $MySQL_JDBC_jar file"
		echo "or place it in the $PIRL_root/$MySQL_JDBC_dir directory."
		echo "See http://dev.mysql.com/downloads/connector/j/"
		echo
		echo "Use the PostgreSQL_JDBC environment variable"
		echo "to set the location of the $PostgreSQL_JDBC_jar file"
		echo "or place it in the $PIRL_root/$PostgreSQL_JDBC_dir directory."
		echo "See http://jdbc.postgresql.org/"

		Jar_Location_Note:
		if ($OS == "Darwin") then
			echo
			echo "Jar files may also be placed in the"
			echo "~/$Darwin_Extensions or"
			echo "/$Darwin_Extensions directories."
		endif
		echo
		exit -1
	endif

	#	External Java packages:

	#	Location of the Java Components for Mathematics.
	if (! $?JCM) then
		if (-e $PIRL_JAVA_HOME/$JCM_dir/$JCM_jar) then
			set JCM = $PIRL_JAVA_HOME/$JCM_dir/$JCM_jar
		else if ($OS == "Darwin") then
			if (-e ~/$Darwin_Extensions/$JCM_jar) then
				set JCM = ~/$Darwin_Extensions/$JCM_jar
			else if (-e /$Darwin_Extensions/$JCM_jar) then
				set JCM = /$Darwin_Extensions/$JCM_jar
			endif
		endif
		if (! $?JCM) then
			goto JCM_Note
		endif
	else
		if (! -e $JCM) then
			echo "No such file or directory: $JCM"

			JCM_Note:
			echo
			echo "The Java Components for Mathematics are required by Conductor."
			echo "Use the JCM environment variable"
			echo "to set the location of the $JCM_jar file"
			echo "or place it in the $PIRL_root/$JCM_dir directory."
			echo "See http://math.hws.edu/javamath"
			goto Jar_Location_Note
		endif
	endif
	set classpath = ${classpath}:$JCM

endif


exec java -cp $classpath PIRL.Database.Update_DB $argv:q
