#!/bin/bash
#
# (c) Copyright 2002 Hewlett-Packard Development Company, L.P.
#

# source function library
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
elif [ -f /etc/rc.config ]; then
. /etc/rc.config
elif [ -f /etc/rc.d/functions ]; then
. /etc/rc.d/functions
elif [ -f /etc/rc.status ]; then
. /etc/rc.status
fi


if [ -z "$LOGFILE" ]; then
LOGFILE=/var/spool/compaq/cma.log
fi

AGNTFILEPATH=/var/run

#add pid into agent file
addpidintofile() {
		PIDOFAGNT=`$PIDOF -o $$ -o $PPID -o %PPID -x $PNAME > /dev/stdout | cut -d " " -f1` 2> /dev/null		
		if [ -f $AGNTFILEPATH/$PNAME.pid ]; then
			echo "$PIDOFAGNT" > $AGNTFILEPATH/$PNAME.pid
		fi
}		

#delete agent file
delagentfile () {
	if [ -f $AGNTFILEPATH/$PNAME.pid ]; then
		rm -f $AGNTFILEPATH/$PNAME.pid 2> /dev/null	
	fi			
}		

#different distros put pidof in different places
if [ -f /sbin/pidof ]; then
  PIDOF=/sbin/pidof
elif [ -f /bin/pidof ]; then
  PIDOF=/bin/pidof
fi

#NAME="agent name"
#PNAME="cmaagentd"
#PFLAGS=

#chkhttpserverconfig () {
#  unset HPWEBSETUP
#  if [ -x /opt/compaq/foundation/etc/hpwebsetup ]; then
#    /opt/compaq/foundation/etc/hpwebsetup -q: >/dev/null 2>&1
#    if [ $? != 4 ]; then
#      cmaerr "ERROR: Can't start the Web agent. HP HTTP server must be configured"
#      cmaerr "       first. The minimum configuration required is the \"administrator\""
#      cmaerr "       password which can be configured with following command:"
#      cmaerr "       /opt/compaq/foundation/etc/hpwebsetup -pA:'yourpasswd' -f"
#    fi
#  fi
#  return 0
#}

cmaerr () {
  printf "  $*\n" | tee -a "$LOGFILE" 1>&2
}

cmaecho () {
  printf "  $*\n" | tee -a "$LOGFILE"
}

cmaechon () {
  printf "  $*" | tee -a "$LOGFILE"
}

showsuccess()
{
if [ -f /etc/rc.d/init.d/functions ]; then
  echo_success
elif [ -f /etc/rc.status ]; then
  rc_reset
  rc_status -v
else
  printf "\t\t[ SUCCESS ]\n"
fi

}
showfailure()
{
if [ -f /etc/rc.d/init.d/functions ]; then
   echo_failure
elif [ -f /etc/rc.status ]; then
   rc_failed
   rc_status -v
else
  printf "\t\t[ FAILED ]\n"
fi
}

stopproc () {
	rc=1
	pidlist=`$PIDOF -o $$ $1`
	if [ -n "$pidlist" ]; then
           rc=2
  	   for i in `seq 1 25`; do
		if [ -z "$pidlist" ]; then
			rc=0	
			break
		fi
		if [ "$i" = "1" ]; then
			kill $pidlist 2>/dev/null
		elif [ "$i" = "22" ]; then
			kill -s 9 $pidlist 2>/dev/null
		else
			sleep 1 
		fi
		pidlist=`$PIDOF -o $$ $1`
	   done
        fi
	return $rc
}

checkformodule()
{
   rc=0
   CIUP=`lsmod | fgrep -e "$1"`
   if [ -z "$CIUP" ]; then
       if [ -f $2 ]; then
          $2 start
       else
          cmaechon "$NAME ($PNAME) requires $1"
          rc=1
       fi
   fi
   return $rc
}

