#!/bin/sh
# Begin $rc_base/init.d/rc - Main Run Level Control Script

# Based on rc script from LFS-3.1 and earlier.
# Rewritten by Gerard Beekmans - gerard@linuxfromscratch.org
# 
# Modified to use variables below, instead of rc*.d directories with
# symbolic links by Neven Has - haski@sezampro.yu


# Additional user scripts, not part of the base LFS.

# Run level: 3
ADD[3]=""

# Run level: sysinit
ADD[7]=""


# Run level: 0
 STOP[0]="${ADD[3]} ${ADD[7]} sysklogd sendsignals mountfs swap localnet halt"
START[0]=""

# Run level: 1
 STOP[1]="sysklogd"
START[1]=""

# Run level: 2
 STOP[2]=""
START[2]="sysklogd"

# Run level: 3
 STOP[3]=""
START[3]="sysklogd ${ADD[3]}"

# Run level: 4
 STOP[4]=""
START[4]="sysklogd"

# Run level: 5
 STOP[5]=""
START[5]="sysklogd"

# Run level: 6
 STOP[6]="${ADD[3]} ${ADD[7]} sysklogd sendsignals mountfs swap localnet reboot"
START[6]=""

# Run level: sysinit
 STOP[7]=""
START[7]="swap mountproc checkfs mountfs cleanfs setclock loadkeys localnet \
	  ${ADD[7]}"


# There is probably a much better way to do this.
should_skip() {
	for i in $2 ; do if [ $i = $1 ] ; then return 1 ; fi ; done
	return 0
}

source /etc/sysconfig/rc
source $rc_functions

trap "" INT QUIT TSTP

[ "$1" != "" ] && runlevel=$1

if [ "$runlevel" = "" ]
then
	echo "Usage: $0 <runlevel>" >&2
	exit 1

elif [ $runlevel = sysinit ]
then
	runlevel=7
fi

previous=$PREVLEVEL
[ "$previous" = "" ] && previous=N

if [ "$previous" != "N" ]
then
	for i in ${STOP[$runlevel]}
	do
		script=$rc_base/init.d/$i

		# check_script_status
		if [ ! -x $script ]
		then
			echo "$script is not executable, skipping"
			continue
		fi

		if [ "$runlevel" != "0" ] && [ "$runlevel" != "6" ]
		then
			if [ should_skip $i ${START[$previous]} ] ||
			   [ should_skip $i ${START[7]} ]
			then
				$WARNING
				echo "$i can't be executed because it was"
				echo "not started in the previous runlevel ($previous)"
				$NORMAL
				continue
			fi
		fi

		$script stop
		error_value=$?

		if [ "$error_value" != "0" ]
		then
			print_error_msg
		fi
	done
fi

for i in ${START[$runlevel]}
do
	script=$rc_base/init.d/$i

	if [ "$previous" != "N" ]
	then
		if [ should_skip $i ${START[$previous]} ] ||
		   [ should_skip $i ${STOP[$runlevel]} ]
		then
			continue
		fi
	fi

	# check_script_status
	if [ ! -x $script ]
	then
		echo "$script is not executable, skipping"
		continue
	fi

	$script start
	error_value=$?

	if [ "$error_value" != "0" ]
	then
		print_error_msg
	fi
done

# End $rc_base/init.d/rc

