#!/bin/bash
#
# (c) Copyright 2002 Hewlett-Packard Development Company, L.P.
#
# init file for hp Health Monitor 
#
# processname:hpasmd 

NAME="Proliant System Health Monitor"

PNAME="hpasmd"
#We've got to figure out if we are on a 
#legacy system or a system with embedded health
OD=`which od`
FIND=`which find`
GREP=`which grep`

PFLAGS=""
export RETVAL=0
export PNAME
export PFLAGS
export STARTADDON
export STOPADDON
export STATUSADDON

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


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



DetermineProg() {

   if [ -f /opt/compaq/cpqci/cpcpqci ]; then
      echo ""
      NAME="Proliant iLO 2 Channel Interface System Health Monitor"
      PNAME="hpasmchifd"
      PFLAGS=""

      case "$2" in
      start)
           hpasmdecho "Starting high performance HP iLO 2 Channel Interface device driver"
           /etc/init.d/hprsm start cpqci
           sleep 3
           ;;

      stop)
           hpasmdecho "Stopping high performance HP iLO 2 Channel Interface device driver"
           STOPADDON="/sbin/rmmod cpqci"
           ;;
         
      restart)
           $1 stop
           $1 start
           RETVAL=$?
           ;;

      *)
           STARTADDON="/etc/init.d/hprsm start cpqci"
           STOPADDON="/etc/init.d/hprsm stop cpqci"
           STATUSADDON="/etc/init.d/hprsm status cpqci"
           ;;

      esac

   else

      echo ""
      hpasmdecho "Using standard Linux IPMI device driver and health-lite"
      NAME="Proliant Standard IPMI based System Health Monitor"
      PNAME="hpasmlited"
      PFLAGS="-f /dev/ipmi0"

      STARTADDON="/etc/init.d/ipmi start"
      STOPADDON="/etc/init.d/ipmi  stop"
      STATUSADDON="/etc/init.d/ipmi status"

   fi

}


for d in `${FIND} /proc/bus/pci/ -type f -name "[0-9a-f][0-9a-f].[0-9]"`
do
   ${OD} -x $d | ${GREP} -i "0e11 b203" >/dev/null 2>&1
   if [ $? -eq 0 ]; then
      #We have a management controller, now lets see
      #if it is embedded health
      ${OD} -x $d|${GREP} -ie "103c 3305" >/dev/null 2>&1
      if [ $? -eq 0 ]; then
         DetermineProg $0 $1
      fi
   fi
done

EXTRAMSG="PLEASE WAIT! It may take a few minutes"

if [ "x$CMAINCLUDE" = "x" ]; then
	CMAINCLUDE=/opt/compaq/foundation/etc/cmad
fi

LOGFILE=/var/spool/compaq/hpasmd.log

#precaution only
unset LD_ASSUME_KERNEL
unset LD_LIBRARY_PATH

. $CMAINCLUDE

exit $RETVAL
