#!/bin/bash
#
# ***********************************************************
# copyright 2002 Compaq Information Technologies Group, L.P.
# ***********************************************************
# 
# The following section is used for insserv (8) which is an
# installation / boot utility available under SuSE and United
# Linux based distributions.  This section has no affect on
# Linux distributions which do not implement the insserv (8)
# installation / boot utility.
#
### BEGIN INIT INFO
# Provides: cpqasm
# Required-Start:	$syslog
# Required-Stop:	$syslog
# Default-Start:	3 5
# Default-Stop:	0 1 2 6
# Description:	Starts / Stops ProLiant Advanced Server Management Driver
### END INIT INFO
#
# hp ProLiant Advanced Server Management Driver
#

# ********************************************************
# ** log_msg                                            **
# ********************************************************
# ** This routine will log a message to the screen and  **
# ** to our error log.                                  **
# ********************************************************
function log_msg() {
	echo "$*" >> /opt/compaq/cpqhealth/cpqhealth_boot.log 
	echo "$*"
}


# ********************************************************
# ** kernel_src_avail                                   **
# ********************************************************
# ** This routine will check to see if various kernel   **
# ** header files are available.  These are required to **
# ** rebuild this module.                               **
# ********************************************************
function kernel_src_avail() {
	THIS_KERNEL=`uname -r`
	log_msg " "
	log_msg "Looking for sources to build ${THIS_KERNEL}"
	ls -ld /lib/modules/${THIS_KERNEL}/build > /dev/null
	[ $? -ne 0 ] &&  {
		log_msg " "
		log_msg "FAILURE:  Not able to rebuild on this kernel!"
		log_msg "/lib/modules/${THIS_KERNEL}/build does not exist"
		log_msg "This is an indication that the sources for this kernel (${THIS_KERNEL}) are not loaded."
		log_msg "Please load the appropriate sources to rebuild module".
		exit 1
	}

#
# We need to make sure that "version.h" exists.  This is a problem on SLES7
#
	ls /lib/modules/${THIS_KERNEL}/build/include/linux/version.h > /dev/null
	[ $? -ne 0 ] && {
		ls /boot/vmlinuz.version.h > /dev/null
		[ $? -eq 0 ] && {
			log_msg " "
			log_msg "Missing  /lib/modules/${THIS_KERNEL}/build/include/linux/version.h"
			log_msg "Making a backup of /lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h to"
			log_msg "                   /lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h.ORIGINAL"
			mv /lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h /lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h.ORIGINAL >> /opt/compaq/cpqhealth/cpqhealth_boot.log 2>&1
			log_msg " "
			cp /boot/vmlinuz.version.h /lib/modules/${THIS_KERNEL}/build/include/linux/version.h
		} || {
			log_msg " "
			log_msg "/lib/modules/${THIS_KERNEL}/build/include/linux/version.h does not exist"
			log_msg "Please load the appropriate sources to rebuild module".
			exit 1
		}

	}

#
# We need to make sure that "autoconf.h" exists.  This is a problem on SLES7
#
   ls /lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h > /dev/null
   [ $? -ne 0 ] && {
      ls /boot/vmlinuz.autoconf.h > /dev/null
      [ $? -eq 0 ] && {
			cp /boot/vmlinuz.autoconf.h /lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h
		} || {
			log_msg " "
			log_msg "FAILURE:  Not able to rebuild on this kernel!"
			log_msg "/lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h does not exist"
			log_msg "Please load the appropriate sources to rebuild module".
			exit 1
		}

	}

#
# We now need to make sure that the version.h file matches the kernel
# we are trying to build.
#
	fgrep ${THIS_KERNEL} /lib/modules/${THIS_KERNEL}/build/include/linux/version.h > /dev/null
	[ $? -ne 0 ] && {
		log_msg " "
		log_msg "/lib/modules/${THIS_KERNEL}/build/include/linux/version.h does not match"
		log_msg "the version of this kernel (${THIS_KERNEL})."
		log_msg " "
		log_msg "This is an indication that a patch has been loaded but not the sources"
		log_msg "to match the running kernel.  This driver requires the sources to all"
		log_msg "kernel patches to be loaded in order to relink to the kernel symbols"

# 
#	We will now try to see if this is a SLES 7 system that might have
#  been patched.
#
		ls /boot/vmlinuz.version.h > /dev/null
		[ $? -eq 0 ] && {
			fgrep ${THIS_KERNEL} /boot/vmlinuz.version.h > /dev/null

			[ $? -eq 0 ] && {
				log_msg " "
				log_msg "This appears to be a patched kernel and the correct files are located in the \"/boot\" directory"
				log_msg " "
				log_msg "Making a backup of /lib/modules/${THIS_KERNEL}/build/include/linux/version.h to"
				log_msg "                   /lib/modules/${THIS_KERNEL}/build/include/linux/version.h.ORIGINAL"
				mv /lib/modules/${THIS_KERNEL}/build/include/linux/version.h /lib/modules/${THIS_KERNEL}/build/include/linux/version.h.ORIGINAL
				log_msg " "
				log_msg "Making a backup of /lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h to"
				log_msg "                   /lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h.ORIGINAL"
				mv /lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h /lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h.ORIGINAL
				log_msg " "
				log_msg "Copying /boot/vmlinuz.version.h to /lib/modules/${THIS_KERNEL}/build/include/linux/version.h"
				cp /boot/vmlinuz.version.h /lib/modules/${THIS_KERNEL}/build/include/linux/version.h

				log_msg "Copying /boot/vmlinuz.autoconf.h to /lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h"
				cp /boot/vmlinuz.autoconf.h /lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h

			} || {
				log_msg " "
				log_msg "FAILURE:  Not able to rebuild on this kernel!"
				log_msg "There does not appear to be kernel sources which match the current booting Linux kernel"
				log_msg "There must be a directory named \"/lib/modules/${THIS_KERNEL}\" and there must be a"
				log_msg "valid directory linked to \"/lib/modules/${THIS_KERNEL}/build\"."
				log_msg "Please load the appropriate Linux sources to rebuild module".
				exit 1
			}

		} || {
			exit 1
		}

	}

	log_msg " "
	log_msg "Sources for Linux kernel ${THIS_KERNEL} have been located"
	log_msg " "
}


