#!/bin/sh
#
# ROCK Linux: /etc/rc.d/rc
#
# This script does the switching between runlevels.
#

{
export PATH=/bin:/sbin:/usr/bin:/usr/sbin

echo "** running rc script **"
echo "RC: Previous runlevel: $PREVLEVEL, new runlevel: $RUNLEVEL."

curdir=/etc/rc.d/rc$RUNLEVEL.d
prevdir=/etc/rc.d/rc$PREVLEVEL.d

#
# run the KILL scripts of the previous runlevel (if needed)
#
if ! [ $PREVLEVEL = S -o $PREVLEVEL = N ]
then
	echo "RC: Leave runlevel $PREVLEVEL ..."
	for i in $prevdir/K*; do
		test -x "$i" || continue
		x="`echo "$i" | sed "s,$prevdir/K..,$curdir/S??,"`"
		[ "`echo $x`" = "$x" ] || continue
		/sbin/rc --nobtee $i stop
	done
fi

echo "RC: Enter runlevel $RUNLEVEL ..."

if [ $RUNLEVEL = S ]
then
	#
	# Kill all processes
	#
	echo "Sending all processes a SIGTERM (15) ..."
	killall5 -15
	sleep 5
	echo "Sending all processes a 2nd SIGTERM (15) ..."
	killall5 -15
	sleep 3
	echo "Sending all processes a SIGKILL (9) ..."
	killall5 -9
	sleep 3
elif [ $RUNLEVEL != 0 -a $RUNLEVEL != 6 ]
then
	#
	# run the START scripts of the current (new) runlevel
	#
	for i in $curdir/S*; do
		test -x "$i" || continue
		x="`echo "$i" | sed "s,$curdir/S..,$prevdir/K??,"`"
		[ "`echo $x`" = "$x" ] || continue
		/sbin/rc --nobtee $i start
	done
fi

echo "RC: The system is now in runlevel $RUNLEVEL."

# write EOT mark for btee
echo -ne '\004'

} 2>&1 | /sbin/btee a /var/log/init.msg

exit 0
