#! /bin/bash
#
# rc            This file is responsible for starting/stopping
#               services when the runlevel changes.
#
# Original Author:       
#               Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#

# check a file to be a correct runlevel script
check_runlevel ()
{
	# Check if the file exists at all.
	[ -x "$1" ] || return 1

	# Reject backup files and files generated by rpm.
	case "$1" in
		*.rpmsave|*.rpmorig|*.rpmnew|*~|*.orig)
			return 1
			;;
	esac
	return 0
}

# Now find out what the current and what the previous runlevel are.
argv1="$1"
set `/sbin/runlevel`
runlevel=$2
previous=$1
export runlevel previous

. /etc/init.d/functions

# See if we want to be in user confirmation mode
if [ "$previous" = "N" ]; then
	if [ -f /var/run/confirm ]; then
		echo $"Entering interactive startup"
	else
		echo $"Entering non-interactive startup"
	fi
fi

# Get first argument. Set new runlevel to this argument.
[ -n "$argv1" ] && runlevel="$argv1"

# Is there an rc directory for this new runlevel?
[ -d /etc/rc$runlevel.d ] || exit 0

# bootsplash
if [ -f /etc/sysconfig/bootsplash ]; then
	. /etc/sysconfig/bootsplash
fi

[ -f /proc/splash ] || SPLASH="no"
[ -d /etc/bootsplash ] || SPLASH="no"
grep -q splash=silent /proc/cmdline || SPLASH="no"

if [ "$SPLASH" = "yes" ]; then
	bsthemedir=/etc/bootsplash/themes

	if [ -z "$THEME" ]; then
		THEME="Momonga"
	fi

	pushd $bsthemedir > /dev/null
	[ -L current ] && rm $bsthemedir/current
	ln -s $THEME current
	popd > /dev/null

	SSC=(/etc/rc$runlevel.d/S*)
	sscripts=${#SSC[*]}
	KSC=(/etc/rc$runlevel.d/K*)
	kscripts=${#KSC[*]}

	progress=0

	export SPLASH sscripts kscripts progress

	if [ -x /sbin/splash -a -x /sbin/fbresolution ]; then
		splash -s $bsthemedir/current/config/bootsplash-`fbresolution`.cfg
		echo silent > /proc/splash
	fi
fi

# First, run the KILL scripts.
for i in /etc/rc$runlevel.d/K* ; do
	if [ "$SPLASH" = "yes" ]; then
		progress=$(( $progress + 1))
		[ -x /sbin/splash.sh ] && /sbin/splash.sh "$i start"
	fi

	check_runlevel "$i" || continue

	# Check if the subsystem is already up.
	subsys=${i#/etc/rc$runlevel.d/K??}
	[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \
		|| continue

	# Bring the subsystem down.
	if egrep -q "(killproc |action )" $i ; then
		$i stop
	else
		action $"Stopping $subsys: " $i stop
	fi
done

# Now run the START scripts.
for i in /etc/rc$runlevel.d/S* ; do
	if [ "$SPLASH" = "yes" ]; then
		progress=$(( $progress + 1))
		[ -x /sbin/splash.sh ] && /sbin/splash.sh "$i start"
	fi

	check_runlevel "$i" || continue

	# Check if the subsystem is already up.
	subsys=${i#/etc/rc$runlevel.d/S??}
	[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \
		&& continue
		    
	# If we're in confirmation mode, get user confirmation
	if [ -f /var/run/confirm ]; then
		confirm $subsys
		test $? = 1 && continue
	fi

	update_boot_stage "$subsys"
	# Bring the subsystem up.
	if [ "$subsys" = "halt" -o "$subsys" = "reboot" ]; then
		export LC_ALL=C
		exec $i start
	fi
	if egrep -q "(daemon |action |success |failure )" $i 2>/dev/null \
			|| [ "$subsys" = "single" -o "$subsys" = "local" ]; then
		$i start
	else
		action $"Starting $subsys: " $i start
	fi
done
rm -f /var/run/confirm
[ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --quit
