################################################################################
# Filename: ./src/plugins/qrm-ha/include/qrm-ha-functions
# 
# 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:
################################################################################
# for testing
QRMHAIP1_1=192.168.88.3
QRMHAIP1_2=192.168.88.33
QRMHAIP2_1=192.168.88.4
QRMHAIP2_2=192.168.88.44
HOSTNAME=`hostname`
if [ "$HOSTNAME" == "node3" ]; then
	ifconfig eth0:xha down
	ifconfig eth0:xha 192.168.88.33 netmask 255.255.255.0 up
elif  [ "$HOSTNAME" == "node4" ]; then
	ifconfig eth0:xha down
	ifconfig eth0:xha 192.168.88.44 netmask 255.255.255.0 up
fi



# configures xha.conf
function qrm_configure_xha() {
	if [ ! -f etc/xha.conf.org ]; then
		/bin/cp -f etc/xha.conf etc/xha.conf.org
	fi

	# configure xha.conf
	cat etc/xha.conf.org | grep -B2000 -w ^node | grep -v ^node > etc/xha.conf

	echo "node = $QRMHAIP1_1, $QRMHAIP1_2" >> etc/xha.conf
	echo "node = $QRMHAIP2_1, $QRMHAIP2_2" >> etc/xha.conf

	cat etc/xha.conf.org | grep -A2000 -w ^node | grep -v ^node >> etc/xha.conf
	cat etc/xha.conf | sed -e "s/^app.*/app = xha_seamless_agent/g" \
		| sed -e "s/^file.*/#file =/g" \
		| sed -e "s/^ip_takeover*/#ip_takeover =/g" \
		| sed -e "s#sample#etc/callbacks#g" > /tmp/xha.conf.tmp
	/bin/mv /tmp/xha.conf.tmp etc/xha.conf


	# the callbacks
	mkdir -p etc/callbacks
	/bin/cp -f sample/* etc/callbacks/
	# actDn.sh
	cat >> etc/callbacks/actDn.sh << EOF
		wget -q -O /dev/null 'http://localhost/monitor/events?type=XHA&event=qrm-server-state-changed' &
		/etc/init.d/qrm-server stop
		mount -o remount,ro $QRM_SERVER_BASE_DIR/qrm/

EOF
	# actUp.sh
	cat >> etc/callbacks/actUp.sh << EOF
		wget -q -O /dev/null 'http://localhost/monitor/events?type=XHA&event=qrm-server-state-changed' &
EOF
	# regAct.sh
	cat >> etc/callbacks/regAct.sh << EOF
		wget -q -O /dev/null 'http://localhost/monitor/events?type=XHA&event=qrm-server-state-changed' &
EOF
	# regApp.sh
	cat >> etc/callbacks/regApp.sh << EOF
		wget -q -O /dev/null 'http://localhost/monitor/events?type=XHA&event=qrm-server-state-changed' &
EOF
	# regPsv.sh
	cat >> etc/callbacks/regPsv.sh << EOF
		wget -q -O /dev/null 'http://localhost/monitor/events?type=XHA&event=qrm-server-state-changed' &
EOF
	# unregAct.sh
	cat >> etc/callbacks/unregAct.sh << EOF
		wge -q -O /dev/null 'http://localhost/monitor/events?type=XHA&event=qrm-server-state-changed' &
EOF
	# unregApp.sh
	cat >> etc/callbacks/unregApp.sh << EOF
		wget -q -O /dev/null 'http://localhost/monitor/events?type=XHA&event=qrm-server-state-changed' &
EOF
	# unregPsv.sh
	cat >> etc/callbacks/unregPsv.sh << EOF
		wget -q -O /dev/null 'http://localhost/monitor/events?type=XHA&event=qrm-server-state-changed' &
EOF
	chmod +x etc/callbacks/*

}

function qrm_ha_install() {
	qrm_configure_xha

}

function qrm_ha_uninstall() {
	:
}

function qrm_ha_start() {
	:
}

function qrm_ha_stop() {
	:
}

function qrm_ha_run_once() {
	:
}
