#!/bin/bash
#
# (c) Copyright 2011-2016 Hewlett Packard Enterprise Development Company, L.P.
#
# See "man chkconfig" for information on next two lines (Red Hat only)
# chkconfig: - 90 1
# description: BMC AgentX proxy. 
#
#
# Following lines are in conformance with LSB 1.2 spec
### BEGIN INIT INFO
# Provides:            smad
# Default-Start:       2 3 4 5
# Required-Start:      
# Required-Stop:       $syslog
# Default-Stop:        0 1 6
# Description:         starts System Management Assistant BMC Proxy
# Short-Description:   SMA daemon helper
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME="System Management Assistant Service"
SNAME="/sbin/smad"
RNAME="smad_rev"
SMAD_OPTIONS=""

if [ -f /etc/sysconfig/smad ]; then
  . /etc/sysconfig/smad
fi
if [ -f "/etc/default/smad" ]; then
  . /etc/default/smad
fi
if [ -f "/lib/lsb/init-functions" ]; then
  . /lib/lsb/init-functions
fi

sname=`basename $SNAME`

case "$1" in
   start)
      lsmod|grep -q hpilo
      if [ $? -ne 0 ]; then 
         modprobe hpilo
      fi
      ILO_VERSION=`modinfo -F version hpilo`
      if [ "$ILO_VERSION" = "1.4.1" ]; then
          echo "$SNAME the hpilo version to be 1.5.0 or greater."
          echo "  Please upgrade to a more recent kernel"
          exit 1
      fi

      if [ "$ALLOW_CORE" = "y" ]; then 
        mkdir -p /var/log/cores/smad
        echo "/var/log/cores/%e/%p-%s-%t.core" > /proc/sys/kernel/core_pattern
        ulimit -c unlimited
      fi

      if [ -f /sys/module/hpilo/parameters/max_ccb ]; then
          let MAXCCBS=`cat /sys/module/hpilo/parameters/max_ccb`-2
      else
          let MAXCCBS=6
      fi
      let ILODEVS=`lsof +d /dev/hpilo|grep hpilo|wc -l`
      if [ $ILODEVS -ge $MAXCCBS ]; then
         echo "Running $SNAME will exhaust all available iLO devices." 
         echo "Please stop hp-health, hp-snmp-agents or other consumers of iLO devices."
         exit 1
      fi

      pid=`pidof $sname`
      revpid=`pidof $RNAME`

      if [ -n "$pid" ]; then
          echo "$SNAME is already running.  Please stop $SNAME or use 'restart'"
          exit 1
      fi
      $SNAME $SMAD_OPTIONS; RC=$?
      if [ "$RC" -eq "0" ]; then
          [ -d /var/lock/subsys ] && touch /var/lock/subsys/smad
      fi
      exit $RC
   ;;
   stop)
      RC=0;
      killproc $sname > /dev/null 2>&1; RC=$?
      if [ "$RC" -eq "1" ]; then
         [ -d /var/lock/subsys ] && rm -rf /var/lock/subsys/smad
         exit 0
      else
        sleep 3
        killproc $sname > /dev/null 2>&1; RC=$?
        if [ "$RC" -eq "1" ]; then
           [ -d /var/lock/subsys ] && rm -rf /var/lock/subsys/smad
           exit 0
        else
          sleep 3
          killproc -9 $sname > /dev/null 2>&1
            [ -d /var/lock/subsys ] && rm -rf /var/lock/subsys/smad
          exit 0
        fi

      fi    
   ;;
   reload)
   ;;
   restart)
      $0 stop
      sleep 5
      $0 start
   ;;
   status)
      pid=`pidof $sname`
      if [ -n "$pid" ]; then
          echo "$SNAME is running..."
      else
          echo "$SNAME is stopped..."
      fi
   ;;
   *)
     echo "Usage: /etc/init.d/smad {start|stop|restart|status}"
     exit 1
esac

exit 0 
