#!/bin/bash

# bash provides Brace Substitution (${word%pattern}) and declare -F

# Support:
# 1. Packager (rpm, apt, whatever) with postinstall, preinstall, etc
# 2. Runtime from /etc/init.d

[ -z "$PRODACRO" ] && echo $0 needs env vars first && exit 99 # in /etc/opt

set -u

LINESEP="__________________________________________________________________________"

unset HPBMC_DEVICE	# let's keep it local

# Get the startup scripts, observing the order dependency

cd $HPETC/rc.d
SCRIPTS=`ls S*`	# asterisk will pick up the absolute path
RSCRIPTS=`ls -r S*`

# Source any other function support scripts: functions only so no ordering.
# Do this before the init_ routines below :-)

for i in `ls functions\.*`; do . $i; done
POSTINSTALLS=`declare -F | awk '/_postinstall$/ {print $3}'`
PREUNINSTALLS=`declare -F | awk '/_preuninstall$/ {print $3}'`
CONFIGURES=`declare -F | awk '/_configure$/ {print $3}'`
UNCONFIGURES=`declare -F | awk '/_unconfigure$/ {print $3}'`

# There's a known problem in rpm on RHEL3 (at least) where it leaves fd 18
# open.  This annoys programs like ssh, which wait for the last close on
# its fd, because the stray fd gets inherited by any daemons that we spawn,
# so let's close those pesky extra file descriptors right now:

exec 3>&- 4>&- 5>&- 6>&- 7>&- 8>&- 9>&- 10>&- 11>&- 12>&- 13>&- 14>&- 15>&- \
     16>&- 17>&- 18>&- 19>&- 20>&- 21>&- 22>&- 23>&- 24>&- 25>&- 26>&- 27>&-

action() {
	# Idiot checks from design phase, just leave them in
	MSG="$0::$FUNCNAME() bad arg "
	[ $# -ne 4 ] && echo $MSG count && exit 99

	# $1 Either "$SCRIPTS" or "$RSCRIPTS"; must be passed in quotes
	[ "$1" != "$SCRIPTS" -a "$1" != "$RSCRIPTS" ] && echo $MSG 1 && exit 99

	# $2 One of the approved keywords from /etc/init.d/PRODACRO, it
	#    usually is the $1 passed into this script from there
	[ -z "$2" ] &&  echo $MSG 2 && exit 99
	ACTION=$2

	# $3 Either a subagent or explicit empty string.  Not worth putting
	# smarts in here, although a grep of rc.d/* might be valid...
	SUBAGENT=$3

	# $4 Either "silent" or explicit empty string.  There are 4 args here
	[ -n "$4" -a "$4" != "silent" ] && echo "$MSG 4 ($4)" && exit 99
	VERBOSE=
	[ -n "$4" ] && VERBOSE=">/dev/null 2>&1"

	RETVAL=0
	for SCRIPT in $1; do
		cd $HPETC/rc.d	# Paranoia never killed anyone
		DOIT=yes
		if [ -n "$SUBAGENT" ]; then	# Now check for match
			[ "${SCRIPT%$SUBAGENT}" = "$SCRIPT" ] && DOIT=no
		fi
		if [ $DOIT = yes -a -x $SCRIPT ]; then
			eval "./$SCRIPT $ACTION $SUBAGENT $VERBOSE"
			R=$?
			[ $R -gt 0 ] && RETVAL=$R
		fi
	done
	return $RETVAL
}

lddcheck() {
	$HPETC/lddcheck
	return $?
}

# There is one optional argument: a specific agent or daemon

SUBAGENT=
if [ $# -eq 2 ]; then
	[ "$2" = driver ] && echo "Bad argument" >&2 && exit 99	# Legacy
	SUBAGENT=$2
fi

# Real Soon Now...

RUNDAEMON=$HPETC/rundaemon.$DISTROVENDOR
[ ! -x $RUNDAEMON ] && echo "No rundaemon for $DISTROVENDOR" >&2 && exit 99
export RUNDAEMON

RETHELPER=0
case "$1" in

	# These cases support deployment and should only be called by 
	# a packager (ie, the scriptlets in RPM-based distributions)

	postinstall)
		mkdir -p $REGDIR
		for xyzzy in $POSTINSTALLS; do 
			eval "$xyzzy"
			[ $? -ne 0 ] && RETHELPER=1	# but keep going
		done
		# On SuSE, chkconfig prints out status--get rid of that.
		chkconfig --add $PRODACRO >/dev/null
		lddcheck
		let RETHELPER=$RETHELPER+$?
		;;
	configure|reconfigure)
		action "$RSCRIPTS" stop "" silent # and don't even announce it
		for xyzzy in $CONFIGURES; do 
			eval "$xyzzy $1"
			[ $? -ne 0 ] && RETHELPER=1	# but keep going
		done
		echo "(Re)starting the SNMP daemon, configured to support the"
		echo $PRODNAME
		snmp_restart
		let RETHELPER=$RETHELPER+$?
		;;
	silentstart)
		action "$SCRIPTS" start "" silent 
		RETHELPER=$?
		;;
	silentstop)
		action "$RSCRIPTS" stop "" silent 
		RETHELPER=$?
		;;
	unconfigure)
		echo "Stopping $PRODNAME, please wait..."
		action "$RSCRIPTS" stop "" silent
		RETHELPER=$?
		for i in $UNCONFIGURES; do eval "$i"; done
		# On SuSE, chkconfig prints out status--get rid of that.
		chkconfig --del $PRODACRO >/dev/null
		echo "Restarting SNMP daemon with original configuration..."
		snmp_restart
		echo -e "$PRODNAME de-configuration complete"
		;;
	preuninstall)
		for i in $PREUNINSTALLS; do eval "$i"; done
		for i in $LOGFILE $LOGINSTALL; do
		    cp $i /tmp/`basename $i`.old 2>/dev/null
		done
		rm -r $REGDIR 2>/dev/null	# has logfiles, too
		echo "Log files were copied to /tmp/<name>.old"
		;;

	# Cases called only from /etc/init.d/PRODACRO

	start)
		# We're about to start hpima--start snmpd, if needed.
		snmpd_is_running || snmp_restart

		# If it's still not running, complain.
		if ! snmpd_is_running
		then
		    echo "$PRODNAME can't start snmpd"
		    RETHELPER=1
		else
		    delete_all_transient_registry		# bug 6496
		    action "$SCRIPTS" "$1" "$SUBAGENT" ""	# never silent
		    RETHELPER=$?
		    if [ $? -ne 0 ]; then 
			echo "$PRODNAME are not fully enabled"
		    else
			echo "$PRODNAME are enabled"
		    fi
		fi
		;;
	stop)
		action "$RSCRIPTS" "$1" "$SUBAGENT" ""	# never silent
		RETHELPER=$?
		;;
	status)
		action "$SCRIPTS" "$1" "$SUBAGENT" "" 	# never silent
		RETHELPER=$?
		;;
	sample)
		action "$SCRIPTS" "$1" "$SUBAGENT" "" 	# never silent
		RETHELPER=$?
		;;
	*)	# Restart is unrolled in /etc/init.d/PRODACRO
		echo "Unknown command \"$1\""
		RETHELPER=99
		;;
esac

exit $RETHELPER