case "$1" in
	'start')
#
# First make sure we are not already running
#
		MYPID=`ps -x | grep cevtd | awk '{ print $5 }'`
		for i in $MYPID
		do
			if [ "$i" != "grep" ]
			then
				/etc/init.d/cpqasm stop
			fi
		done

		MYPID=`ps -x | grep casmd | awk '{ print $5 }'`
		for i in $MYPID
		do
			if [ "$i" != "grep" ]
			then
				echo "WARNING: cpqasm: casmd already running!"
				echo "         You must stop the process first."
				echo "         usage:  /etc/init.d/cpqasm stop"
				exit 1
			fi
		done

		cp /opt/compaq/cpqhealth/cpqhealth_boot.log /opt/compaq/cpqhealth/cpqhealth_boot.log.old >/dev/null 2>&1
		MYDATE=`date`
		THIS_KERNEL=`uname -r`
		REBUILD_MODULE=FALSE
		echo "cpqhealth driver loading at ${MYDATE} on Linux kernel:  ${THIS_KERNEL}" > /opt/compaq/cpqhealth/cpqhealth_boot.log 
		CPQEVT=/opt/compaq/cpqhealth/cpqevt/${THIS_KERNEL}/cpqevt.o
		CPQASM=/opt/compaq/cpqhealth/cpqasm/${THIS_KERNEL}/cpqasm.o

		CPQDRIVER=`/sbin/lsmod |fgrep cpqevt`
	   if [ ${#CPQDRIVER} -eq 0 ]
		then
			if [ ! -d /opt/compaq/cpqhealth/cpqevt/${THIS_KERNEL} ]
			then
				REBUILD_MODULE=TRUE
				log_msg " "
				log_msg "The hp ProLiant Event Logging module is not available"
				log_msg "for this Linux kernel:  ${THIS_KERNEL}"

			else
      	 	eval "/sbin/insmod -p ${CPQEVT}" >>/opt/compaq/cpqhealth/cpqhealth_boot.log 2>&1
      		if [ $? -ne 0 ]
      		then
					log_msg " "
					log_msg "Linux Kernel Symbol Conflict - Attempting rebuild to resolve."
					REBUILD_MODULE=TRUE
				fi

			fi

			if [ "${REBUILD_MODULE}" == "TRUE" ]
			then
				kernel_src_avail
				cd /opt/compaq/cpqhealth/cpqevt
				/sbin/rmmod cpqevt > /dev/null 2>&1
				log_msg " "
				make -s -f cpqevt.mk rebuild | tee -a /opt/compaq/cpqhealth/cpqhealth_boot.log

				if [ $? -ne 0 ]
				then
					log_msg " "
					log_msg "WARNING!  Not able to rebuild the cpqevt.o module on this kernel!"
					log_msg "          See  /opt/compaq/cpqhealth/cpqhealth_boot.log "
					log_msg "          for error details."
					log_msg " "
					log_msg "          The ProLiant ASM driver has failed to load!"
					exit 1
				fi

				log_msg " "
				sh retry_cpqevt.sh

				if [ $? -ne 0 ]
				then
					log_msg " "
					log_msg "WARNING!  The cpqhealth RPM has failed to install!"
					exit 1
				fi

      	else
				log_msg "Loading the hp ProLiant Event Logging module . . ."
		   	/sbin/insmod ${CPQEVT} >>/opt/compaq/cpqhealth/cpqhealth_boot.log 2>&1
			fi

		fi

	   CPQDRIVER=`/sbin/lsmod |fgrep cpqasm`
	   if [ ${#CPQDRIVER} -eq 0 ]
		then
			if [ ! -d /opt/compaq/cpqhealth/cpqasm/${THIS_KERNEL} ]
			then
				REBUILD_MODULE=TRUE
				log_msg " "
				log_msg "The hp ProLiant Advanced Server Management module is not available"
				log_msg "for this Linux kernel:  ${THIS_KERNEL}"

			else
			 	eval "/sbin/insmod -p ${CPQASM}" >>/opt/compaq/cpqhealth/cpqhealth_boot.log 2>&1
				if [ $? -ne 0 ]
				then
					log_msg " "
					log_msg "Linux Kernel Symbol Conflict - Attempting rebuild to resolve."
					REBUILD_MODULE=TRUE

				fi

			fi

			if [ "${REBUILD_MODULE}" == "TRUE" ]
			then
				kernel_src_avail
				cd /opt/compaq/cpqhealth/cpqasm
				log_msg " "
				/sbin/rmmod cpqasm > /dev/null 2>&1
				make -s -f cpqasm.mk rebuild | tee -a /opt/compaq/cpqhealth/cpqhealth_boot.log

				if [ $? -ne 0 ]
				then
					log_msg " "
					log_msg "WARNING!  Not able to rebuild the cpqasm.o module on this kernel!"
					log_msg "          See  /opt/compaq/cpqhealth/cpqhealth_boot.log "
					log_msg "          for error details."
					log_msg " "
					log_msg "          The ProLiant ASM driver has failed to load!"
					exit 1
				fi

				log_msg " "
				sh retry_cpqasm.sh

				if [ $? -ne 0 ]
				then
					log_msg " "
					log_msg "WARNING!  The cpqhealth RPM has failed to install!"
					exit 1
				fi

      	else
				log_msg "Loading the hp ProLiant Advance Server Management module . . ."
				/sbin/insmod ${CPQASM} >>/opt/compaq/cpqhealth/cpqhealth_boot.log 2>&1
				if [ $? -eq 0 ]
				then
					sh /opt/compaq/cpqhealth/cpqasm/cpqasm_mknod.sh
					/opt/compaq/cpqhealth/cpqevt/cevtd &
					/opt/compaq/cpqhealth/cpqasm/casmd &
					log_msg " "
					log_msg "The ProLiant Advanced Server Management module has been "
               log_msg "loaded sucessfully for this Linux kernel:  ${THIS_KERNEL}"
					log_msg " "
				fi

			fi

		else
			/opt/compaq/cpqhealth/cpqevt/cevtd &
			/opt/compaq/cpqhealth/cpqasm/casmd &
		fi

      sleep 3
      echo -e "\n"
		exit 0
		;;

	'stop')
		echo -e "\n"
		echo "cpqasm:  Stopping ProLiant Advanced Server Management Driver"
		/opt/compaq/cpqhealth/cpqasm/casmd  -t
		/opt/compaq/cpqhealth/cpqevt/cevtd  -t
		sleep 3
		/sbin/rmmod cpqasm
		/sbin/rmmod cpqevt
		sleep 3
 
		CPQASM=`/sbin/lsmod | /usr/bin/fgrep cpqasm`
		if [ ${#CPQASM} -ne 0 ]; then
				echo "WARNING!  Not able to stop the cpqasm device driver!"
				echo "          The CIM agents may not be terminated."
		exit 1
		fi
		exit 0
        	;;

	'status')
		echo -e "\n"
		echo "Here are the modules / daemons executing:"
		MYPID=`ps -x | grep casmd | awk '{ print $5 }'`
		declare -i PIDCNT
		PIDCNT=0
		for i in $MYPID
		do
			if [ "$i" != "grep" ]
			then
				PIDCNT=$PIDCNT+1
			fi
		done

		echo -e "\n"
		/sbin/lsmod | grep Module
		/sbin/lsmod | grep cpqasm
		echo -e "\n"

		ps -x | grep casmd
		ps -x | grep cevtd
		echo -e "\n"

		case $PIDCNT in
			0 )
				echo "WARNING:  The Health Driver is not operational!"
				echo -e "\n"
				exit 1
				;;

			1 )
				echo "WARNING:  The Health Driver is operating in a degraded state!"
				echo -e "\n"
				exit 1
				;;

			2 )
				echo "NOTICE:  The Health Driver is operating normally."
				echo -e "\n"
				;;

			* )
				echo "WARNING:  The Health Driver is operating in an unknown state!"
				echo -e "\n"
				exit 1
				;;
		esac

		exit 0
        	;;

	*)
		echo "Usage: $0 [ start | stop | status ]"
		exit 1
		;;
esac

exit 0
