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

startup_helper() {
	if [ -f /lib/modules/$OS_VER/kernel/drivers/char/$1.o ]; then
		/sbin/insmod -f $1 > /dev/null 2> /dev/null
		chatter $2 "..$1 started."
	fi
}

stopit_helper() {
	if eval "/sbin/rmmod $1 > /dev/null 2> /dev/null"; then
		chatter $2 "..$1 stopped."
	else
                LS_MOD=`lsmod | fgrep $1`
                if [ ${#LS_MOD} -eq 0 ]; then
                        exit 0
                fi
		chatter $2 "..$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() {
	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 /lib/modules/$OS_VER/kernel/drivers/char/$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 /lib/modules/$OS_VER/kernel/drivers/char/$1.o
	else
		echo "Linux Kernel Symbol Conflict - Attempting to rebuild to resolve"
		rebuild
		if eval "insmod -p /opt/compaq/cpqrid/custom/$1.o > /dev/null 2> /dev/null"; then
			echo "Rebuild successful."
			if [ ! -d /lib/modules/$OS_VER/kernel/drivers/char ]; then
			        mkdir -p /lib/modules/$OS_VER/kernel/drivers/char
			fi
			cp /opt/compaq/cpqrid/custom/$1.o /lib/modules/$OS_VER/kernel/drivers/char/$1.o > /dev/null 2> /dev/null
		else
			echo "Failed to rebuild for custom kernel."
		fi
	fi
}

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 /lib/modules/$OS_VER/kernel/drivers/char/${CRID_DRIVER}.o ]; then
		OS_RUN="init"
	else
		startup $2
		exit 0
	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}
startup_helper ${CRID_DRIVER} $2

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