#!/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"
MOD_VER="1.2"

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
}


case "$1" in
  start)
# silently load the module
        if [  ! -d /sys/module/$MOD ]; then
            modprobe -q $MOD
        fi
#
# test to see if hpilo driver is loaded
#
        if [ -d /sys/module/$MOD ]; then
            # See if the our module is Newer then what is loaded
            if [ "$MOD_VER" != `cat /sys/module/$MOD/version` ]; then
                # Reload the distros latest module and see if is the same
                rmmod $MOD
                modprobe -q $MOD
                if [ "$MOD_VER" = `cat /sys/module/$MOD/version` ]; then
                    cmaechon "Current version already started $MOD module:"
                    showsuccess
                    cmaecho
                    exit 0
                fi
                #remove the loaded module
                rmmod $MOD
            else
                cmaechon "Current version already started $MOD module:"
                showsuccess
                cmaecho
                exit 0
            fi
	fi

        cmaechon "Starting hp-ilo: "

	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
        else 
	  /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
          else
                FLAVOR=`uname -r |cut -f3 -d-`
                RPM=`tail -6 /var/spool/compaq/cma.log|grep Wrote:|grep kmp-$FLAVOR|cut -f2 -d\ `
                rpm -i  $RPM  >> /var/spool/compaq/cma.log 2>&1
                modprobe $MOD
	  fi
	fi

	if  [ ! -d /sys/module/$MOD ]; then
		# 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 [ -d /sys/module/$MOD ]; then
	    COUNT=`cat /proc/modules | grep -m 1 $MOD | cut -d ' ' -f 3`
            if [ "${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
        else
            showfailure
            cmaecho
            cmaecho "hp-ilo: $MOD is not currently loaded."
            exit 0
        fi

	if  [ -d /sys/module/$MOD ]; then
		rmmod "$MOD"
		if [ "$?" -ne 0 ] ; then
                        showfailure
			cmaecho "hp-ilo: Not able to stop $MOD"
			exit 1
		fi
	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  [ -d /sys/module/$MOD ]; 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
