# 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 runlevel 2 at the moment...
#
if [ $runlevel != "0" -a $runlevel != "6" ]; then

	# SEREL_BOOT might be set in here...
	#
	if [ -f /etc/default/serel ]; then
		. /etc/default/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/rcS.d/S*; do
		[ ! -f $i ] && continue

		suffix=${i#/etc/rcS.d/S[0-9][0-9]}
		suffix=${suffix/%.sh/} 

		SEREL_STARTED="$SEREL_STARTED $suffix"
	done

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

		suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9]}
		suffix=${suffix/%.sh/} 

		if [ $previous != N ] && [ $previous != S ] 
		then
			#
			# Find start script in previous runlevel and
			# stop script in this runlevel.
			#
			stop=/etc/rc$runlevel.d/K[0-9][0-9]$suffix
			previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
			#
			# If there is a start script in the previous level
			# and _no_ stop script in this level, we don't
			# have to re-start the service.
			#
			[ -f $previous_start ] && [ ! -f $stop ]  \
			  && SEREL_STARTED="$SEREL_STARTED $suffix" && continue 
		fi

		SEREL_PAIRS="$SEREL_PAIRS $suffix $i"
	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

		# serel_startup()
		# A replacement for serel_startup() in /etc/init.d/rc
		#
		serel_startup() {
		  case "$1" in
			*.sh)
				$debug serel-wrap sh "$@" &
				;;
			*)
				$debug serel-wrap "$@" &
				;;
		  esac
		}

		serel_startup $path start
	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
