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

startup_helper() {
	if [ -f ${CHIF_DRIVER_LOC}/$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 ${CHIF_DRIVER} $1
	rm -f /var/lock/subsys/cpqci
}

startup() {
	startup_helper ${CHIF_DRIVER} $1
	touch /var/lock/subsys/cpqci
#	chatter $1 "...done. Please type 'man cpqci' 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 ${CHIF_DRIVER_LOC}/$1.o
	else
		echo "Fixing up $1..."
		sh /lib/modules/Compaq/fixup.${CHIF_DRIVER} /lib/modules/Compaq/drivers/$OS_SMP ${CHIF_DRIVER_LOC} $1.o
	fi
}

CHIF_DRIVER="cpqci"
OS_VER=`uname -r`
OS_SMP=`uname -a|grep SMP`
OS_RUN=""
CHIF_DRIVER_LOC=/lib/modules/$OS_VER/kernel/drivers/char

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 ${CHIF_DRIVER_LOC}/${CHIF_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 ${CHIF_DRIVER}
startup_helper ${CHIF_DRIVER} $2

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