#!/bin/bash
# hp ProLiant Memory Hotplug driver 
#

BOOTLOG=/opt/compaq/cmhp/cmhp_boot.log
THIS_KERNEL=`uname -r`
NOTAINTEDKERNEL=`grep notaint /opt/compaq/cma.conf | cut -d' ' -f2 | tr [a-z] [A-Z]`

# ********************************************************
# ** log_msg                                            **
# ********************************************************
# ** This routine will log a message to the screen and  **
# ** to our error log.                                  **
# ********************************************************
function log_msg() {
	echo "$*" >> $BOOTLOG
	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() {
	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 >> $BOOTLOG 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')
                if [ "$NOTAINTEDKERNEL" = "YES" ]; then
                   exit 0
                fi
		EXPNAMES=`egrep "^exclude" /opt/compaq/cma.conf 2>/dev/null | cut -b 8- 2> /dev/null`
		if [ ! -z "`echo $EXPNAMES | grep cmhp`" ]; then
			exit 0
		fi
		
		MYDATE=`date`
		THIS_KERNEL=`uname -r`
		REBUILD_MODULE=FALSE
		echo "cmhp driver loading at ${MYDATE} on Linux kernel:  ${THIS_KERNEL}" >> $BOOTLOG 
		CMHP=/opt/compaq/cmhp/bin/${THIS_KERNEL}/cmhp.o

	   	if [ "`lsmod | grep cmhp`" == "" ]
		then
			if [ ! -d /opt/compaq/cmhp/bin/${THIS_KERNEL} ]
			then
				REBUILD_MODULE=TRUE
				log_msg " "
				log_msg "The hp ProLiant Memory Hotplug module is not available"
				log_msg "for this Linux kernel:  ${THIS_KERNEL}"

			else
				eval "/sbin/insmod -p ${CMHP}" >>$BOOTLOG 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/cmhp/bin
				/sbin/rmmod cmhp> /dev/null 2>&1
				log_msg " "
				make -f Makefile TARGET=cmhp TARGETLIB=$THIS_KERNEL all install >> $BOOTLOG 2>&1

				if [ $? -ne 0 ]
				then
					log_msg " "
					log_msg "WARNING!  Not able to rebuild the cmhp.o module on this kernel!"
					log_msg "          See  $BOOTLOG "
					log_msg "          for error details."
					log_msg " "
					log_msg "          The ProLiant Memory Hotplug driver has failed to load!"
					exit 1
				fi

				log_msg " "
			fi

			sh /opt/compaq/cmhp/bin/retry_cmhp.sh
			if [ $? -ne 0 ]
			then
				log_msg " "
				log_msg "WARNING!  The cmhp driver has failed to load!"
				exit 1
			fi
			sh /opt/compaq/cmhp/bin/mknod_cmhp.sh
			if [ $? -ne 0 ]
			then
				log_msg " "
				log_msg "WARNING!  The cmhp driver has failed to load!"
				exit 1
			fi

		fi
		exit 0
		;;

	'stop')
                if [ "$NOTAINTEDKERNEL" = "YES" ]; then
                   exit 0
                fi
                rm -f /dev/cmhp
		/sbin/rmmod cmhp 2> /dev/null
 
		if [ "`/sbin/lsmod | fgrep cmhp`" != "" ]; then
			echo "WARNING!  Not able to stop the cmhp device driver!"
			echo "          The CIM agents may not be terminated."
			exit 1
		fi
		exit 0
        	;;

	'status')
                if [ "$NOTAINTEDKERNEL" = "YES" ]; then
                   exit 0
                fi
		if [ "`/sbin/lsmod | fgrep cmhp`" != "" ]; then
			/sbin/lsmod | grep Module
			/sbin/lsmod | grep cmhp
		fi
		exit 0
        	;;

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

exit 0
