#!/bin/bash
################################################################################
# Filename: ./src/plugins/qrm-ha/etc/init.d/qrm-server-ha
# 
# 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-ha init script
#
# chkconfig: 2345 97 23
# description: Qrm-server-ha init script

QRM_SERVER_BASE_DIR=`readlink /etc/init.d/qrm-server | sed -e "s#/qrm/etc/init.d/qrm-server##g"`
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
export XHA_DIR="$QRM_SERVER_BASE_DIR/qrm/plugins/xha"
export CONFIG_DIR="$XHA_DIR/etc/"
export XHA_BIN="$XHA_DIR/bin"
export LD_LIBRARY_PATH="$XHA_DIR/lib"
export LOG_DIR=/tmp/xha
export LOG_SYSLOG=false
export LOG_LEVEL=NOTIFY
export XHA_MODULES_DIR=none


# memory loaded functions
. $QRM_SERVER_BASE_DIR/qrm/include/qrm-server-functions
. $QRM_SERVER_BASE_DIR/qrm/include/qrm-functions

qrm_get_server_config 1>/dev/null

start() {
	echo "Starting the Qrm-server ha-plugin"
	# be sure to not start the regular server but the the active-up call back do this
	qrm_chkconfig del qrm-server
	# starting xha-seamless agent
	cd $XHA_DIR/bin && ./xha.rc start
	cd $XHA_DIR/bin && ./xha_seamless.sh
	touch /var/lock/subsys/qrm-server-ha

	# getting active / xeamless-agent now daemonized
	# mount read-writeable
	mount -o remount,rw $QRM_SERVER_BASE_DIR/qrm/
	# start the regular qrm-server
	/etc/init.d/qrm-server start

}



stop() {
	echo "Stopping the Qrm-server ha-plugin"
	# stopping xha-seamless agent
	killall	xha_seamless_agent 1>/dev/null 2>&1
	# wait for the callback
	sleep 5
	# stop xha
	cd $XHA_DIR/bin && ./xha.rc stop
	/bin/rm -f /var/lock/subsys/qrm-server
}


status() {
	if [ -f /var/lock/subsys/qrm-server-ha ]; then
		echo "Qrm-server-ha is running"
	else
		echo "Qrm-server-ha 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 $?


