#!/bin/bash

# cmanic init file for NIC Agents
#
# See "man chkconfig" for information on next two lines
# chkconfig: - 99 1
# description: NIC Agents - Linux
#
# comments for insserv
### BEGIN INIT INFO
# Provides: cmanic
# Required-Start: hpasm
# Required-Stop: hpasm
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Start the NIC Agents for Linux
### END INIT INFO
#

# 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
fi

RETVAL=0

PATH=/opt/compaq/nic/bin:$PATH
LOGFILE=/var/spool/compaq/cma.log
NAME="NIC Agents"
ETCROOT="/opt/compaq/nic/etc"
PNAME="cmanic"
PNAMES="cmanicd"
SNMPD="snmpd"
USAGE="Usage: $PNAME {start|stop|status|restart|condrestart} [cmanicd]..."

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

###########################################################################
cmausage () {
  cmaecho $USAGE
}

###########################################################################
driver_check () {

        tg3_loaded=`/sbin/lsmod | grep tg3`
        if [ ! -z "$tg3_loaded" ]; then

           msg1="WARNING: "
           msg2="Unsupported tg3 driver loaded, please use the "
           msg3="tested and supported bcm5700"
           msg4="driver from:"
           msg5="http://h18000.www1.hp.com/products/servers/linux/softwaredrivers.html"

           cmaecho "$msg1"
           cmaecho "$msg2""$msg3"
           cmaecho "$msg4"
           cmaecho "$msg5"
        fi


        eepro100_loaded=`/sbin/lsmod | grep eepro100`
        if [ ! -z "$eepro100_loaded" ]; then

           msg1="WARNING: "
           msg2="Unsupported eepro100 driver loaded, please use the "
           msg3="tested and supported e100"
           msg4="driver from:"
           msg5="http://h18000.www1.hp.com/products/servers/linux/softwaredrivers.html"

           cmaecho "$msg1"
           cmaecho "$msg2""$msg3"
           cmaecho "$msg4"
           cmaecho "$msg5"
        fi
} 


###########################################################################
start () {
        snmppid=`pidof $SNMPD`
        if [ -z "$snmppid" ]; then
	  cmaecho
          cmaecho "Can not start $PNAME! ($SNMPD is not running)"
	  RETVAL=1
        else
          if [ -z "$AGENTS" ]; then #start all agents
	    cmaecho "Starting $NAME ($PNAME): All agents"
            for AGENT in $PNAMES; do
              [ -x $ETCROOT/$AGENT ] && (cd /; $ETCROOT/$AGENT start)
            done
          else
	    cmaecho "Starting $NAME ($PNAME): $AGENTS"
            for AGENT in $AGENTS; do
              [ -x $ETCROOT/$AGENT ] && (cd /; $ETCROOT/$AGENT start)
            done
          fi
	  touch /var/lock/subsys/$PNAME >/dev/null 2>&1
        fi
	cmaecho
        driver_check
}

###########################################################################
stop () {
        if [ -z "$AGENTS" ]; then #stop all agents
	  cmaecho "Shutting down $NAME ($PNAME): All agents"
          for AGENT in $PNAMES; do
            [ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT stop
          done
        else
	  cmaecho "Shutting down $NAME ($PNAME): $AGENTS"
          for AGENT in $AGENTS; do
            [ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT stop
          done
        fi
        REMOVELOCK=1
        for AGENT in $PNAMES; do
          pidlist=`pidof -o $$ $AGENT`
          if [ ! -z "$pidlist" ]; then
            REMOVELOCK=0
            break;
          fi
        done
	[ $REMOVELOCK = "1" ] && rm -f /var/lock/subsys/$PNAME >/dev/null 2>&1
	cmaecho
}

###########################################################################
restart () {
        if [ -z "$AGENTS" ]; then #restart all agents
	  cmaecho "Re-starting $NAME ($PNAME): All agents"
          for AGENT in $PNAMES; do
            [ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT restart
          done
        else
	  cmaecho "Re-starting $NAME ($PNAME): $AGENTS"
          for AGENT in $AGENTS; do
            [ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT restart
          done
        fi
	cmaecho
}

###########################################################################
status () {
        if [ -z "$AGENTS" ]; then #status all agents
          for AGENT in $PNAMES; do
            [ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT status
          done
        else
          for AGENT in $AGENTS; do
            [ -x $ETCROOT/$AGENT ] && $ETCROOT/$AGENT status
          done
        fi
        driver_check

	RETVAL=$?
}


###########################################################################
condrestart () {
    [ -e /var/lock/subsys/$PNAME ] && restart
    return 0
} 

###########################################################################
if [ "$#" -lt 1 ]; then
  cmausage
  exit 1
fi

OPCODE=$1
shift
AGENTS=$*

for i in $AGENTS; do
  FOUND=0
  for j in $PNAMES; do
    if [ $i = $j ]; then
      FOUND=1
      break;
    fi
  done
  if [ $FOUND = "0" ]; then
    cmausage
    exit 1
  fi
done

# echo "OPCODE = $OPCODE, AGENTS = $AGENTS"

case "$OPCODE" in
  start)
        start
	;;
  stop)
        stop
	;;
  restart)
        restart
        ;;
  status)
        status
        ;;
  condrestart)
        condrestart
        ;;
  *)
	cmausage
	exit 1
esac

exit $RETVAL
