#!/bin/sh

# Set Variables
NAME=dbXML
DBXML_HOME=%%DBXML_HOME%%
LOG=${DBXML_HOME}/logs/${NAME}ctl.log
JAR_DIR=${DBXML_HOME}/java/lib
PID_FILE=/var/run/${NAME}.pid
MYSELF=`basename $0`
USER_NAME=dbxml

# Check if we're being run as a shell script or as an rc script
if [ ${MYSELF} = "00dbxml.sh" ]; then
	AS_RC_SCRIPT=yes
else
	AS_RC_SCRIPT=no
fi

# Check if the JAVA_HOME directory is defined, otherwise set it to the
# fallback default
if [ "${JAVA_HOME}a" = "a" ]; then
	JAVA_HOME=%%JAVA_HOME%%
	export JAVA_HOME
fi
JAVA_CMD=${JAVA_HOME}/bin/java

# Make sure the dbXML directory does exist
if [ ! -d ${DBXML_HOME} ]; then
	if [ "${AS_RC_SCRIPT}" = "yes" ]; then
		echo ""
	fi
	echo "${NAME}: ERROR: Unable to find dbXML home directory at ${DBXML_HOME}."
	exit 1
fi

# Make sure the Java VM can be found
if [ ! -x ${JAVA_CMD} ]; then
	if [ "${AS_RC_SCRIPT}" = "yes" ]; then
		echo ""
	fi
	echo "${NAME}: ERROR: Unable to find Java VM at ${JAVA_HOME}."
	exit 2
fi

# Make sure the JAR file Directory exists
if [ ! -d ${JAR_DIR} ]; then
	if [ "${AS_RC_SCRIPT}" = "yes" ]; then
		echo ""
	fi
	echo "${NAME}: ERROR: Unable to find JAR Directory at ${JAR_DIR}."
	exit 3
fi

server_shutdown() {			
	# Read in instances and kill processes
	INSTANCES="`cat ${DBXML_HOME}/config/instances.cfg`"
	for instance in $INSTANCES
	do
		/usr/local/bin/dbxmladmin shutdown -c /$instance 1> /dev/null 2> /dev/null
	done

	# Remove stale pid file
	rm ${PID_FILE}
   	}
   	
server_start() {
	# setup dbXML Classpath
	CLASSPATH=${DBXML_HOME}/classgen:${DBXML_HOME}/config:$CLASSPATH
	JARS=`ls -1 ${JAR_DIR}/*.jar`
	for jar in $JARS
	do
		CLASSPATH=$jar:$CLASSPATH ;
	done

	# Read in the extra VM paramters for starting the program
	VMPROPS=""
	if [ -f ${DBXML_HOME}/config/vm.cfg ]; then
		PROPS="`cat ${DBXML_HOME}/config/vm.cfg`"
		for prop in $PROPS
		do
			VMPROPS="$VMPROPS $prop"
		done
	fi

	# Setup pid file
	touch ${PID_FILE}
	chown ${USER_NAME} ${PID_FILE}
	chmod 600 ${PID_FILE}

	# Start dbXML
	su -fm ${USER_NAME} -c "(cd ${DBXML_HOME} && ${JAVA_CMD} -Xms16m -Xmx168m -classpath "$CLASSPATH" -Ddbxml.home="${DBXML_HOME}" org.dbxml.core.server.dbXML & echo \$! > ${PID_FILE}) > ${LOG}"
	}
	
	case "$1" in
		start)
		# Make sure another instance of dbXML is not running
		if [ -e ${PID_FILE} ]; then
			if [ "${AS_RC_SCRIPT}" = "yes" ]; then
				echo ""
			fi
		   echo "A dbxml instance is already running with PID" `cat ${PID_FILE}`
		   exit 0
		fi
		echo "starting dbXML...."
		server_start;
		;;
	stop)
		# Make sure an instance of dbXML is running
		if [ ! -e ${PID_FILE} ]; then
			if [ "${AS_RC_SCRIPT}" = "yes" ]; then
				echo ""
			fi
			echo "dbXML is not currently running"
			exit 0
		fi
		echo "stopping dbXML...."
		server_shutdown;
   		;;
   	restart)
		# Make sure an instance of dbXML is running
		if [ ! -e ${PID_FILE} ]; then
			server_start;
		else
			echo "restarting dbXML...."
			server_shutdown;
			server_start;
		fi
		;;
	*)
		echo ""
		echo "Usage: ${MYSELF} { start | restart | stop}"
		echo ""
		exit 64
		;;
esac
