#!/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
   [ $? != 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
	[ $? != 0 ] && {
		ls /boot/vmlinuz.version.h > /dev/null
		[ $? -eq 0 ] && {
			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
   [ $? != 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
	[ $? != 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')
		cp /opt/compaq/cpqhealth/cpqhealth_boot.log /opt/compaq/cpqhealth/cpqhealth_boot.log.old >/dev/null 2>&1
      echo 5 >/proc/sys/kernel/printk
		MYDATE=`date`
		echo "cpqhealth driver loading at ${MYDATE}" > /opt/compaq/cpqhealth/cpqhealth_boot.log 
		echo "Setting Console Logging to KERN_NOTICE level (5)" >> /opt/compaq/cpqhealth/cpqhealth_boot.log 
		OS_SMP=`uname -a | grep SMP`
		OS_ENT=`uname -r | grep enterprise`
		if [ ${#OS_SMP} -eq 0 ]; then
			CPQEVT=/lib/modules/Compaq/drivers/up/cpqevt.o
			CPQASM=/lib/modules/Compaq/drivers/up/cpqasm.o
		else
			if [ ${#OS_ENT} -eq 0 ]; then
				CPQEVT=/lib/modules/Compaq/drivers/smp/cpqevt.o
				CPQASM=/lib/modules/Compaq/drivers/smp/cpqasm.o
			else
				CPQEVT=/lib/modules/Compaq/drivers/ent/cpqevt.o
				CPQASM=/lib/modules/Compaq/drivers/ent/cpqasm.o
			fi

		fi

	   CPQDRIVER=`lsmod |fgrep cpqevt`
	   if [ ${#CPQDRIVER} -eq 0 ]
		then
      	if eval "/sbin/insmod -p ${CPQEVT}" >>/opt/compaq/cpqhealth/cpqhealth_boot.log 2>&1
      	then
				log_msg "Loading the Compaq Health Event Logging module . . ."
		   	/sbin/insmod ${CPQEVT} >>/opt/compaq/cpqhealth/cpqhealth_boot.log 2>&1
      	else
				log_msg " "
				log_msg "The Compaq Health Event Logging module failed to load!"
				log_msg "Linux Kernel Symbol Conflict - Attemping rebuild to resolve"
            kernel_src_avail
				cd /opt/compaq/cpqhealth/cpqevt
				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!  Kernel source files not found or do not match the booting kernel."
					log_msg "          See /opt/compaq/cpqhealth/cpqhealth_boot.log for details."
					log_msg "          For more information please refer to the installation documentation."

            	exit 1
      		fi
				log_msg " "
				sh retry_cpqevt.sh
            if [ $? -ne 0 ]
				then
					log_msg " "
					log_msg "WARNING!  Kernel source files not found."
					log_msg "          For more information please refer to the installation documentation."	

            	exit 1
      		fi
      	fi
	   fi

	   CPQDRIVER=`lsmod |fgrep cpqasm`
	   if [ ${#CPQDRIVER} -eq 0 ]
		then
			log_msg "Loading the Compaq Advanced Server Management module . . ."
      	if eval "/sbin/insmod -p ${CPQASM}"  >>/opt/compaq/cpqhealth/cpqhealth_boot.log 2>&1
      	then
		   	/sbin/insmod ${CPQASM} >>/opt/compaq/cpqhealth/cpqhealth_boot.log 2>&1
         	sh /opt/compaq/cpqhealth/cpqasm/cpqasm_mknod.sh
         	/opt/compaq/cpqhealth/cpqasm/casmd &
      	else
				log_msg " "
				log_msg "The Compaq Advanced Server Management module failed to load!"
				log_msg "Linux Kernel Symbol Conflict - Attemping rebuild to resolve"
         	kernel_src_avail
				cd /opt/compaq/cpqhealth/cpqasm
				log_msg " "
				make -s -f cpqasm.mk rebuild | tee -a  /opt/compaq/cpqhealth/cpqhealth_boot.log
         	if [ $? -ne 0 ]
				then
					log_msg " "
					log_msg "WARNING!  Kernel source files not found or do not match the booting kernel."
					log_msg "          See /opt/compaq/cpqhealth/cpqhealth_boot.log for details."
					log_msg "          For more information please refer to the installation documentation."
            	exit 1
      		fi
				log_msg " "
				sh retry_cpqasm.sh
          	if [ $? -ne 0 ]
				then
					log_msg " "
					log_msg " "
					log_msg "WARNING!  Kernel source files not found."
					log_msg "          For more information please refer to the installation documentation."	
           		exit 1
      		fi
      	fi
		else
        	/opt/compaq/cpqhealth/cpqasm/casmd &
		fi
      sleep 3
      echo -e "\n"
		exit 0
		;;

	'stop')
      /opt/compaq/cpqhealth/cpqasm/casmd  -t
		sleep 3
		exit 0
        	;;

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

exit 0
