#!/bin/sh

. /opt/compaq/hpasm/etc/common.functions

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

disable() {
	for RC in `ls -d $RC_PATH/rc*.d 2>/dev/null`; do
		rm -rf $RC/*$1
	done;
}

setup_driver() {
	log_msg "Enabling Lights-Out Drivers and Agents at ${MYDATE} on Linux kernel: ${OS_VER}"
	/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)."
		log_msg "Error loading $1:  no insight management devices available on this system."
		rm -rf /dev/cpqci
		rm -rf /dev/crid
		rm -rf /dev/cpqhealth/crid
		exit -1
	fi
	touch /opt/compaq/$1/enabled
        if test "$1" = "cpqrid"; then
		mknod /dev/crid c 237 0 >/dev/null 2>&1
		mkdir -p /dev/cpqhealth >/dev/null 2>&1
		mknod /dev/cpqhealth/crid c 237 0 >/dev/null 2>&1
		rm -rf /dev/cpqci
	else
		mknod /dev/cpqci c 10 209 >/dev/null 2>&1
		rm -rf /dev/crid
		rm -rf /dev/cpqhealth/crid
	fi
	log_msg "$1 has been enabled."
	if test "$2" = "verbose"; then
		echo "..$1 started."
	fi
}

startup() {
	if [ -f /opt/compaq/$2/enabled ]; then
		/opt/compaq/$2/cp$2 start $1
	fi
}

stopit() {
	if [ -f /opt/compaq/$2/enabled ]; then
		PID1=`pidof cmahostd`
		PID2=`pidof cmapeerd`
		/opt/compaq/foundation/etc/cmahostd stop >/dev/null 2>&1
		/opt/compaq/foundation/etc/cmapeerd stop >/dev/null 2>&1
		sleep 5
		/opt/compaq/$2/cp$2 stop $1
		if [ ! -z "$PID2" ]; then
			/opt/compaq/foundation/etc/cmapeerd start >/dev/null 2>&1
		fi
		if [ ! -z "$PID1" ]; then
			/opt/compaq/foundation/etc/cmahostd start >/dev/null 2>&1
		fi
	fi
}

endit() {
	rm -rf /opt/compaq/cpqrid/enabled
	rm -rf /opt/compaq/cpqci/enabled
	disable hprsm
	log_msg "Lights-Out Drivers and Agents have been disabled."
}

setup_agents() {
        TMPCMACONF=/tmp/cma.conf
        TMPFILE2=/tmp/file.conf
	kill_filehandles
	if [ ! -f $CMACONFFILE ]; then
		echo "You must run 'hpasm activate' as 'root' user before"
		echo "running the software agents contained in this package."
		echo "For proper configuration of the software agents in"
		echo "this package, it is recommended that you run"
		echo "'/opt/compaq/hprsm/hprsm activate' after you have"
		echo "run 'hpasm activate.'"
	else
                EXCLUDESTR=`egrep "^exclude" /opt/compaq/cma.conf | cut -b 9- 2> /dev/null`
                if test "$1" = "cpqrid"; then
                        EXNAMES="cpqci cpqriisd cmarackd"
			if [ -z "`echo $EXCLUDESTR | grep cmasm2d 2>/dev/null`" ]; then
                        	INCNAMES="cpqrid cmasm2d"
			else
                        	INCNAMES="cpqrid"
			fi
                else
                        EXNAMES="cpqrid"
			if [ -z "`echo $EXCLUDESTR | grep cmasm2d 2>/dev/null`" ]; then
                        	INCNAMES="cpqci cmasm2d cpqriisd cmarackd"
			else
                        	INCNAMES="cpqci"
			fi
                fi
                EXCLUDESTRTMP=""
		FIELDS=`echo $EXCLUDESTR|awk '{print NF}'`
		for i in $INCNAMES; do
			for J in `seq 1 $FIELDS`; do
				TMPNAME=`echo $EXCLUDESTR|cut -d ' ' -f $J 2>/dev/null`
				if [ "$i" != "$TMPNAME" ]; then
					if [ -z "`echo $EXCLUDESTRTMP | grep $TMPNAME 2>/dev/null`" ]; then
						if [ -z "`echo $INCNAMES | grep $TMPNAME 2>/dev/null`" ]; then
							EXCLUDESTRTMP="$EXCLUDESTRTMP $TMPNAME"
						fi
					fi
				fi
			done
		done
                EXCLUDESTR=$EXCLUDESTRTMP
                for i in $EXNAMES; do
                        if [ -z "`echo $EXCLUDESTR | grep $i`" ]; then
                                EXCLUDESTR="$EXCLUDESTR $i"
                        fi
                done	
		SNMPDAGENTEXCLUDE="exclude $EXCLUDESTR\\n"
        	echo -en "$SNMPDAGENTEXCLUDE" >> $TMPFILE2
        	LINES2=1
        	cma_conf_add
		if [ "`echo $CMANOSTARTINSTALL | tr [a-z] [A-Z]`" = "YES" ]; then
			echo "Delaying startup of HP Insight Management Drivers and Agents ..."
		else
			$RC_DIR/hprsm start agents
		fi
	fi
}

check_rebuild() {
	#do we need to build a new binary rpm? (custom kernel)
	DONE="0"
	if [ -f /opt/compaq/cpqrid/custom/cpqrid.${MOD_EXT} ]; then
		if [ ! -f /opt/compaq/cpqci/custom/cpqci.${MOD_EXT} ]; then
			/opt/compaq/cpqci/cpcpqci rebuild
			/opt/compaq/hprsm/etc/rebuild
			DONE="1"
		fi
	fi
	if test "$DONE" = "0"; then
		if [ -f /opt/compaq/cpqci/custom/cpqci.${MOD_EXT} ]; then
			if [ ! -f /opt/compaq/cpqrid/custom/cpqrid.${MOD_EXT} ]; then
				/opt/compaq/cpqrid/cpcpqrid rebuild
				/opt/compaq/hprsm/etc/rebuild
			fi
		fi
	fi
}

log_msg() {
        echo "$*" >> $HPRSM_LOGFILE
}

kill_filehandles() {
#first of all, I do not want any file handles
#open other than 0,1,2
	for FILE in /proc/$$/fd/*; do
        	FD=`basename $FILE`
		#exempt the standard file descriptor
        	[ "$FD" = "0" ] && continue
        	[ "$FD" = "1" ] && continue
        	[ "$FD" = "2" ] && continue
		# this is a file to the script at hand
        	[ "$FD" = "255" ] && continue
        	LINKTO=`ls -o $FILE 2> /dev/null | awk '{print $10}'`
        	if [ -e "$LINKTO" ]; then
               		# echo "Closing $FD pointing to $LINKTO" 
			eval "exec $FD>&-"
        	fi
	done
	exec 0<&-	
}

MYDATE=`date`
HPRSM_LOGFILE=/opt/compaq/hprsm/hprsm_boot.log
CMACONFFILE=/opt/compaq/cma.conf
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

K_VER=`uname -r | awk -F. '{ print $2 }'`
if [ "$K_VER" = "6" ]; then
	MOD_EXT="ko"
else
	MOD_EXT="o"
fi

RC_PATH="etc/rc.d"
if [ ! -d /etc/rc.d ]; then
	RC_PATH="/etc"
fi

# Scan PCI space for both RILOE1 and 2

RILOE1_SIG="`cut -f 2 /proc/bus/pci/devices | grep 80861960 2>/dev/null`"
RILOE2_SIG="`cut -f 2 /proc/bus/pci/devices | grep 0e11005a 2>/dev/null`"

if [ ! -z "$RILOE1_SIG$RILOE2_SIG" ]; then
	if [ ! -f /opt/compaq/cpqrid/enabled ]; then
		OS_RUN="init"
		rm -rf /opt/compaq/cpqci/enabled
		echo "RILOE/RILOE II card detected, attempting to load cpqrid"
	fi
else
	if [ -f /opt/compaq/cpqrid/enabled ]; then
		OS_RUN="init"
		rm -rf /opt/compaq/cpqrid/enabled
		echo "RILOE/RILOE II card removal detected, attempting to load cpqci"
	fi
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 [ -f $CMACONFFILE ]; then
	TAINT=`egrep "notaint" /opt/compaq/cma.conf 2> /dev/null`

	if [ "$TAINT" = "notaint" ]; then
        	echo "Warning: Can not start hprsm!"
        	echo "Please run '/etc/init.d/hpasm reconfigure'"
        	echo "as 'root' user to allow kernel tainting."
        	exit 1
	fi
fi

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

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

if test "$1" = "start"; then
        if [ ! -f /opt/compaq/cpqrid/drivers/cpqrid.${MOD_EXT} ]; then
        	if [ ! -f /opt/compaq/cpqci/driver/cpqci.${MOD_EXT} ]; then
                	OS_RUN="init"
		fi
	fi
	if [ ! -f /opt/compaq/cpqrid/enabled ]; then
		if [ ! -f /opt/compaq/cpqrid/enabled ]; then
               		OS_RUN="init"
		fi
	fi
        if test "$OS_RUN" != "init"; then 
                startup $2 $3
                exit 0
        fi
fi

if test "$1" = "activate"; then
	if [ -f /opt/compaq/cpqrid/enabled ]; then
		setup_agents cpqrid
                exit 0
	else
		setup_agents cpqci
                exit 0
	fi
fi

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

enable hprsm S91 K01
/sbin/insserv -d hprsm >/dev/null 2>&1

if [ ! -z "$RILOE1_SIG$RILOE2_SIG" ]; then
	setup_driver cpqrid $2
	check_rebuild
	setup_agents cpqrid
	exit 0
fi


#default
setup_driver cpqci $2
check_rebuild
setup_agents cpqci
exit 0
