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

startup_helper() {
	if [ -f /opt/compaq/cpqrid/drivers/$1.o ]; then
		log_msg "Loading the hp ProLiant Remote Insight Board Driver . . ."
		/sbin/insmod /opt/compaq/cpqrid/drivers/$1.o >> $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
}

startup() {
	startup_helper ${CRID_DRIVER} $1
	touch /var/lock/subsys/cpqrid
	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() {
	kernel_src_avail
	cd /opt/compaq/cpqrid/custom
	make -f linux_rebuild.mk clean all > /dev/null 2> /dev/null
}

copyorfixup() {
	REBUILD_MODULE=FALSE
	if eval "insmod -p /opt/compaq/cpqrid/drivers/${OS_SMP}/$1.o > /dev/null 2> /dev/null"; then
		cp /opt/compaq/cpqrid/drivers/${OS_SMP}/$1.o /opt/compaq/cpqrid/drivers/$1.o
	elif eval "insmod -p /opt/compaq/cpqrid/custom/$1.o > /dev/null 2> /dev/null"; then
		cp /opt/compaq/cpqrid/custom/$1.o /opt/compaq/cpqrid/drivers/$1.o
	else
		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
	MODINFO="`modinfo -d /opt/compaq/cpqrid/drivers/$1.o 2>/dev/null`"
	if [[ "$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.o
			REBUILD_MODULE=TRUE
		fi
	fi
	
	if [ "${REBUILD_MODULE}" == "TRUE" ]
        then
		rebuild
		if eval "insmod -p /opt/compaq/cpqrid/custom/$1.o > /dev/null 2> /dev/null"; then
			echo "Rebuild successful."
			log_msg "Rebuild of cpqrid successful."
			cp /opt/compaq/cpqrid/custom/$1.o /opt/compaq/cpqrid/drivers/$1.o > /dev/null 2> /dev/null
		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`
CRID_DRIVER="cpqrid"
OS_VER=`uname -r`
OS_SMP="smp"
if [ "$PREBUILT_UP" = "$OS_VER" ]; then
        OS_SMP="up"
fi
if [ "$PREBUILT_ENT" = "$OS_VER" ]; then
        OS_SMP="ent"
fi



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

if test "$1" = "start"; then
	if [ ! -f /opt/compaq/cpqrid/drivers/${CRID_DRIVER}.o ]; 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
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
