#!/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 ${CRID_DRIVER} $1
	rm -f /var/lock/subsys/cpqhealth
}

startup() {
	startup_helper ${CROM_DRIVER} silent 
	startup_helper ${HEALTH_DRIVER} silent
	startup_helper ${CRID_DRIVER} $1
	touch /var/lock/subsys/cpqrid
	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
		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
}

CRID_DRIVER="cpqrid"
CROM_DRIVER="cpqrom"
HEALTH_DRIVER="cpqhealth"
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/${CRID_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

copyorfixup ${CRID_DRIVER}
startup_helper ${CRID_DRIVER} $2

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