#!/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/hpasmd.log
fi


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



DetermineProg() {

   if [ -f /opt/hp/hp-OpenIPMI/hp-OpenIPMI ]; then
      echo ""
      hpasmdecho "Using high performance hp-OpenIPMI device driver"
      NAME="Proliant High Performance IPMI based System Health Monitor"
      PNAME="hpasmxld"
      PFLAGS="-f /dev/ipmi0"

      STARTADDON="/opt/hp/hp-OpenIPMI/hp-OpenIPMI start"
      STOPADDON="/opt/hp/hp-OpenIPMI/hp-OpenIPMI stop"
      STATUSADDON="/opt/hp/hp-OpenIPMI/hp-OpenIPMI status"
   else
      echo ""

      # We need to special case VMware. We need to check that IPMI
      # support is present by looking at the "/etc/init.d" direcotry
      # to see if the IPMI service is present.  If so, we will assume
      # that we have an IPMI driver that understands the PCI BMC and
      # try to load that driver with the si_pci_vend_devs parameter.

      VMWARE=`uname -r | grep vmnix`

      if [ ${#VMWARE} -ne 0 ]; then

         hpasmdecho "Using standard VMWare IPMI device driver and hpasmxld"
         NAME="Proliant High Performance IPMI based System Health Monitor"
         PNAME="hpasmxld"
         PFLAGS="-f /dev/ipmi0"

         # Here we need to cover for the inadequacies of Linux and their
         # stock IPMI driver. We need to force the issue and tell the 
         # startup script what interface type and what port or the ipmi
         # device node will not be created. If they ever get their act
         # straight then we can remove this sed statement and change the 
         # ensuing ipmi.hp calls to simply, ipmi. Remember, the options
         # are different between 2.4 and 2.6 kernels.
         # NOTE:  VMware did pick up the extra debug for the interfaces
         #        which can be enabled by setting debug_intfs=1 below.

         kernel=`uname -r | cut -d. -f1-2`
         if [ "${kernel}" == "2.4" ]; then
            sed 's/modprobe ${IPMI_SI_MODULE_NAME}/sleep 3\n\t\tmodprobe ${IPMI_SI_MODULE_NAME} si_type=kcs si_pci_vend_devs=0x103c3302 si_debug_intfs=0/' /etc/init.d/ipmi > /etc/init.d/ipmi.hp
         else
            sed 's/modprobe ${IPMI_SI_MODULE_NAME}/modprobe ${IPMI_SI_MODULE_NAME} type=kcs pci_vend_devs=0x103c3302 debug_intfs=0/' /etc/init.d/ipmi > /etc/init.d/ipmi.hp
         fi

      else
         # This is not VMware so must be a standard Linux driver
         hpasmdecho "Using standard Linux IPMI device driver and health-lite"
         NAME="Proliant Standard IPMI based System Health Monitor"
         PNAME="hpasmlited"
         PFLAGS="-f /dev/ipmi0"

         # Here we need to cover for the inadequacies of Linux and their
         # stock IPMI driver. We need to force the issue and tell the 
         # startup script what interface type and what port or the ipmi
         # device node will not be created. If they ever get their act
         # straight then we can remove this sed statement and change the 
         # ensuing ipmi.hp calls to simply, ipmi. Remember, the options
         # are different between RH3 and RH4.
         kernel=`uname -r | cut -d. -f1-2`
         if [ "${kernel}" == "2.4" ]; then
            sed 's/modprobe ${IPMI_SI_MODULE_NAME}/sleep 3\n\t\tmodprobe ${IPMI_SI_MODULE_NAME} si_type=kcs si_ports=0xca2/' /etc/init.d/ipmi > /etc/init.d/ipmi.hp
         else
            sed 's/modprobe ${IPMI_SI_MODULE_NAME}/modprobe ${IPMI_SI_MODULE_NAME} type=kcs ports=0xca2/' /etc/init.d/ipmi > /etc/init.d/ipmi.hp
         fi
      fi

      chmod 755 /etc/init.d/ipmi.hp
      STARTADDON="/etc/init.d/ipmi.hp start"
      STOPADDON="/etc/init.d/ipmi.hp  stop"
      STATUSADDON="/etc/init.d/ipmi.hp 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
      fi
   fi
done

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

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


#precaution only
unset LD_ASSUME_KERNEL
unset LD_LIBRARY_PATH
unset VMWARE

. $CMAINCLUDE

exit $RETVAL
