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

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

stopit_helper() {
	LS_MOD=`lsmod | grep $1`
	if test "$LS_MOD" = ""; then
		return 0;
	fi
	if eval "/sbin/rmmod $1 > /dev/null 2> /dev/null"; then
		chatter $2 "..$1 stopped."
	else
		chatter $2 "..$1 busy."
		exit -1
	fi
}

stopit() {
	stopit_helper ${HEALTH_DRIVER} $1
	stopit_helper ${CROM_DRIVER} $1
	rm -f /var/lock/subsys/cpqhealth
}

startup() {
	startup_helper ${CROM_DRIVER} $1 
	startup_helper ${HEALTH_DRIVER} $1
	touch /var/lock/subsys/cpqhealth
	chatter $1 "...done. Please type 'man cpqhealth' for more information."
}


copyorfixup() {
	if eval "insmod -p /lib/modules/Compaq/drivers/${OS_SMP}/$1.o > /dev/null 2> /dev/null"; then
		mkdir -p /lib/modules/$OS_VER/kernel/drivers/char
		cp /lib/modules/Compaq/drivers/${OS_SMP}/$1.o /lib/modules/$OS_VER/misc/$1.o
	else
		echo "Fixing up $1..."
		sh /lib/modules/Compaq/fixup /lib/modules/Compaq/drivers/$OS_SMP /lib/modules/$OS_VER/misc $1.o
	fi
}

HEALTH_DRIVER="cpqhealth"
CROM_DRIVER="cpqrom"
OS_VER=`uname -r`
OS_SMP=`uname -a|grep SMP`
OS_RUN=""

if [ ${#OS_SMP} -eq 0 ]; then
	OS_SMP="up"
else 
	OS_SMP="smp"	
fi


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

if test "$1" = "start"; then
	if [ ! -f /lib/modules/$OS_VER/misc/${HEALTH_DRIVER}.o ]; then
		OS_RUN="init"
	else
		startup $2
		exit 0
	fi
fi

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

#start the "right driver" for EISA ID 
copyorfixup cpqeisaid 
/sbin/insmod -f  /lib/modules/$OS_VER/misc/cpqeisaid.o > /dev/null 2> /dev/null
if [ -f /proc/cpqeid ]; then
	EISA_ID=`cat /proc/cpqeid`
	case "$EISA_ID" in
		9106110e)
				;;
		8906110e)
				;;
		7906110e)
				;;
		 907110e)
				;;
		 808110e)
				;;
		1008110e)
				;;
		 816110e)
				;;
		 916110e)
				;;
		8506110e)
				;;
		5506110e)
				;;
		1207110e)
				;;
		5306110e)
				;;
		9206110e)
				;;
		1507110e)
				;;
		1407110e)
				;;
		9006110e)
				;;
		2608110e)
				;;
		2508110e)
				;;
		2808110e)
				;;
		1e07110e)
				;;
		1a07110e)
				;;
		2307110e)
				;;
		1016110e)
				;;
		1116110e)
				;;
		2107110e)
				;;
		2207110e)
				;;
		*) 		EISA_ID=ffffffff
				;;
	esac
	/sbin/rmmod cpqeisaid
	rm -f /lib/modules/$OS_VER/misc/cpqeisaid.o 
	if test "${EISA_ID}" = "ffffffff"; then
		if test "$2" = "verbose"; then
			echo "This Compaq Health & Wellness Driver is not certified for your system. Uninstall this package at your earliest convenience."
		fi
		exit -1
	fi
else
	echo "This system is missing packages required to run the Compaq Health & Wellness Driver.  Please uninstall this package at your earliest convenience.  Please refer to the trouble-shooting section on Failed Dependencies in the Health Driver How-To located at http://www.compaq.com/products/servers/linux/linux-howtos.html for a list of required packages."

	exit -1
fi

copyorfixup ${CROM_DRIVER}
startup_helper ${CROM_DRIVER} $2 

copyorfixup ${HEALTH_DRIVER}
startup_helper ${HEALTH_DRIVER} $2

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