# toggle if 'serel' was passed on the boot line
#
serel_boot_toggle() {
    if grep -i serel /proc/cmdline >/dev/null; then
 
        if [ $SEREL_BOOT = "yes" ]; then
            SEREL_BOOT="no"
        else
            SEREL_BOOT="yes"
        fi
    fi
}

# Use serel to start in parallel?
#
SEREL_BOOT=

# serel has only been tested with runlevels 3 and 5 at the mo
# this restriction ought to disappear soon...
#
if [ "$CONFIRM" != "yes" ] && [ $runlevel = "3" -o $runlevel = "5" ]; then

	# SEREL_BOOT might be set in here...
	#
	if [ -f /etc/sysconfig/serel ]; then
		. /etc/sysconfig/serel
	fi

	serel_boot_toggle

	[ -f /usr/sbin/sereld ] || SEREL_BOOT="no"
fi

if [ "$SEREL_BOOT" = "yes" ]; then
	# Work out what's started, and what's about to start
	#
	SEREL_STARTED=
	SEREL_PAIRS=

	for i in /etc/rc$runlevel.d/S*; do
		# Check if the script is there.
		[ ! -f $i ] && continue

		# Don't run [KS]??foo.{rpmsave,rpmorig} scripts
		[ "${i%.rpmsave}" != "${i}" ] && continue
		[ "${i%.rpmorig}" != "${i}" ] && continue
		[ "${i%.rpmnew}" != "${i}" ] && continue

		# Check if the subsystem is already up.
		subsys=${i#/etc/rc$runlevel.d/S??}

		# fall back to non-serel boot in these cases
		if [ "$subsys" = "halt" -o "$subsys" = "reboot" -o "$subsys" = "single" ]; then
			SEREL_BOOT=
			break;
		fi
		
		# build the arguments to --populate
		if [ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/${subsys}.init ]; then
			SEREL_STARTED="$SEREL_STARTED $subsys"
			continue;
		else
			SEREL_PAIRS="$SEREL_PAIRS $subsys $i"
		fi
	done
fi

# start the daemon
[ "$SEREL_BOOT" = "yes" ] && sereld -b $SERELD_OPTIONS

[ "$SEREL_BOOT" = "yes" -a $? != "0" ] && SEREL_BOOT="no"

# tell the daemon what's started and what's about to start
if [ "$SEREL_BOOT" = "yes" ]; then
	serelhistory --populate <<EOF
	--started
	$SEREL_STARTED
	--pairs
	$SEREL_PAIRS
EOF
fi

[ "$SEREL_BOOT" = "yes" -a $? != "0" ] && \
	SEREL_BOOT="no" && serelc --control stop

[ "$SEREL_BOOT" = "yes" ] && \
	serelhistory --query verify | grep "success" 2>&1 > /dev/null

[ "$SEREL_BOOT" = "yes" -a $? != "0" ] && \
	SEREL_BOOT="no" && serelc --control stop
		
if [ "$SEREL_BOOT" = "yes" ]; then
	# can't fall back to normal-boot now, we're going with serel
	#
	serelhistory --apply-started

	toggle="service"

	for i in $SEREL_PAIRS; do
		if [ "$toggle" = "service" ]; then
			toggle="path"
			subsys=$i
			continue
		else
			toggle="service"
			path=$i
		fi

		egrep -q "(daemon |action )" $path

		if [ $? = "0" -o "$subsys" = "local" ]; then
			serel-wrap $path start &
		else

			# serel_action()
			# A replacement for action() in /etc/rc.d/init.d/functions
			# that runs the script arguments in the background.
			#
			# I have a vague worry about a subtle race condition lurking
			# both here and in the regular non-serel boot.  initlog's
			# behaviour apepars undefined unless syslog has started, so what
			# about when this routine gets called prior to syslog starting?
			# Must investigate further!
			#
			serel_action() {
				STRING=$1
				echo -n "$STRING "
				shift
				/bin/bash -s << END &
				. /etc/init.d/functions
			
				initlog $INITLOG_ARGS -c "serel-wrap $*" && \
				            success $"$STRING" || failure $"$STRING"
  				rc=$?
  				echo
  				exit $rc
END
			}

			serel_action $"Starting $subsys: " $path start
		fi
	done

	wait

	# No need to generate a new graph if --populate reused an old history.
	#
	serelhistory --query prior-history | grep -v "success" 2>&1 > /dev/null
	[ -n "$SEREL_GENERATE_RDF" -a $? = "0" ] && serel-rdfgen > $SEREL_GENERATE_RDF

	serelc --control stop

	SEREL_BOOT=
fi
