#!/bin/bash
#
# ************************************************
# Copyright (c) 2002, Compaq Computer Corporation
# ************************************************
#
# Compaq 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 "/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 "Attempting 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 "/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 "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
		}

	}

}


case "$1" in
	'start')
#
# First make sure we are not already running
#
		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 
		echo "Setting Console Logging to KERN_NOTICE level (5)" >> /opt/compaq/cpqhealth/cpqhealth_boot.log 
		echo 5 >/proc/sys/kernel/printk
		CPQEVT=/opt/compaq/cpqhealth/cpqevt/${THIS_KERNEL}/cpqevt.o
		CPQASM=/opt/compaq/cpqhealth/cpqasm/${THIS_KERNEL}/cpqasm.o

		CPQDRIVER=`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 Compaq Health Event Logging module is not available"
				log_msg "for this Linux kernel:  ${THIS_KERNEL}"
			fi

      	 eval "/sbin/insmod -p ${CPQEVT}" >>/opt/compaq/cpqhealth/cpqhealth_boot.log 2>&1
      	if [ $? -ne 0 ]
      	then
				log_msg " "
				log_msg "The Compaq Health Event Logging module failed to load!"
				log_msg "Linux Kernel Symbol Conflict - Attemping rebuild to resolve."
				REBUILD_MODULE=TRUE
			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 "          Remove and install again the cpqhealth RPM to correct."
					log_msg "          See  /opt/compaq/cpqhealth/cpqhealth_boot.log "
					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 Compaq Health Event Logging module . . ."
		   	/sbin/insmod ${CPQEVT} >>/opt/compaq/cpqhealth/cpqhealth_boot.log 2>&1
			fi

		fi

	   CPQDRIVER=`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 Compaq Advanced Server Management module is not available"
				log_msg "for this Linux kernel:  ${THIS_KERNEL}"
			fi

			 eval "/sbin/insmod -p ${CPQASM}" >>/opt/compaq/cpqhealth/cpqhealth_boot.log 2>&1
			if [ $? -ne 0 ]
			then
				log_msg " "
				log_msg "The Compaq Advanced Server Management  module failed to load!"
				log_msg "Linux Kernel Symbol Conflict - Attemping rebuild to resolve."
				REBUILD_MODULE=TRUE
			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 "          Remove and install again the cpqhealth RPM to correct."
					log_msg "          See  /opt/compaq/cpqhealth/cpqhealth_boot.log "
					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 Compaq Advance Server Management module . . ."
				/sbin/insmod ${CPQASM} >>/opt/compaq/cpqhealth/cpqhealth_boot.log 2>&1
				sh /opt/compaq/cpqhealth/cpqasm/cpqasm_mknod.sh
				/opt/compaq/cpqhealth/cpqasm/casmd &
			fi

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

		echo "Setting Console Logging to KERN_NOTICE level (6)" >> /opt/compaq/cpqhealth/cpqhealth_boot.log 
		echo 6 >/proc/sys/kernel/printk
      sleep 3
      echo -e "\n"
		exit 0
		;;

	'stop')
		echo -e "\n"
		echo "cpqasm:  You should also stop the driver otherwise the Automatic"
		echo "         Server Recovery (ASR) Feature may reboot the server."
		echo "         usage:  rmmod cpqasm"
		/opt/compaq/cpqhealth/cpqasm/casmd  -t
		sleep 3
		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"
		lsmod | grep Module
		lsmod | grep cpqasm
		echo -e "\n"

		ps -x | grep casmd
		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