RETVAL=0
if [ -d /opt/hp/hp-snmp-agents ]; then
   PATH=/opt/hp/hp-snmp-agents/server/bin:/opt/hp/hp-snmp-agents/storage/bin:/opt/hp/hp-snmp-agents/nic/bin:$PATH
   EXCLUDES="`egrep "^exclude" /opt/hp/hp-snmp-agents/cma.conf 2>/dev/null | cut -b 8- 2> /dev/null`"
else
   PATH=/opt/compaq/foundation/bin:/opt/compaq/server/bin:/opt/compaq/hpasmd/bin:/opt/compaq/storage/bin:$PATH
   EXCLUDES=`egrep "^exclude" /opt/compaq/cma.conf 2>/dev/null | cut -b 8- 2> /dev/null`
fi
#RHEL3 U3 did not like this, if LD_ASSUME_KERNEL needs to be set do it
#in the individual scripts 
#[ -z "$LD_ASSUME_KERNEL" ] && export LD_ASSUME_KERNEL=2.4.1

if [ -n "`echo $EXCLUDES | grep $PNAME`" ]; then
   if [ "$1" = "start" -o "$1" = "stop" -o "$1" = "status" ]; then
      cmaecho
      cmaechon "NOTE: $NAME ($PNAME) is excluded "
      if [ -f /etc/rc.status ]; then
        rc_status -s
      else
        printf "\t\t   [SKIPPED]\n"    
      fi
      exit 0
   fi
fi

case "$1" in
  start)
	$STARTADDON	
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		touch $AGNTFILEPATH/$PNAME.pid 		
		pidlist=`$PIDOF -o $$ $PNAME`
		if [ -z "$pidlist" ]; then
		        cmaechon "Starting $NAME ($PNAME): "
	  		$PNAME $PFLAGS < /dev/null > /dev/null 2>&1 &
			addpidintofile
	  		RETVAL=$?
                else
		        cmaechon "Already started $NAME ($PNAME): "
		fi
	fi
        ;;
  stop)
	pidlist=`$PIDOF -o $$ $PNAME`
	if [ -z "$pidlist" ]; then
		cmaechon "Already stopped $NAME ($PNAME): "
		RETVAL=0
	else
		cmaechon "Shutting down $NAME ($PNAME): "
		stopproc $PNAME 
		delagentfile
		RETVAL=$?
#        if [ -n "$STOPADDON" ]; then
#	   cmaecho
#        fi
		$STOPADDON
#	cmaecho
	fi
	;;
  restart)
	$0 stop
	$0 start
	RETVAL=$?
	;;
  status)
      
        if [ -n "$STATUSADDON" ]; then
	   cmaecho
        fi
	$STATUSADDON
	cmaecho
        if [ -f /etc/rc.status ]; then
           cmaechon "$PNAME is ..."
           pid=`$PIDOF -o $$ -o $PPID -o %PPID -x $PNAME`
           if [ -n "$pid" ]; then
			   rc_status -v
		   elif [ -f $AGNTFILEPATH/$PNAME.pid ]; then
		       showfailure
		   else
		       checkproc $PNAME 2> /dev/null
			   rc_status -v
		   fi
           exit 0
        else
           pid=`$PIDOF -o $$ -o $PPID -o %PPID -x $PNAME`
           if [ -n "$pid" ]; then
                   cmaechon "$PNAME is running..."
		   elif [ -f $AGNTFILEPATH/$PNAME.pid ]; then	
				   cmaechon "$PNAME is dead..."
		           showfailure		   
           else
                   cmaechon "$PNAME is stopped..."
           fi
	   RETVAL=$?
        fi
	;;
  *)
	cmaecho "Usage: $PNAME {start|stop|restart|status}"
	exit 1
esac


if [ $RETVAL -eq 0 ]; then
showsuccess
else
showfailure
fi

if [ "$1" = "start" -o "$1" = "stop" -o "$1" = "status" ]; then
cmaecho
fi

exit $RETVAL
