#!/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

PKGBIN=/opt/compaq/nic/bin
PATH=/opt/compaq/nic/bin:$PATH
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

showsuccess()
{
if [ -f /etc/rc.d/init.d/functions ]; then
  echo_success
elif [ -f /etc/rc.status ]; then
  rc_reset
  rc_status -v
else
  echo -ne "\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
else
  echo -ne "\t\t[ FAILED ]\n"
fi
}

cmaecho () {
  echo "  " $*
  echo "  " $* >>$LOGFILE 2>&1
}

cmaechon () {
  echo -n "  " $*
  echo -n "  " $* >>$LOGFILE 2>&1
}

cmalog () {
  echo " "$* >>$LOGFILE 2>&1
}

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
                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; rm -f $NIC_TMP_FILE)&

        else
          cmaecho "Already started $NAME ($PNAME)." 
          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 
           touch /var/lock/subsys/$PNAME >/dev/null 2>&1
           # success
           showsuccess
	   cmalog "[OK]"
        else
           # failure
	   cmalog "[FAILED]"
           showfailure
        fi
	;;
  stop)
        pidlist=`pidof -o $$ $PNAME`
        if [ -z "$pidlist" ]; then
          cmaecho "Already stopped $NAME ($PNAME)." 
          exit 0
        fi

	cmaechon "Shutting down $NAME ($PNAME): "

        if [ -f /etc/SuSE-release ]; then
	   killproc $PKGBIN/$PNAME >/dev/null 2>&1
        else
	   killproc $PNAME >/dev/null 2>&1
        fi

	RETVAL=$?
        sleep $STOPSLEEP
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$PNAME >/dev/null 2>&1
	cmalog "[OK]"
        # success
        showsuccess
       	;;
  restart)
        $0 stop
        $0 start
	RETVAL=$?
        ;;
  status)
        if [ -f /sbin/checkproc ]; then
           echo -n "Checking for $PNAME: "
           /sbin/checkproc $PKGBIN/$PNAME && echo OK || echo No process
        else
           status $PNAME
        fi
	RETVAL=$?
        ;;
  *)
	cmaecho "Usage: $PNAME {start|stop|restart|status}"
	exit 1
esac

exit $RETVAL
