#!/bin/sh
# (C) Copyright 2007 Hewlett-Packard Development Company, L.P.
#############################################################################
#
# hp-ilo:	Provice HP Channel Interface Driver
#
#
# chkconfig: 2345 90 1
# description: hpilo Driver init script
#
### BEGIN INIT INFO
# Provides: hp-ilo
# Required-Start: $local_fs $remote_fs $syslog
# Required-Stop: $local_fs $remote_fs $syslog
# Default-Start:
# Default-Stop:
# Short-Description: hpilo Driver init script
# Description: hpilo Driver init script
### END INIT INFO
#
# Status return code bits
# no bits set = no errors
# bit 0 set = minimum modules aren't loaded
# bit 1 set = requested feature module isn't loaded
# bit 2 set = /dev/ipmi0 (or /dev/imb if using that instead) doesn't exist
# bit 3 set = /dev/watchdog doesn't exist
# bit 4 set = lockfile doesn't exist
# bit 5 set = modules are loaded even when asked to be unloaded

#
#############################################################################
# source function library
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
elif [ -f /etc/rc.config ]; then
. /etc/rc.config
elif [ -f /etc/rc.d/functions ]; then
. /etc/rc.d/functions
elif [ -f /etc/rc.status ]; then
. /etc/rc.status
fi


if [ ! -d /var/spool/compaq ]; then
	mkdir -p /var/spool/compaq
fi

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

MOD="hpilo"
LOADED_MARKER="/var/run/hp-ilo.loaded"

cmaerr () {
  printf "  $*\n" >&2
  printf "  $*\n" >>$LOGFILE 2>&1
}

cmaecho () {
  printf "  $*\n" | tee -a "$LOGFILE" 2>&1
}

cmaechon () {
  printf "  $*" | tee -a "$LOGFILE" 2>&1
}

showsuccess()
{
if [ -f /etc/rc.d/init.d/functions ]; then
  echo_success
elif [ -f /etc/rc.status ]; then
  rc_reset
  rc_status -v
else
  printf "\t\t[ SUCCESS ]\n"
fi

}
showfailure()
{
if [ -f /etc/rc.d/init.d/functions ]; then
   echo_failure
elif [ -f /etc/rc.status ]; then
   rc_failed
else
  printf "\t\t[ FAILED ]\n"
fi
}

hpilo_loaded() {
	lsmod | cut -d" " -f1 | grep -q "^${MOD}"
}

#
# The following helper functions are used to keep track of
# whether or not it was us that loaded the driver.
# The policy we've elected to implement is that a "stop"
# only unloads the module if it was loaded via a "start".
# If, for example, the distro hpilo gets loaded by udev,
# this script should be a noop.
#
mark_loaded() {
	touch "$LOADED_MARKER"
}

unmark_loaded() {
	rm -f "$LOADED_MARKER"
}

i_loaded_it() {
	test -f "$LOADED_MARKER"
}

case "$1" in
  start)
#
# First test to see if hpilo driver is loaded
#
	if hpilo_loaded; then
		cmaechon "Already started $MOD module:"
                showsuccess
                cmaecho
		exit 0
	fi

        cmaechon "Starting hp-ilo: "

	modprobe -q "$MOD"
	if hpilo_loaded; then
		# RHEL5.3 has a module which does not support newer devices,
		# so module will load, but will not be usable.  In that case,
		# we need to load our out of tree module.
		if [ -d /dev/hpilo ]; then 
			mark_loaded
			cmaechon "Loaded distribution-provided $MOD module:"
			showsuccess
			cmaecho
			exit 0
		else
			rmmod "$MOD"
		fi
	fi
	
	/opt/hp/hp-ilo/check_install_kernel.sh >> /var/spool/compaq/cma.log

	if [ $? != 0 ]; then
                showfailure
                cmaecho
		cmaecho "Check installed kernel failed"
		exit 1
	fi

	insmod /opt/hp/hp-ilo/bin/`uname -r`/${MOD}.ko 2>/tmp/hptest
	if [ "$?" -eq -1 ] ; then
           if [ "`sed -e 's/.*\(File exists\)/\1/' /tmp/hptest`" != "File exists" ]; then
                showfailure
                cmaecho
		cmaecho "hp-ilo: Not able to start ${MOD}.ko"
		exit 1
          fi
	fi

	if hpilo_loaded; then
		mark_loaded
	else
		# Should never get here
		cmaecho
		cmaecho "hp-ilo: Error loading $MOD module"
		exit 1
	fi

	sleep 2

	if [ ! -d /dev/hpilo ]; then 
		mkdir /dev/hpilo > /dev/null 2>&1
		major=`awk "\\$2==\"hpilo\" {print \\$1}" /proc/devices`
		for i in $(seq 0 7)
			do mknod /dev/hpilo/d0ccb$i c $major $i > /dev/null 2>&1
		done
	fi

	;;


  stop)
	cmaechon "Stopping hp-ilo: "

#	if [ -f /etc/init.d/hpasm ] ; then
#		sh /etc/init.d/hpasm stop
#	fi

	COUNT=`cat /proc/modules | grep -m 1 $MOD | cut -d ' ' -f 3`
	if [ "${COUNT}" == "" ];then 
                showfailure
                cmaecho
                cmaecho "hp-ilo: $MOD is not currently loaded."
                exit 0
        elif [ "${COUNT}" != "0" ]; then
                showfailure
                cmaecho
		cmaecho "hp-ilo: $MOD is being used by another application. Try 'sh /etc/init.d/hp-snmp-agents stop' to terminate related applications."
		exit 1
	fi

	if ! i_loaded_it; then
		showfailure
		cmaecho
		cmaecho "hp-ilo: The $MOD module was loaded by another mechanism."
		cmaecho "hp-ilo: This script will not attempt to unload it."
		cmaecho "hp-ilo: You can manually unload the $MOD driver with the rmmod command"
		exit 0
	fi


	if hpilo_loaded; then
		rmmod "$MOD"
		if [ "$?" -ne 0 ] ; then
                        showfailure
			cmaecho "hp-ilo: Not able to stop $MOD"
			exit 1
		else
			unmark_loaded
		fi
	fi

	sleep 2
	
	if [  -d /dev/hpilo ]; then
		rm -rf /dev/hpilo > /dev/null 2>&1
	fi

	;;


  restart)
        $0 stop
        if [ "$?" -eq "0" ]; then
           $0 start
        fi
        exit 0
        ;;
  status)
	echo "Status of hp-ilo:"
	echo "Status of hp-ilo:" >> $LOGFILE
	if hpilo_loaded; then
		cmaecho "$MOD driver is loaded..."
        else   
                cmaecho "$MOD driver is NOT loaded..."
	fi
        exit 0
        ;;
  configure)
	exit 0
        ;;
  reconfigure)
	exit 0
        ;;
  unconfigure)
	exit 0
        ;;

  *)
        cmaecho "Usage: /etc/init.d/hp-ilo {start|stop|restart|status}"
	exit 1

esac

showsuccess
cmaecho
exit 0
