#!/bin/sh

#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

. /opt/compaq/hpasm/etc/common.functions
. /opt/compaq/hpasm/etc/cpqasm.functions
. /opt/compaq/hpasm/etc/foundation.functions
. /opt/compaq/hpasm/etc/server.functions

enable() {
	NAME=$1
	SUFF=$2
	shift
	shift
	LVL=$*	
	for I in $LVL; do
		ln -sf $RC_DIR/$NAME /etc/rc.d/rc$I.d/$SUFF$NAME >/dev/null 2>/dev/null
	done
}

disable() {
	NAME=$1
	SUFF=$2
	shift
	shift
	LVL=$*	
	for I in $LVL; do
		rm -f /etc/rc.d/rc$I.d/$SUFF$NAME >/dev/null 2>/dev/null
	done
}

#determine which subagents are addressed
determinesubs() {
	CURRENTSUBS=$2
	case "$2$1" in
  	cpqasmstop)
		CURRENTSUBS="cpqasm cmafdtn cmasvr hprsm cmastor cmanic"
		;;
  	cpqasm*)
		CURRENTSUBS="cpqasm"
		;;
  	cmafdtnstart)
		CURRENTSUBS="cpqasm cmafdtn"
		;;
  	cmafdtn*)
		CURRENTSUBS="cmafdtn"
		;;
  	cmasvrstart)
		CURRENTSUBS="cpqasm cmasvr"
		;;
  	cmasvr*)
		CURRENTSUBS="cmasvr"
		;;
  	driverstop)
		CURRENTSUBS="cpqasm cmafdtn cmasvr hprsm cmastor cmanic"
		;;
  	driver*)
		CURRENTSUBS="cpqasm hprsm"
		;;
  	agentstart)
		CURRENTSUBS="cpqasm cmafdtn cmasvr hprsm cmastor cmanic"
		;;
  	agent*)
		CURRENTSUBS="cmafdtn cmasvr hprsm cmastor cmanic"
		;;
  	silentstart)
		CURRENTSUBS="cpqasm cmafdtn cmasvr hprsm cmastor cmanic"
		;;
  	silentstop)
		CURRENTSUBS="cpqasm cmafdtn cmasvr hprsm cmastor cmanic"
		;;
  	start)
		CURRENTSUBS="cpqasm cmafdtn cmasvr hprsm cmastor cmanic"
		;;
	onlystart)
		CURRENTSUBS="cpqasm cmafdtn cmasvr"
		;;
  	stop)
		CURRENTSUBS="cpqasm cmafdtn cmasvr hprsm cmastor cmanic"
		;;
  	status)
		CURRENTSUBS="cpqasm cmafdtn cmasvr"
		;;
	*)
		CURRENTSUBS="cpqasm cmafdtn cmasvr hprsm cmastor cmanic"
		;;
	esac
}
	
		

actionhelper() {
	CURRENTSCRIPTS=$1
	CURRENTARGS=$2
	CURRENTACTION=$3
	CURRENTSUBS=$4
	determinesubs $CURRENTACTION $CURRENTSUBS
	I=1
	RC=0

	# At this point, no input interaction is needed, 
	# so, I shut stdin down
	exec 0<&-	

	for SCRIPT in $CURRENTSCRIPTS; do
		BASENAME=`basename $SCRIPT`
		ARGS=`echo -n $CURRENTARGS | cut -d '|' -f $I`
		TESTSTRING=`echo -n $CURRENTSUBS | fgrep $BASENAME` 

		if [ "x$TESTSTRING" != "x" ]; then
			if [ -x $SCRIPT ]; then
				eval "sh $SCRIPT $CURRENTACTION $ARGS"
				SCRIPTSTATUS=$?
                                # If the return code is a failure and the current script
                                # is the cpqasm script that means our driver could not
 				# be loaded. In this case we should just exit and bail
				# out. This will fix issue 245135 where the console
				# message "Starting hpasm.....[ok]" is displayed when
				# in fact it should say FAILED.
                                if [ $SCRIPTSTATUS -ne 0 -a $SCRIPT = "/opt/compaq/cpqhealth/cpqasm/cpqasm" ]; then
                                   	exit 1
 				# Here of course it could just be an agent not loading for some
				# reason, but our driver is loaded and functional.
                                elif [ $SCRIPTSTATUS -ne 0 ]; then
					RC=4	
				fi
			fi
		fi

		I=`expr $I + 1`

	done

	return $RC
}

action() {
	actionhelper "$SCRIPTS" "$1" "$2" "$3" 
}

raction() {
	actionhelper "$RSCRIPTS" "$1" "$2" "$3" 
}

condcopy() {
	sh $CONDCOPYDIR/condcopy $CONDCOPYDIR/condcopy.txt
}

condrem() {
	sh $CONDCOPYDIR/condrem $CONDCOPYDIR/condcopy.txt
}

sancheck() {
	sh $SANCHECKDIR/sancheck $SANCHECKDIR/sancheck.txt
	return $?
} 

SCRIPTS="\
/opt/compaq/cpqhealth/cpqasm/cpqasm \
/opt/compaq/foundation/etc/cmafdtn \
/opt/compaq/server/etc/cmasvr \
/etc/init.d/hprsm \
/etc/init.d/cmastor \
/etc/init.d/cmanic"

RSCRIPTS="\
/etc/init.d/cmanic \
/etc/init.d/cmastor \
/etc/init.d/hprsm \
/opt/compaq/server/etc/cmasvr \
/opt/compaq/foundation/etc/cmafdtn \
/opt/compaq/cpqhealth/cpqasm/cpqasm" 

