#!/bin/sh

enable() {
	ln -s $RC_DIR/$1 /etc/rc.d/rc0.d/$3$1 >/dev/null 2>/dev/null
	ln -s $RC_DIR/$1 /etc/rc.d/rc1.d/$3$1 >/dev/null 2>/dev/null
	ln -s $RC_DIR/$1 /etc/rc.d/rc2.d/$2$1 >/dev/null 2>/dev/null
	ln -s $RC_DIR/$1 /etc/rc.d/rc3.d/$2$1 >/dev/null 2>/dev/null
	ln -s $RC_DIR/$1 /etc/rc.d/rc4.d/$2$1 >/dev/null 2>/dev/null
	ln -s $RC_DIR/$1 /etc/rc.d/rc5.d/$2$1 >/dev/null 2>/dev/null
	ln -s $RC_DIR/$1 /etc/rc.d/rc6.d/$3$1 >/dev/null 2>/dev/null
}

disable() {
	for RC in `ls -d /etc/rc.d/rc*.d`; do
		rm -rf $RC/*$1
	done;
}

setup_driver() {
	if test "$1" = "cpqrid"; then
		touch /opt/compaq/$1/enabled
		HEALTH=`lsmod | fgrep cpqasm`
		if [ ${#HEALTH} -eq 0 ]; then
			echo "The ProLiant Remote Insight Board Driver"
			echo "requires the Advance Server Management"
			echo "package."
			echo "Please remove the hprsm package at your"
			echo "earliest convenience (rpm -e hprsm)."
			exit -1
		fi
	fi
	/opt/compaq/$1/cp$1 init
	LS_MOD=`lsmod | fgrep $1`
	if [ ${#LS_MOD} -eq 0 ]; then
		echo "No insight management devices available"
		echo "(missing or disabled) on this system."
		echo "Please remove the hprsm package at your"
		echo "earliest convenience (rpm -e hprsm)."
		exit -1
	fi
	touch /opt/compaq/$1/enabled
	if test "$2" = "verbose"; then
		echo "..$1 started."
	fi
}

setup_agent() {
	/opt/compaq/$1/cp$1 init $3
	$RC_DIR/hprsm start $2
}

startup() {
	if [ -f /opt/compaq/cpqrid/enabled ]; then
		/opt/compaq/cpqrid/cpcpqrid start $1
	fi
	if [ -f /opt/compaq/cpqci/enabled ]; then
		/opt/compaq/cpqci/cpcpqci start $1
	fi
}

stopit() {
	if [ -f /opt/compaq/cpqci/enabled ]; then
		/opt/compaq/cpqci/cpcpqci stop $1
	fi
	if [ -f /opt/compaq/cpqrid/enabled ]; then
		/opt/compaq/cpqrid/cpcpqrid stop $1
	fi
}

endit() {
	rm -rf /opt/compaq/cpqrid/enabled
	rm -rf /opt/compaq/cpqci/enabled
	disable hprsm
}

check_rebuild() {
	#do we need to build a new binary rpm? (custom kernel)
	DONE="0"
	if [ -f /opt/compaq/cpqrid/custom/cpqrid.o ]; then
		if [ ! -f /opt/compaq/cpqci/driver/cpqci.o ]; then
			/opt/compaq/cpqci/cpcpqci rebuild
			/opt/compaq/hprsm/rebuild
			DONE="1"
		fi
	fi
	if test "$DONE" = "0"; then
		if [ -f /opt/compaq/cpqci/driver/cpqci.o ]; then
			if [ ! -f /opt/compaq/cpqrid/custom/cpqrid.o ]; then
				/opt/compaq/cpqrid/cpcpqrid rebuild
				/opt/compaq/hprsm/rebuild
			fi
		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`
RC_DIR=/etc/init.d
OS_VER=`uname -r`
OS_RUN=""
OS_SMP="smp"
if [ "$PREBUILT_UP" = "$OS_VER" ]; then
        OS_SMP="up"
fi
if [ "$PREBUILT_ENT" = "$OS_VER" ]; then
        OS_SMP="ent"
fi


#we want to do this basically every time this script is run
/opt/compaq/hprsm/addon/condcopy /opt/compaq/hprsm/addon/condcopy.txt


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

if test "$1" = "end"; then
	endit
	exit 0
fi

if test "$1" = "start"; then
        if [ ! -f /lib/modules/$OS_VER/kernel/drivers/char/cpqrid.o ]; then
        	if [ ! -f /lib/modules/$OS_VER/kernel/drivers/char/cpqci.o ]; then
                	OS_RUN="init"
		fi
	fi
        if test "$OS_RUN" != "init"; then 
                startup $2
                exit 0
        fi
fi

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

enable hprsm S91 K01

#setup pciFindDev
DEV_DIR=/opt/compaq/hprsm
if [ -d $DEV_DIR/tools ]; then
	if eval "insmod -p /opt/compaq/hprsm/tools/$OS_SMP/pciFindDev.o > /dev/null 2> /dev/null"; then
		DEV_DIR=/opt/compaq/hprsm/tools/$OS_SMP
	else
		if [ ! -f $DEV_DIR/pciFindDev.o ]; then
			/opt/compaq/hprsm/tools/setup_build
			[ $? != 0 ] && {
				echo "Kernel sources not available."
        			echo "Please load the appropriate sources to rebuild module."
        			exit -1
			}

			make -C /opt/compaq/hprsm/tools -f linux.mk all > /dev/null 2> /dev/null
			if eval "insmod -p $DEV_DIR/pciFindDev.o > /dev/null 2> /dev/null"; then	
				echo "Detection on errata kernel succeeded."
			else
				echo "Detection on errata kernel failed."
				exit -1
			fi	
		fi
	fi
fi


#RILOE I

/sbin/insmod $DEV_DIR/pciFindDev.o vend_id=0x8086 dev_id=0x1960 >/dev/null 2>/dev/null
INSMOD=$?
if test "$INSMOD" = "0"; then
	/sbin/rmmod pciFindDev
	setup_driver cpqrid $2
	setup_agent server cmasm2d $2
	check_rebuild
	exit 0
fi

#RILOE II

/sbin/insmod $DEV_DIR/pciFindDev.o vend_id=0x0e11 dev_id=0x005a >/dev/null 2>/dev/null
INSMOD=$?
if test "$INSMOD" = "0"; then
	/sbin/rmmod pciFindDev
	setup_driver cpqrid $2
	setup_agent server cmasm2d $2
	check_rebuild
	exit 0
fi

#default
setup_driver cpqci $2
#start cpqriisd
$RC_DIR/hprsm start cpqriisd
setup_agent server cmasm2d $2
$RC_DIR/hprsm start cmarackd
check_rebuild
exit 0
