#!/bin/sh
#
# (c) Copyright 2002 Hewlett-Packard Development Company, L.P.
#

#unset the environmental variables we may inherit from the LSP
unset LD_ASSUME_KERNEL
unset LD_LIBRARY_PATH

#We are not internationalized
export LANG=C

umask 0027
#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


INSHPTXT=/opt/compaq/hpasm/etc/inshptxt
RMHPTXT=/opt/compaq/hpasm/etc/rmhptxt

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

createdistrotxt

enable() {
	NAME=$1
	SUFF=$2
	shift
	shift
	LVL=$*	
	for I in $LVL; do
		rm -f /etc/rc.d/rc$I.d/S*$NAME >/dev/null 2>/dev/null
		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/K*$NAME >/dev/null 2>/dev/null
		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="cmhp hpasmd cpqasm cmafdtn cmasvr hprsm cmastor cmanic"
		;;
  	cpqasm*)
		CURRENTSUBS="cmhp cpqasm"
		;;
  	hpasmd*)
		CURRENTSUBS="cmhp hpasmd"
		;;
  	cmafdtnstart)
		CURRENTSUBS="cmhp hpasmd cpqasm cmafdtn"
		;;
  	cmafdtn*)
		CURRENTSUBS="cmafdtn"
		;;
  	cmasvrstart)
		CURRENTSUBS="cmhp hpasmd cpqasm cmasvr"
		;;
  	cmasvr*)
		CURRENTSUBS="cmasvr"
		;;
  	driverstop)
		CURRENTSUBS="cmhp hpasmd cpqasm cmafdtn cmasvr hprsm cmastor cmanic"
		;;
  	driver*)
		CURRENTSUBS="cmhp hpasmd cpqasm hprsm"
		;;
  	agentstart)
		CURRENTSUBS="cmhp hpasmd cpqasm cmafdtn cmasvr cmastor hprsm cmanic"
		;;
  	agent*)
		CURRENTSUBS="cmhp cmafdtn cmasvr hprsm cmastor cmanic"
		;;
  	silentstart)
		CURRENTSUBS="cmhp hpasmd cpqasm cmafdtn cmasvr cmastor hprsm cmanic"
		;;
  	silentstop)
		CURRENTSUBS="cmhp hpasmd cpqasm cmafdtn cmasvr hprsm cmastor cmanic"
		;;
  	start)
		CURRENTSUBS="cmhp hpasmd cpqasm cmafdtn cmasvr cmastor hprsm cmanic"
		;;
  	onlystart)
		CURRENTSUBS="cmhp hpasmd cpqasm cmafdtn cmasvr"
		;;
  	stop)
		CURRENTSUBS="cmhp hpasmd cpqasm cmafdtn cmasvr hprsm cmastor cmanic"
		;;
  	status)
		CURRENTSUBS="cmhp hpasmd cpqasm cmafdtn cmasvr cmastor"
		;;
	*)
		CURRENTSUBS="cmhp hpasmd cpqasm cmafdtn cmasvr cmastor hprsm cmanic"
		;;
	esac
}
	
		

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

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

		if [ "$CURRENTACTION" = "start" ]; then
                    if [ -f $AGENTLOCKFILE ]; then
			TESTSTRING=`cat $AGENTLOCKFILE | fgrep $BASENAME`
			if [ ! -z "$TESTSTRING" ]; then
				echo "Skipping startup of $BASENAME. This is normal during the initial install."
				echo "Otherwise, remove $AGENTLOCKFILE"
				echo
				continue
			fi
		    fi
                fi

		TESTSTRING=`echo -n $CURRENTSUBS | fgrep $BASENAME` 
		if [ ! -z "$TESTSTRING" ]; 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 $?
} 

hpsmh_check() {

        # group add hpsmh - this is for cleansweep
        if [ ! -z `egrep "^hpsmh" /etc/group` ]; then
               echo "The hpsmh group already exists...doing nothing"
        else
               echo "The hpsmh group does NOT exist so creating it now..."
               groupadd hpsmh > /dev/null 2> /dev/null
        fi
        # user add hpsmh - for cleansweep
        if [ ! -z `egrep "^hpsmh" /etc/passwd` ]; then
               echo "The hpsmh user already exists...doing nothing"
        else
               echo "The hpsmh user does NOT exist so creating it now..."
               useradd -g hpsmh -s /sbin/nologin -M -r -d /opt/hp/hpsmh hpsmh > /dev/null 2> /dev/null
        fi

        echo "Changing Owner and Group of '/opt/hp/hpsmh/data' to 'hpsmh:hpsmh' ..."
        chown -R hpsmh:hpsmh /opt/hp/hpsmh/data

        echo "Going to apply the permissions..."
        chmod 4750 /opt/hp/hpsmh/data/webapp-data/webagent/csginkgo

}

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

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

