#!/bin/bash
################################################################################
# Filename: ./src/base/etc/init.d/qrm-server
# 
# The contents of this file are subject to the Qlusters Public License
# Version 1.1 (the License); you may not use this file except in compliance
# with the License.
# You may obtain a copy of the License at
# http://www.openqrm.org/content/view/33/69/.
# 
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
# 
# The Original Code is openQRM.
# 
# The Initial Developer of the Original Code is Qlusters Corporation. Portions
# created by Qlusters Corporation are Copyright (C) 2003-2005. All Rights
# Reserved.
# 
# Contributor(s):
# 
# Description:
# 
# Comments:
################################################################################
# qrm-server	Qrm-server init script
#
# chkconfig: 2345 98 24
# description: Qrm-server init script

if [ -z "$QRM_SERVER_BASE_DIR" ]; then
	SCRIPT=$0
	while [ -L "$SCRIPT" ]; do
		SRC=$(readlink $SCRIPT)
		if [ "${SRC:0:1}" != "/" ]; then
			SRC=$(dirname $SCRIPT)/$SRC
		fi
		SCRIPT=$SRC
	done

        QRM_SERVER_BASE_DIR=`pushd \`dirname $SCRIPT\`/../../../ 1>/dev/null && pwd && popd 1>/dev/null`
fi

CONFIG_FILE=$QRM_SERVER_BASE_DIR/qrm/etc/qrm.ini

if [ ! -f $CONFIG_FILE ]; then
	echo "ERROR: Could not find Qrm-server configuration file $CONFIG_FILE"
	exit 1
fi

export QRM_SERVER_BASE_DIR

# set the environment
. $QRM_SERVER_BASE_DIR/qrm/etc/qrm.env
if [ -f $QRM_SERVER_BASE_DIR/qrm/etc/user.env ]; then
	. $QRM_SERVER_BASE_DIR/qrm/etc/user.env
fi
# memory loaded functions
. $QRM_SERVER_BASE_DIR/qrm/include/qrm-functions
. $QRM_SERVER_BASE_DIR/qrm/include/qrm-server-functions
. $QRM_SERVER_BASE_DIR/qrm/include/qrm-setup-functions
qrm_export_functions $QRM_SERVER_BASE_DIR/qrm/include/qrm-server-functions
qrm_export_functions $QRM_SERVER_BASE_DIR/qrm/include/qrm-functions
qrm_export_functions $QRM_SERVER_BASE_DIR/qrm/include/qrm-setup-functions

IS_SOURCED=y
. $QRM_SERVER_BASE_DIR/qrm/bin/qrm-boot-image
. $QRM_SERVER_BASE_DIR/qrm/bin/qrm-filesystem-image
qrm_export_functions $QRM_SERVER_BASE_DIR/qrm/bin/qrm-boot-image
qrm_export_functions $QRM_SERVER_BASE_DIR/qrm/bin/qrm-filesystem-image
unset IS_SOURCED

qrm_get_server_config 1>/dev/null

start() {
	echo "Starting the Qrm-server"
	# check qrm-interface
	if ! ifconfig "$QRM_SERVER_INTERFACE" | grep inet 1>/dev/null 2>&1; then
		echo "ERROR : Interface $QRM_SERVER_INTERFACE is down. Please configure and activate it and try again"
		exit 1
	else
		CONFIGURED_IP=`cat $QRM_SERVER_BASE_DIR/qrm/tftpboot/pxelinux.cfg/default 2>/dev/null | grep "qrm=" | sed -e "s/.*qrm=//" | awk {' print $1 '}`
		if [ "$QRM_SERVER_IP_ADDRESS" != "$CONFIGURED_IP" ]; then
			echo "ERROR: Interface $QRM_SERVER_INTERFACE has the wrong ip-configuration."
			echo "       The Qrm-server is configured to have the ip-address $CONFIGURED_IP"
			echo "       but the interface $QRM_SERVER_INTERFACE is running with the ip-address $QRM_SERVER_IP_ADDRESS"
			exit 1
		fi
	fi

	# get version details as VERSION_xxx variables	
	eval export `cat ${QRM_SERVER_BASE_DIR}/qrm/VERSION.txt | sed 's/\(.*\)=\(.*\)$/VERSION_\1=\2/g'`

	# start server services
	if cd $TOMCAT_HOME; then
		for file in conf/jaas.config webapps/ROOT/META-INF/context.xml webapps/ROOT/WEB-INF/classes/spring-config.xml webapps/ROOT/WEB-INF/classes/env.ini; do
			if [ ! -f $file.tmpl ]; then
				echo ERROR: Could not find template $file.tmpl 1>&2
			else
				interpolate $file.tmpl > $file
			fi
		done 
		unset CATALINA_HOME #just in case a developer has it set to something else

		cd bin || exit 1
		CATALINA_OUT=../logs/catalina.out
		rm -rf ${CATALINA_OUT}
		./startup.sh	
		sleep 1

		if [ -s ${CATALINA_OUT} ]; then
                        count="$(wc -l ${CATALINA_OUT})"
                        count="$(echo $count)" # trim
                        count=${count%%${CATALINA_OUT}}
                        if [ "$count" -gt 1 ] || ! expr match "$(cat ${CATALINA_OUT})" 'Listening' > /dev/null; then
                                echo "ERROR: Could not start tomcat. Message is: " 1>&2
                                cat ${CATALINA_OUT} 1>&2
                                exit 1;
                        fi
                fi
	fi
	if ! wait_for_pattern_in_file  "QRM Server Started"  480 "tomcat-server" $QRM_SERVER_BASE_DIR/qrm/logs/qrm.log 1>/dev/null 2>&1; then
		echo "ERROR: Could not start the Qrm-server correctly (timed out). Please check the config"
		exit 1
	fi
	
	touch /var/lock/subsys/qrm-server
	echo "Qrm-server started successfully"
	
}



stop() {
	echo "Stopping the Qrm-server"

	if cd $TOMCAT_HOME/bin; then
		# trying to fix bug 2737 by killing ourselves
		#./shutdown.sh -force
		if [ -f $CATALINA_PID ]; then
			kill `cat $CATALINA_PID` 1>/dev/null 2>&1
			while [ -e /proc/$(cat $CATALINA_PID)/ ]; do
				sleep 1
			done
		fi
	else
		 echo "ERROR: Failed to find tomcat " 1>&2;
	fi 

	killall rmiregistry 2> /dev/null
	
	
	# stop qrm-rexecd
	# should be moved to the hooks.
	killall qrm-rexecd 1>/dev/null 2>&1
	killall qrm-portmon 1>/dev/null 2>&1


	# clear the lockfile
	/bin/rm -f /var/lock/subsys/qrm-server
	echo "Qrm-server stopped successfully"

}


status() {
	if [ -f /var/lock/subsys/qrm-server ]; then
		echo "Qrm-server is running"
	else
		echo "Qrm-server is stopped"
	fi
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status
		;;
	restart)
		stop
		sleep 1
		start
		;;
	*)
		echo "Usage: $0 {start|stop|status|restart}"
		exit 1

esac
exit $?


