#! /bin/sh
### BEGIN INIT INFO
# Provides:
# Required-Start: network
# Required-Stop:
# Default-Start:   3 5
# Default-Stop:
# Description:     HP Version Control Agent
### END INIT INFO

# Source function library.
. /etc/rc.status

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

test -x $vcagent || exit 1

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
        rc_failed 1
        rc_status -v
        return 1
	fi
	cd $vcapath
	export LD_LIBRARY_PATH=$vcapath/lib
	startproc $vcagent
    rc_status -v
}

stop() {
	echo -n $"Stopping $prog: "
	pid=`pidof $vcagent`
	if [ -z "$pid" ]; then
        rc_failed 1
        rc_status -v
        return 1
	fi
	#killproc $vcagent -15
	mykillproc
    rc_status -v
}

# See how we were called.
rc_reset
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
	        echo -n 'Checking for process vcagent: '
		checkproc $vcagent
		rc_status -v
		;;
	restart)
		stop
		start
		rc_status
		;;
	*)
		echo $"Usage: $prog {start|stop|restart|status}"
		exit 1
esac

rc_exit
