#!/bin/bash
#
# vcagent script.
#
# description: HP Version Control Agent.
# pidfile: /var/run/vcagent.pid
# chkconfig: 35 53 60
#   35 - levels 3 and 5
#   53 - start after the snmpd (level 50)
#   60 - ??? 

# chkconfig: 345 40 60
# description: HP Version Control Agent

# Source function library.
. /etc/rc.d/init.d/functions

# Path to the server binary, and short-form for messages.
vcapath=/opt/hp/vcagent
vcagent=$vcapath/bin/vcagentd
vcapid=/var/run/vcagent.pid
prog='HP Version Control Agent'
RETVAL=0

mykillproc() {
	kill -TERM $pid > /dev/null 2>&1
	if [ "$?" != "0" ]; then
		return 1;
	fi
	usleep 500000 # sleeps for 0.5 secs
	pid=`pidof $vcagent`
	if [ ! -z "$pid" ]; then
		kill -KILL $pid > /dev/null 2>&1
		if [ "$?" != "0" ]; then
			return 1;
		fi	
	fi
	return 0;
}

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
	echo -n $"Starting $prog: "
	pid=`pidof $vcagent`
	if [ ! -z "$pid" ]; then
		echo_failure
		echo
		return 1
	fi
	cd $vcapath
	export LD_LIBRARY_PATH=$vcapath/lib
	daemon $vcagent
	RETVAL=$?
	cd -
	echo
	[ $RETVAL = 0 ] && touch /var/lock/subsys/vcagent
	return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	pid=`pidof $vcagent`
	if [ -z "$pid" ]; then
		echo_failure
		echo 
		return 1
	fi
	#killproc $vcagent -15
	mykillproc
	RETVAL=$?
	if [ "$RETVAL" = "0" ]; then
	    echo_success
	else
	    echo_failure
	fi
	echo
	[ $RETVAL = 0 ] && rm -f /var/lock/subsys/vcagent /var/run/vcagent.pid
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status $vcagent
		RETVAL=$?
		;;
	restart)
		stop
		start
		;;
	*)
		echo $"Usage: $prog {start|stop|restart|status}"
		exit 1
esac

exit $RETVAL
