#!/bin/sh

# (C) Copyright 2002-2005 Hewlett-Packard Development Company, L.P.
#  RHEL 3, Update 6 start 



case "$1" in
  start)
	echo  " " "Starting hp-OpenIPMI: "
	echo  " " "Starting hp-OpenIPMI: " >> /var/spool/compaq/cma.log

#
# First test to see if IPMI driver is loaded
#

	RUNNING=`lsmod | fgrep ipmi_si_drv`
	if [ ${#RUNNING} -ne 0 ]; then
		echo "Attemting to stop existing IPMI drivers"
		sleep 5
		rmmod ipmi_si_drv
		if [ "$?" -ne 0 ] ; then
			echo "hp-OpenIPMI: Not able to stop ipmi_si"
			exit 1
		fi
	fi

	RUNNING=`lsmod | fgrep ipmi_devintf`
	if [ ${#RUNNING} -ne 0 ]; then
		rmmod ipmi_devintf
		if [ "$?" -ne 0 ] ; then
			echo "hp-OpenIPMI: Not able to stop ipmi_devintf"
			exit 1
		fi
	fi

	RUNNING=`lsmod | fgrep ipmi_msghandler`
	if [ ${#RUNNING} -ne 0 ]; then
		rmmod ipmi_msghandler
		if [ "$?" -ne 0 ] ; then
			echo "hp-OpenIPMI: Not able to stop ipmi_msghandler"
			exit 1
		fi
	fi

	rm -f /dev/ipmi0
	rm -f /dev/ipmi1

	/opt/hp/hp-OpenIPMI/check_install_kernel.sh |tee /opt/hp/hp-OpenIPMI/rebuild.out

	if [ $? != 0 ]; then
		exit 1
	fi
	insmod /opt/hp/hp-OpenIPMI/bin/`uname -r`/ipmi_msghandler.o
	if [ "$?" -ne 0 ] ; then
		echo "hp-OpenIPMI: Not able to start ipmi_msghandler.o"
		echo "hp-OpenIPMI: Not able to start ipmi_msghandler.o" >> /var/spool/compaq/cma.log
		exit 1
	fi

	insmod /opt/hp/hp-OpenIPMI/bin/`uname -r`/ipmi_devintf.o
	if [ "$?" -ne 0 ] ; then
		echo "hp-OpenIPMI: Not able to start ipmi_devintf.o"
		echo "hp-OpenIPMI: Not able to start ipmi_devintf.o" >> /var/spool/compaq/cma.log
		exit 1
	fi

	insmod /opt/hp/hp-OpenIPMI/bin/`uname -r`/ipmi_si_drv.o si_type=kcs si_pci_vend_devs=0x103c3302 si_debug_intfs=0
	if [ "$?" -ne 0 ] ; then
		echo "hp-OpenIPMI: Not able to start ipmi_si_drv.o"
		echo "hp-OpenIPMI: Not able to start ipmi_si_drv.o" >> /var/spool/compaq/cma.log
		exit 1
	fi

	sleep 2

	major=`awk "\\$2==\"ipmidev\" {print \\$1}" /proc/devices`
	mknod /dev/ipmi0 c $major 0 > /dev/null 2>&1
#	mknod /dev/ipmi1 c $major 1
	;;


  stop)
	echo " " "Stopping hp-OpenIPMI: "
	echo " " "Stopping hp-OpenIPMI: " >> /var/spool/compaq/cma.log

#
# Due to a "Chicken and Egg" issue, we need to postpone our removal to allow
# applications that are using the OpenIPMI driver to finish closing their file
# handles.  We will call ourselves in "x" seconds to allow other applications 
# to cleanly stop.  Not sure what we'll do in the case of OEM applications.
#
	if [ -z $2 ]; then
		sh /opt/hp/hp-OpenIPMI/hp-OpenIPMI stop 3&
	else
    		sleep $2

		RUNNING=`lsmod | fgrep ipmi_si_drv`
		if [ ${#RUNNING} -ne 0 ]; then
			rmmod ipmi_si_drv
			if [ "$?" -ne 0 ] ; then
				echo "hp-OpenIPMI: Not able to stop ipmi_si"
				exit 1
			fi
		fi

		RUNNING=`lsmod | fgrep ipmi_devintf`
		if [ ${#RUNNING} -ne 0 ]; then
			rmmod ipmi_devintf
			if [ "$?" -ne 0 ] ; then
				echo "hp-OpenIPMI: Not able to stop ipmi_devintf"
				exit 1
			fi
		fi

		RUNNING=`lsmod | fgrep ipmi_msghandler`
		if [ ${#RUNNING} -ne 0 ]; then
			rmmod ipmi_msghandler
			if [ "$?" -ne 0 ] ; then
				echo "hp-OpenIPMI: Not able to stop ipmi_msghandler"
				exit 1
			fi
		fi

	fi
	;;


  restart)
	exit 0
        ;;
  status)
	echo ""
	echo "hp-OpenIPMI Status:"
	lsmod | grep -e "Module" -e "ipmi"
	IPMIDRIVER=`/sbin/lsmod |fgrep ipmi`
	if [ ${#IPMIDRIVER} -eq 0 ]; then
		echo "WARNING:  The hp-OpenIPMI driver does not appear to be loaded!"
		exit 1
	fi
        ;;
  configure)
	exit 0
        ;;
  reconfigure)
	exit 0
        ;;
  unconfigure)
	exit 0
        ;;

  *)
	exit 1

esac

exit 0