#/opt/compaq/server/etc/cmasvrpeerd \
#/opt/compaq/server/etc/cmahealthd \
#/opt/compaq/server/etc/cmastdeqd \
#/opt/compaq/foundation/etc/hpwebsetup"
#/opt/compaq/foundation/etc/cmafdtnpeerd \
#/opt/compaq/foundation/etc/cmahostd \
#/opt/compaq/foundation/etc/cmathreshd \
#/opt/compaq/foundation/etc/cmawebd \


RC_DIR=/etc/init.d

OS_DIST=`cat /opt/compaq/hpasm/distro.txt`
CONDCOPYDIR=/opt/compaq/hpasm/addon
SANCHECKDIR=/opt/compaq/hpasm/addon
INSTALLLOG=/var/spool/compaq/cma.log
INSTMSG=hpasm

SILENTSTARTARGS='quiet|>/dev/null|>/dev/null|>/dev/null|>/dev/null|>/dev/null'
SILENTSTOPARGS='>/dev/null|>/dev/null|>/dev/null|>/dev/null|>/dev/null|quiet'
DRIVERSTARTARGS='|||cpqci cpqrid||'
AGENTSTOPARGS='||cmasm2d cmarackd cpqriisd|||'
DEFAULTARGS='|||||'

# We will allow 'hpasm activate' to be issued more
# than once, but only if the second time is done
# because of an upgrade through the rpm -Uvh command.
if [ "$0" = "/sbin/hpasm" -a "$1" != "activate" ]; then
	echo "Use hpasm activate before issuing any other commands"
	exit 1
fi


#Conditional coppy step, regardless of what action
condcopy

case "$2" in
	driver)
		CURRENTSTARTARGS=$DRIVERSTARTARGS
		CURRENTSTOPARGS=$DEFAULTARGS
		;;
	agent)
		CURRENTSTARTARGS=$DEFAULTARGS
		CURRENTSTOPARGS=$AGENTSTOPARGS
		;;
	*)
		CURRENTSTARTARGS=$DEFAULTARGS
		CURRENTSTOPARGS=$DEFAULTARGS
		;;
esac

case "$1" in
	postinstall|activate)
		if [ ! -e /sbin/hpasm ]; then
			echo "hpasm activate/postinstall can only be issued once."
			echo "Use '/etc/init.d/hpasm configure' or"
			echo "'/etc/init.d/hpasm reconfigure' instead."
			exit 1
		fi
		enable hpasm K10 0 1 6 
		enable hpasm S90 2 3 4 5
		cpqasm_postinstall $OS_DIST
		foundation_postinstall $OS_DIST
		server_postinstall $OS_DIST
		ln -sf /opt/compaq/hpasm/etc/rebuild /sbin/hpasm_rebuild
		if [ "`echo $CMANOSTARTINSTALL | tr [a-z] [A-Z]`" = "YES" ]; then
			echo "Delaying startup of HP Server Management Drivers and Agents ..."
		else
			make_hpasmrc
			snmp_restart
			echo "Starting HP Server Management Drivers and Agents, please wait ..."
			shift	
			action "$SILENTSTARTARGS" start silent 
			[ $? -ne 0 ] && exit 1
		fi
		sancheck	
		rm -f /sbin/hpasm
		;;
	preuninstall)
		echo "Stopping HP Server Management Drivers and Agents, please wait ..."
		shift	
		raction "$SILENTSTOPARGS" stop silent 
		server_preuninstall $OS_DIST
		foundation_preuninstall $OS_DIST
		cpqasm_preuninstall $OS_DIST
		rm /etc/hpasmrc
		rm /sbin/hpasm_rebuild 
		disable hpasm K10 0 1 6 
		disable hpasm S90 2 3 4 5
		condrem
		;;
	configure|reconfigure)
		echo "Stopping HP Server Management Drivers and Agents, please wait ..."
		raction "$SILENTSTOPARGS" stop silent
		cpqasm_configure $OS_DIST $1 
		foundation_configure $OS_DIST $1
		server_configure $OS_DIST $1
		if [ "$1" = "configure" ]; then
			make_hpasmrc
		fi
		echo "Starting HP Server Management Drivers and Agents, please wait ..."
		snmp_restart
		action "$SILENTSTARTARGS" start silent
		sancheck	
		;;
	unconfigure)
		echo "Stopping HP Server Management Drivers and Agents, please wait ..."
		raction "$SILENTSTOPARGS" stop silent
		server_unconfigure $OS_DIST
		foundation_unconfigure $OS_DIST
		cpqasm_unconfigure $OS_DIST
		echo "Server Management unconfigured."
		snmp_restart
		;;
	start)
		action "$CURRENTSTARTARGS" "$1" "$2" "$3" 
		sancheck	
		if [ $? -ne 0 ]; then 
			echo "hpasm:  Server Management is not fully enabled"
		else
			echo "hpasm:  Server Management is enabled"
		fi
		;;
	stop)
		raction "$CURRENTSTOPARGS" "$1" "$2" "$3" 
		;;
	silentstart)
		echo "Starting HP Server Management Drivers and Agents, please wait ..."
		shift	
		action "$SILENTSTARTARGS" start silent 
		if [ $? -ne 0 ]; then 
			exit 1
		fi
		sancheck	
		;;
	silentstop)
		echo "Stopping HP Server Management Drivers and Agents, please wait ..."
		shift
		raction "$SILENTSTOPARGS" stop silent 
		;;
	*)
		action "$DEFAULTARGS" "$1" "$2" "$3" 
		;;
esac

