#!/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
      # 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`
      THIS_RELEASE=`uname -r`
      if [ "${kernel}" == "2.4" ]; then
         PCI_BMC=`modinfo -p /lib/modules/${THIS_RELEASE}/kernel/drivers/char/ipmi/ipmi_si_drv.o | grep pci_vend_devs`

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

            sed 's/modprobe ${IPMI_SI_MODULE_NAME}/modprobe ${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
            hpasmdecho "Using standard Linux IPMI device driver and hpasmxld"
            NAME="Proliant High Performance IPMI based System Health Monitor"
            PNAME="hpasmxld"
            PFLAGS="-f /dev/ipmi0"

         else

            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
            hpasmdecho "Using standard Linux IPMI device driver and hpasm-lite"
            NAME="Proliant Standard IPMI based System Health Monitor"
            PNAME="hpasmlited"
            PFLAGS="-f /dev/ipmi0"

         fi

      else

         PCI_BMC=`modinfo -p /lib/modules/${THIS_RELEASE}/kernel/drivers/char/ipmi/ipmi_si.ko | grep pci_vend_devs`
         if [ ${#PCI_BMC} -ne 0 ]; then
            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
            hpasmdecho "Using standard Linux IPMI device driver and hpasmxld"
            NAME="Proliant High Performance IPMI based System Health Monitor"
            PNAME="hpasmxld"
            PFLAGS="-f /dev/ipmi0"

         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
            hpasmdecho "Using standard Linux IPMI device driver and hpasm-lite"
            NAME="Proliant Standard IPMI based System Health Monitor"
            PNAME="hpasmlited"
            PFLAGS="-f /dev/ipmi0"

         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
