#!/bin/bash
#
# (c) Copyright 2005 Hewlett-Packard Development Company, L.P.
# init file for NIC Agent Daemon
#
# processname: cmanicd
#
# 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

RETVAL=0
STARTUP_UNKNOWN=0

if [ -d /opt/compaq/nic/bin ]; then 
   PKGBIN=/opt/compaq/nic/bin
   PATH=/opt/compaq/nic/bin:$PATH
else
   PKGBIN=/opt/hp/hp-snmp-agents/nic/bin
   PATH=/opt/hp/hp-snmp-agents/nic/bin:$PATH
fi
LOGFILE=/var/spool/compaq/cma.log
NIC_TMP_FILE=/var/spool/compaq/.nic.tmp.$$
NIC_WAIT_LOOP=5
NAME="NIC Agent Daemon"
PNAME="cmanicd"
PFLAGS=
UPSTRING=
DOWNSTRING=
STOPSLEEP=1
#Added addpidintofile,delagentfile for QXCR1000850218

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

#add pid into agent file
addpidintofile() {
    PIDOFAGNT=`$PIDOF -o $$ -o $PPID -o %PPID -x $PNAME`
    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
}

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
echo
}

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
}

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

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

cmalog () {
  printf " $*\n" >> "$LOGFILE"
}
#Added for QXCR1000850218

if [ -d /opt/hp/hp-snmp-agents ]; then
	EXPNAMES="`egrep "^exclude" /opt/hp/hp-snmp-agents/cma.conf 2>/dev/null | cut -b 8- 2> /dev/null`"
else
	EXPNAMES=`egrep "^exclude" /opt/compaq/cma.conf 2>/dev/null | cut -b 8- 2> /dev/null`
fi
if [ -n "`echo $EXPNAMES | grep $PNAME`" ]; then
	if [ "$1" = "start" -o "$1" = "stop" -o "$1" = "status" ]; then
		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)

        pidlist=`pidof -o $$ $PNAME`
        if [ -z "$pidlist" ]; then

	  cmaechon "Starting $NAME ($PNAME): "
          # remove any old tmp files
          rm -f /var/spool/compaq/.nic.tmp.*
          tail -0f $LOGFILE > $NIC_TMP_FILE &
          tail_proc=$!

	  if [ -n "$UPSTRING" ]; then
                UOPT="-U"
                USTRING=$UPSTRING
          fi
          if [ -n "$DOWNSTRING" ]; then
                DOPT="-D"
                DSTRING=$DOWNSTRING
          fi
          $PNAME $PFLAGS $UOPT "$USTRING" $DOPT "$DSTRING">> $LOGFILE 2>&1 &
	  RETVAL=$?

          # make sure the agents start; if they don't, inform user
          #
          # if we see "ERROR: cmanic could not start" the nic agent did not
          # start... if we see "cmanic started" it started
          loop_count=0
          while [ $loop_count -lt $NIC_WAIT_LOOP ]; do
             grep "cmanic started" $NIC_TMP_FILE >/dev/null 2>&1
             cmanic_started=$?
             grep "ERROR: cmanic could not start" $NIC_TMP_FILE >/dev/null 2>&1
             cmanic_failed=$?
 
             if [ $cmanic_started -eq 0 ]; then
                # we started
        	touch $AGNTFILEPATH/$PNAME.pid
		addpidintofile
	 	break;
             fi
 
             if [ $cmanic_failed -eq 0 ]; then
                # failed to start agent, inform user
                cmaecho " "
                cat $NIC_TMP_FILE
                cat $NIC_TMP_FILE >>$LOGFILE 2>&1
                RETVAL=1
                break;
 
             fi 

             loop_count=$((loop_count+1))
             sleep 1
          done
 
          if [ $loop_count -eq $NIC_WAIT_LOOP ]; then
             # just a double check... 
             pidlist=`pidof -o $$ $PNAME`
             if [ -z "$pidlist" ]; then
                STARTUP_UNKNOWN=1
             fi
          fi
 
          # kill the tial of NIC_TMP_FILE
          #
          # kill $tail_proc &> /dev/null
          # rm -f $NIC_TMP_FILE
          #
          # the below is a work-around to prevent the following error
          # from showing up on the console (window) when the above command
          # is run...
          # .../cmanicd: line X: 32160 Terminated
          #                            tail -0f $LOGFILE >$NIC_TMP_FILE
          #
          # we kill tail_proc after this script exits to prevent the
          # message from spewing to the screen... if there is a better way,
          # let me know -tonycu
          # 
          (sleep 3; kill $tail_proc > /dev/null 2>&1; rm -f $NIC_TMP_FILE)&

        else
          cmaechon "Already started $NAME ($PNAME):" 
          showsuccess
	  cmaecho
	  exit 0
        fi

	if [ $STARTUP_UNKNOWN -eq 1 ]; then 
           cmaecho "Unable to determine if cmanic successfully started"
           exit 0
        fi
        
	if [ $RETVAL -eq 0 ]; then 
           # success
           showsuccess
           cmaecho
	   cmalog "[OK]"
        else
           # failure
	   cmalog "[FAILED]"
           showfailure
        fi
	;;
  stop)
        #
        # added line to insert blank line on display
        # there are cases where line is overwriiten 
        # so fix by forcing line to always start at the
        # beginning of new line 
        #
        cmaecho " "
        pidlist=`pidof -o $$ $PNAME`
        if [ -z "$pidlist" ]; then
             cmaechon "Shutting down $NAME ($PNAME):"
	     showfailure 
	     cmaecho
          exit 0
        fi

	cmaechon "Shutting down $NAME ($PNAME): "
        if [ -f /etc/SuSE-release ]; then
	   killproc $PKGBIN/$PNAME >/dev/null 2>&1
        else
	   if [ -f /etc/redhat-release ];then
		   killproc $PNAME >/dev/null 2>&1
	   else
		   pkill -o $PNAME >/dev/null 2>&1
	   fi
        fi

	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
	   delagentfile
	fi
        sleep $STOPSLEEP
	cmalog "[OK]"
        # success
        showsuccess
        cmaecho
       	;;
  restart)
        $0 stop
        $0 start
	RETVAL=$?
        ;;
  status)
	cmaecho
        if [ -f /sbin/checkproc ]; then
           	cmaechon "$PNAME is ..."
           	/sbin/checkproc $PKGBIN/$PNAME
	   	rc_status -v
	else
		found=0
		plist=`ps -C cmanicd -o pid=`
		if [ $? -eq 0 ];then
			for p in $plist
			do
				exe=`ls -l /proc/$p/exe | awk '{print $NF}'`
				if [ "$exe" = "$PKGBIN/$PNAME" ]
				then
					found=1;
					cmaechon "$PNAME is running..."
					showsuccess
					break;
				fi
			done
		fi
		#Added for QXCR1000850218
		if [ $found -eq 0 ]; then
			if [ -f $AGNTFILEPATH/$PNAME.pid ]; then
		    		cmaechon "$PNAME is dead..."
				showfailure
			else
				cmaechon "$PNAME is stopped..."
				showsuccess
			fi
			cmaecho
		fi
        fi
	RETVAL=$?
        ;;
  *)
	cmaecho "Usage: $PNAME {start|stop|restart|status}"
	exit 1
esac

exit $RETVAL
