#! /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
	is_ignored_file "$1" && return 1
        a=$[service++]
	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

# Get first argument. Set new runlevel to this argument.
if [ "$argv1" != "" ] ; then
 newrunlevel="$argv1"
else
 newrunlevel="$runlevel"
fi

if [ "$previous" = "N" ];then
    if [ -e /var/run/failsafe ];then
        rm -f /var/run/failsafe
	if [ -x /sbin/askrunlevel ];then
		/sbin/askrunlevel --interactive && ASKRUNLEVEL=no
	else
	    init 1 #jump to init 1 if not aksrunlevel.
       fi
    fi
fi

. /etc/init.d/functions

initsplash

# See if we want to be in user confirmation mode
if [ "$previous" = "N" ]; then
        rc_splash start
	if [ -f /var/run/confirm ]; then
		gprintf "Entering interactive startup\n"
	else
		gprintf "Entering non-interactive startup\n"
	fi
elif [[ "$newrunlevel" = "0" || "$newrunlevel" = "6" ]]; then
    chvt 1
    exec &> /dev/console
    # Make sure terminal is switched to correct charset.
    # It has to be after chvt of course. For startup case it really
    # does not matter as tty is not changed between rc.sysinit and rc
    # DO NOT REMOVE THIS. Please :)
    /sbin/setsysfont
fi

# Tell linuxconf what runlevel we are in
[ -d /var/run ] && echo "/etc/rc$runlevel.d" > /var/run/runlevel.dir

# 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

nopinit=`grep nopinit /proc/cmdline`

# First, run the KILL scripts.
if [ -z "$nopinit" ]; then
    /sbin/prcsys K /etc/rc$runlevel.d/
    progress=$?
else
    for i in /etc/rc$runlevel.d/K* ; do
	check_runlevel "$i" || continue

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

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

if [ -z "$nopinit" ] && [ ! -f /var/run/confirm -a "$runlevel" != "0" -a "$runlevel" != "1" -a "$runlevel" != "6" ]; then
    /sbin/prcsys S /etc/rc$runlevel.d/
else
    # Now run the START scripts.
    for i in /etc/rc$runlevel.d/S* ; do
	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
	        if [ "$subsys" = dm ]; then
		    CONFIRM_DM=1
		    continue
		fi
		confirm $subsys
		case $? in
		    1) continue;;
		    2) rm -f /var/run/confirm;;
		esac
	fi

	rc_splash $subsys
	
	# Bring the subsystem up.
	if [ "$subsys" = "halt" -o "$subsys" = "reboot" ]; then
		export LC_ALL=C
		exec $i start
	fi
	if [ "$subsys" = "single" ]; then
	    rc_splash stop
        fi
	if LC_ALL=C egrep -q "(daemon |action |success |failure )" $i 2>/dev/null \
			|| [ "$subsys" = "single" -o "$subsys" = "local" ]; then
		$i start
	else
		action "Starting %s: " $subsys $i start
	fi

   done

   # start dm service at the end in confirmation mode
   if [ "$CONFIRM_DM" = 1 ]; then
    if [ -f /var/run/confirm ]; then
	confirm dm
	case $? in
	    1) :;;
	    *) rc_splash dm; /etc/init.d/dm start;;
	esac
    else
	rc_splash dm
	/etc/init.d/dm start
    fi
   fi
fi

if [[ "$newrunlevel" != "0" && "$newrunlevel" != "6" ]]; then
    rc_splash stop
fi
rm -f /var/run/confirm
