#!/bin/bash
# Description: Startup script for 3PAR Host Explorer 
# $Id: redhat.init,v 1.3 2009/09/15 23:55:13 pambati Exp $
#
# $Copyright: $
#

# chkconfig: 2345 83 25
# description: 3PAR Host Explorer 
# processname: hagent
# pidfile: /var/run/hagent.pid

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

# set the varaible to yes, for debug information
# uncomment the following line 
# HAGENT_DEBUG=yes
# export HAGENT_DEBUG

lockdir=/etc/3par/hostexplorer/lock
disp_str="3PAR Host Explorer"
hagent=/usr/sbin/hagent
prog=${hagent##*/}
prog_name="TpdHostAgentSvc"
RETVAL=0

logmsg()
{
	$hagent -g "$@"
}

findproc() {
	pid=`/bin/ps -e -o pid,comm |
	/bin/grep " $1" | /bin/grep -v grep |
	/bin/awk '{ print $1 }'`
}

start() {
	# check always before starting
	start_hagent=1
	findproc hagent
	if [ "X$pid" != "X" ]; then
                start_hagent=0
		echo -n "$disp_str is already running ..."
		logmsg "$disp_str is already running ..."
		echo
                RETVAL=1
        fi
	# start now
	if [ "$start_hagent" -eq 1 ]; then
        	echo -n $"Starting $disp_str: "
        	logmsg "Starting $disp_str. "
        	daemon $hagent
		echo
		sleep 5
		findproc hagent;
       		if [ -z "$pid" ] || [ "$pid" -eq 0 ]; then
                	echo -n "Error starting $disp_str"
                	logmsg "Error starting $disp_str"
               		echo_failure
			echo
                	return 99
       		 fi

	fi
	
        [ $RETVAL = 0 ] && touch /var/lock/subsys/hagent
        return $RETVAL
}
status() {
	findproc hagent;
	if [  "X$pid" != "X" ]; then
		logmsg "3PAR Host Explorer is running ..."
		echo -n $"3PAR Host Explorer is running ..."
		echo
	else
		logmsg "3PAR Host Explorer is not running."
		echo -n $"3PAR Host Explorer is not running."
		echo
	fi
}
stop() {
        echo -n $"Stopping $disp_str: "
        killproc $hagent
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/hagent /var/run/hagent.pid
        logmsg $"Stopped $disp_str: "
}
reload() {
        echo -n $"Reloading $disp_str: "
        killproc $hagent -HUP
        RETVAL=$?
        echo
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status $hagent
	RETVAL=$?
        ;;
  restart)
        stop
        sleep 5
        start
        ;;
  reload)
	reload
	;;
  *)
	export prog;
	echo -n $"Usage: $prog_name { start | stop | restart | status | push}"
	echo
        exit 1
esac

exit $RETVAL
