#!/bin/sh
chatter() {
	if test "$1" = "verbose"; then
		echo $2
	fi
}

startup_helper() {
	if [ -f /opt/compaq/cpqrid/drivers/$1.${MOD_EXT} ]; then
		log_msg "Loading the hp ProLiant Remote Insight Board Driver . . ."
		/sbin/insmod /opt/compaq/cpqrid/drivers/$1.${MOD_EXT} >> $HPRSM_LOGFILE 2>&1
		chatter $2 "..$1 started."
	fi
}

stopit_helper() {
	log_msg "Stopping the hp ProLiant Remote Insight Board Driver . . ."
	if eval "/sbin/rmmod $1 > /dev/null 2> /dev/null"; then
		chatter $2 "..$1 stopped."
		log_msg "..$1 stopped."
	else
                LS_MOD=`lsmod | fgrep $1`
                if [ ${#LS_MOD} -eq 0 ]; then
                        exit 0
                fi
		chatter $2 "..$1 busy."
		log_msg "..$1 busy."
		exit -1
	fi
}

stopit() {
	stopit_helper ${CRID_DRIVER} $1
	rm -f /var/lock/subsys/cpqrid
	rm -rf /dev/crid
        rm -rf /dev/cpqhealth/crid
}

startup() {
	startup_helper ${CRID_DRIVER} $1
	touch /var/lock/subsys/cpqrid
	mknod /dev/crid c 237 0 >/dev/null 2>&1
	mkdir -p /dev/cpqhealth >/dev/null 2>&1
        mknod /dev/cpqhealth/crid c 237 0 >/dev/null 2>&1
	chatter $1 "...done. Please type 'man cpqrid' for more information."
}

kernel_src_avail() {
	THIS_KERNEL=`uname -r`
	echo "Looking for sources to build ${THIS_KERNEL}"
	ls -ld /lib/modules/${THIS_KERNEL}/build > /dev/null
	[ $? != 0 ] && {
		echo "/lib/modules/${THIS_KERNEL}/build does not exist"
		echo "This is an indication that the sources for this kernel (${THIS_KERNEL}) are not loaded."
		echo "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
                } || {
                        echo "/lib/modules/${THIS_KERNEL}/build/include/linux/version.h does not exist"
                        echo "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
                } || {
                        echo "/lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h does not exist"
                        echo "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 ] && {
                echo "/lib/modules/${THIS_KERNEL}/build/include/linux/version.h does not match"
                echo "the version of this kernel (${THIS_KERNEL})."
                echo "This is an indication that a patch has been loaded but not the sources"
                echo "to match the running kernel.  This driver requires the sources to all"
                echo "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 ] && {
                                echo "This appears to be a patched kernel and the correct files are located in the \"/boot\" directory"
                                echo "Making a backup of /lib/modules/${THIS_KERNEL}/build/include/linux/version.h to"
                                echo "                   /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
                                echo "Making a backup of /lib/modules/${THIS_KERNEL}/build/include/linux/autoconf.h to"
                                echo "                   /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
                                echo "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

                                echo "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

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

                } || {
                        exit 1
                }

        }

}

rebuild() {
        MACH=`uname -m`
        if [ "$MACH" = "x86_64" ]; then
                export OS_MEM="-mcmodel=kernel"
        fi
	if [ "$K_VER" = "6" ]; then
		echo "Setting up kernel sources to build custom cpqrid driver"
		cd /usr/src/linux
		make mrproper >/dev/null 2>&1
                cp /boot/config-${OS_VER} .config
                make oldconfig >/dev/null 2>&1
		make prepare-all >/dev/null 2>&1
		echo "Building custom cpqrid driver"
		cd /opt/compaq/cpqrid/custom
		make clean > /dev/null 2>/dev/null
		make > /dev/null 2>/dev/null
                if [ "$MACH" = "x86_64" ]; then
                        make -e LD_OPT=elf_x86_64 TARGETDIR=./ all > /dev/null 2>/dev/null
                else
                        make -e TARGETDIR=./ all > /dev/null 2>/dev/null
                fi
	else
		kernel_src_avail
		cd /opt/compaq/cpqrid/custom
		make -f linux.mk clean all > /dev/null 2> /dev/null
	fi
}

copyorfixup() {
	if [ "$REBUILD_MODULE" = "FALSE" ]; then
		if [ "$MOD_EXT" = "o" ]; then
			if eval "insmod -p /opt/compaq/cpqrid/drivers/${OS_SMP}/$1.${MOD_EXT} > /dev/null 2> /dev/null"; then
				cp /opt/compaq/cpqrid/drivers/${OS_SMP}/$1.${MOD_EXT} /opt/compaq/cpqrid/drivers/$1.${MOD_EXT} >/dev/null 2>&1
				echo "$OS_VER" > /opt/compaq/cpqrid/drivers/kernel.txt
			elif eval "insmod -p /opt/compaq/cpqrid/custom/$1.${MOD_EXT} > /dev/null 2> /dev/null"; then
				cp /opt/compaq/cpqrid/custom/$1.${MOD_EXT} /opt/compaq/cpqrid/drivers/$1.${MOD_EXT}
				echo "$OS_VER" > /opt/compaq/cpqrid/drivers/kernel.txt
			else
				LS_MOD=`lsmod | fgrep $1`
				if [ ${#LS_MOD} -eq 0 ]; then

					echo "Linux Kernel Symbol Conflict - Attempting to rebuild to resolve"
					log_msg "Linux Kernel Symbol Conflict - Attempting to rebuild cpqrid to resolve."
					REBUILD_MODULE=TRUE
				fi
			fi
		else
			/sbin/insmod /opt/compaq/cpqrid/drivers/${OS_SMP}/$1.${MOD_EXT} > /dev/null 2> /dev/null
			LS_MOD=`lsmod | fgrep $1`
			if [ ${#LS_MOD} -eq 0 ]; then
				/sbin/insmod /opt/compaq/cpqrid/custom/${OS_SMP}/$1.${MOD_EXT} > /dev/null 2> /dev/null
				LS_MOD=`lsmod | fgrep $1`
				if [ ${#LS_MOD} -eq 0 ]; then
					echo "Linux Kernel Symbol Conflict - Attempting to rebuild to resolve"
					log_msg "Linux Kernel Symbol Conflict - Attempting to rebuild cpqrid to resolve"
					REBUILD_MODULE=TRUE
				else
					cp /opt/compaq/cpqrid/custom/$1.${MOD_EXT} /opt/compaq/cpqrid/drivers/$1.${MOD_EXT}
					echo "$OS_VER" > /opt/compaq/cpqrid/drivers/kernel.txt
				fi
			else
				cp /opt/compaq/cpqrid/drivers/${OS_SMP}/$1.${MOD_EXT} /opt/compaq/cpqrid/drivers/s$1.${MOD_EXT} >/dev/null 2>&1
				echo "$OS_VER" > /opt/compaq/cpqrid/drivers/kernel.txt
			fi
		fi
		MODINFO="`modinfo -d /opt/compaq/cpqrid/drivers/$1.${MOD_EXT} 2>/dev/null`"
		if [[  ! -z "$MODINFO" && "$MODINFO" != "<none>" && "${REBUILD_MODULE}" = "FALSE" ]]
		then
			echo "$MODINFO" | tr -d '"' | md5sum --check --status 2> /dev/null > /dev/null
			if [ $? -ne 0 ]
			then
				log_msg "Linux Kernel Checksum Conflict - Attempting rebuild to resolve."
				echo "Linux Kernel Checksum Conflict - Attempting rebuild to resolve."
				rm -f /opt/compaq/cpqrid/drivers/$1.${MOD_EXT}
				REBUILD_MODULE=TRUE
			fi
		fi
	fi
	
	if [ "${REBUILD_MODULE}" = "TRUE" ]
        then
		rebuild
		if [ -f /opt/compaq/cpqrid/custom/$1.${MOD_EXT} ]; then
			if eval "insmod -p /opt/compaq/cpqrid/custom/$1.${MOD_EXT} > /dev/null 2> /dev/null"; then
				echo "Rebuild successful."
				log_msg "Rebuild of cpqrid successful."
				cp /opt/compaq/cpqrid/custom/$1.${MOD_EXT} /opt/compaq/cpqrid/drivers/$1.${MOD_EXT} > /dev/null 2> /dev/null
				echo "$OS_VER" > /opt/compaq/cpqrid/drivers/kernel.txt
			else
				LS_MOD=`lsmod | fgrep $1`
				if [ ${#LS_MOD} -eq 0 ]; then
					echo "cpqrid rebuilt, but failed to load."
					log_msg "cpqrid failed to load after rebuild."
				else
					echo "Rebuild successful."
					log_msg "Rebuild of cpqrid successful."
					cp /opt/compaq/cpqrid/custom/$1.${MOD_EXT} /opt/compaq/cpqrid/drivers/$1.${MOD_EXT} > /dev/null 2> /dev/null
				fi
			fi
		else
			echo "Failed to rebuild for custom kernel."
			log_msg "Rebuild of cpqrid failed."
		fi
	fi
}

log_msg() {
        echo "$*" >> $HPRSM_LOGFILE
}

HPRSM_LOGFILE=/opt/compaq/hprsm/hprsm_boot.log
PREBUILT_UP=`cat /opt/compaq/cpqci/driver/up/kernel.txt`
PREBUILT_SMP=`cat /opt/compaq/cpqci/driver/smp/kernel.txt`
PREBUILT_ENT=`cat /opt/compaq/cpqci/driver/ent/kernel.txt`
if [ -f /opt/compaq/cpqrid/drivers/kernel.txt ]; then
	CUSTOM_KERN=`cat /opt/compaq/cpqrid/drivers/kernel.txt`
else
	CUSTOM_KERN=""
fi
CRID_DRIVER="cpqrid"
OS_VER=`uname -r`

REBUILD_MODULE=FALSE
OS_SMP=""
if [ "$PREBUILT_UP" = "$OS_VER" ]; then
        OS_SMP="up"
elif [ "$PREBUILT_ENT" = "$OS_VER" ]; then
        OS_SMP="ent"
elif [ "$PREBUILT_SMP" = "$OS_VER" ]; then
	OS_SMP="smp"
elif [ "$CUSTOM_KERN" != "$OS_VER" ]; then
	REBUILD_MODULE=TRUE
fi
K_VER=`uname -r | awk -F. '{ print $2 }'`
if [ "$K_VER" = "6" ]; then
	MOD_EXT="ko"
else
	MOD_EXT="o"
fi

if test "$1" = "stop"; then
	stopit $2
	exit 0
fi

if test "$1" = "start"; then
	if [ ! -f /opt/compaq/cpqrid/drivers/${CRID_DRIVER}.${MOD_EXT} ]; then
		OS_RUN="init"
 	elif [ "$CUSTOM_KERN" != "$OS_VER" ]; then
 		OS_RUN="init"
	else
		startup $2
		LS_MOD=`lsmod | fgrep ${CRID_DRIVER}`
                if [ ${#LS_MOD} -eq 0 ]; then
                        OS_RUN="init"
		else
			exit 0
                fi
	fi
fi

if test "$1" = "rebuild"; then
	rebuild
	cp /opt/compaq/cpqrid/custom/${CRID_DRIVER}.${MOD_EXT} /opt/compaq/cpqrid/drivers/${CRID_DRIVER}.${MOD_EXT}
	echo "$OS_VER" > /opt/compaq/cpqrid/drivers/kernel.txt
fi

if test "$OS_RUN" != "init"; then
	if test "$1" != "init"; then
		exit -1	
	fi
fi

copyorfixup ${CRID_DRIVER}
if [ "`echo $CMANOSTARTINSTALL | tr [a-z] [A-Z]`" != "YES" ]; then
	startup_helper ${CRID_DRIVER} $2
fi

touch /var/lock/subsys/cpqrid
/sbin/depmod -a -e  > /dev/null 2> /dev/null
