#!/bin/bash
# Startup script for swagent
#
# Copyright (c) 2005 by SWsoft
# All rights reserved.
#
# description: swagent provides various access to HN
# chkconfig: 2345 99 17

# This constant is replaced by the build system. In the result script this bool value will reflect 
# the configfuration of the PVA core. If PVA_MN_CORE is 1, then we work for PVA management node core,
# otherwise the core we deal with is for slave agent.    
PVA_MN_CORE=0 # 1

if [[ $PVA_MN_CORE == 1 ]]; then
	installDir=/opt/pva/mn
else
	installDir=/opt/pva/agent
fi

. ${VZA_TMPDIR}/$installDir/install/etc/vzl_defines
. ${VZA_TMPDIR}/$installDir/install/etc/vzl_functions

checkMysqlRunning()
{
	${MYSQLADMIN_BIN} ping >> /dev/null 2>&1 || { echo "# ERROR: MySQL service is not running" && exit -1; }
}


isMounted() {
	$VZCTL status $1 2>/dev/null | grep -q " mounted"	
}

isRunning() {
	$VZCTL status $1 2>/dev/null | grep -q " running"
}

isLdapRunning() {
	${LDAP_INITSCRIPT} status 2>/dev/null | grep -q " running"
}

start ()
{
	if [[ $PVA_MN_CORE == 1 ]]; then 
		checkMysqlRunning
	fi
	startService
}

stop ()
{
	stopService
} 

vzstat ()
{
	status -p /var/run/$VZAGENT_PID_NAME.pid $VZAGENT | sed -e "s/${VZAGENT}/${VZAGENT_SHOW_NAME}/"
	return ${PIPESTATUS[0]}
}

case "$1" in
	start)
		start
		;;
	
	stop)
		stop
		;;
	
	status)
		vzstat
		;;

	restart)
		stop
		start
		;;

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

esac

exit $?