#/opt/compaq/server/etc/cmasvrpeerd \
#/opt/compaq/server/etc/cmahealthd \
#/opt/compaq/server/etc/cmastdeqd \
#/opt/compaq/foundation/etc/hpwebsetup"
#/opt/compaq/foundation/etc/cmapeerd \
#/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 | cut -d : -f1`
CONDCOPYDIR=/opt/compaq/hpasm/addon
SANCHECKDIR=/opt/compaq/hpasm/addon
INSTALLLOG=/var/spool/compaq/cma.log
INSTMSG=hpasm
AGENTLOCKFILE=/var/spool/compaq/cma.install

SILENTSTARTARGS='>/dev/null|>/dev/null|quiet|>/dev/null|>/dev/null|>/dev/null|>/dev/null|>/dev/null'
SILENTSTOPARGS='>/dev/null|>/dev/null|>/dev/null|>/dev/null|>/dev/null|quiet|>/dev/null|>/dev/null'
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

#if agents are prevented from starting up by cma.install
#AND we are not in non-interactive mode
#delete this file
if [ -f $AGENTLOCKFILE ]; then
   if [ -z "$CMASILENT" -a -z "$CMAFDTNSILENT" ]; then
      echo "Removing lock file $AGENTLOCKFILE automatically, left over from PSP install"
      rm -f $AGENTLOCKFILE
   fi
fi

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/reconfigure' instead."
			exit 1
		fi
		if [ -x /sbin/insserv ]; then
                        /sbin/insserv snmpd
			/sbin/insserv -f hpasm
		elif [ -x /sbin/chkconfig ]; then
                        /sbin/chkconfig --level 2345 snmpd on 
                        /sbin/chkconfig --level 016 snmpd off 
			/sbin/chkconfig --add hpasm
		else
			enable snmpd K20 0 1 6 
			enable snmpd S80 2 3 4 5
			enable hpasm K01 0 1 6 
			enable hpasm S91 2 3 4 5
		fi
		foundation_postinstall $OS_DIST
		server_postinstall $OS_DIST
		storage_postinstall $OS_DIST
		if [ "$CMASKIPHPSMHCONFIG" != "y" ]; then
                	hpsmh_check
		fi
		if [ "$CMASKIPSNMPCONFIG" != "y" ]; then
			foundation_snmp_conf_add
		else
			foundation_snmp_conf_perm
		fi
		ln -sf /opt/compaq/hpasm/etc/rebuild /sbin/hpasm_rebuild
		if [ ! -z "$CMANOSTARTINSTALL" ]; then
			echo "Delaying startup of the following agent/driver group(s): $CMANOSTARTINSTALL"
			echo $CMANOSTARTINSTALL > $AGENTLOCKFILE 
		fi
		make_hpasmrc
		snmp_restart
		hpsmh_restart
		echo "Starting HP Server Management Drivers and Agents, please wait ..."
		shift	
		action "$SILENTSTARTARGS" start silent 
		[ $? -ne 0 ] && exit 1
		sancheck	
		rm -f /sbin/hpasm
                
                # On SuSE systems add a convience link
                if [ -f /etc/SuSE-release ]; then
                   ln -sf /etc/init.d/hpasm /sbin/rchpasm
                fi
		;;
	preuninstall)
		echo "Stopping HP Server Management Drivers and Agents, please wait ..."
		shift	
		raction "$SILENTSTOPARGS" stop silent 
		storage_preuninstall $OS_DIST
		server_preuninstall $OS_DIST
		foundation_preuninstall $OS_DIST
		cpqasm_preuninstall $OS_DIST
		[ -f /etc/hpasmrc ] && rm /etc/hpasmrc
		[ -f /etc/hpasm_rebuild ] && rm /sbin/hpasm_rebuild 
                [ -f /sbin/rchpasm ] && rm /sbin/rchpasm
                [ -f /etc/init.d/ipmi.hp ] && rm /etc/init.d/ipmi.hp
		# this is needed to undo the kill on SuSE
		if [ -x /sbin/insserv ]; then
			/sbin/insserv -f -r hpasm
		elif [ -x /sbin/chkconfig ]; then
			/sbin/chkconfig --del hpasm
		else
			disable hpasm K01 0 1 6 
			disable hpasm S91 2 3 4 5
		fi
		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
		storage_configure $OS_DIST $1
		if [ "$1" = "configure" ]; then
			make_hpasmrc
		fi
		echo "Starting HP Server Management Drivers and Agents, please wait ..."
		if [ "$CMASKIPHPSMHCONFIG" != "y" ]; then
                	hpsmh_check
		fi
		if [ "$CMASKIPSNMPCONFIG" != "y" ]; then
			foundation_snmp_conf_add
		else
			foundation_snmp_conf_perm
		fi
		snmp_restart
		hpsmh_restart
		action "$SILENTSTARTARGS" start silent
		sancheck	
		;;
	unconfigure)
		echo "Stopping HP Server Management Drivers and Agents, please wait ..."
		raction "$SILENTSTOPARGS" stop silent
		storage_unconfigure $OS_DIST
		server_unconfigure $OS_DIST
		foundation_unconfigure $OS_DIST
		cpqasm_unconfigure $OS_DIST
		echo "Server Management unconfigured."
                #Ok to leave snmp in the current state because the
                #/usr/lib/libcmaX will stay
		#snmp_restart
		#hpsmh_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

