#!/bin/bash
#
# rc            This file is responsible for starting/stopping
#               services when the runlevel changes. It is also
#               responsible for the very first setup of basic
#               things, such as setting the hostname.
#
# Original Author:       
#               Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#

set -m

# 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
	return 0
}

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

# Source function library.
. /etc/init.d/functions

# check if we use upstart
UPSTART=
[ -x /sbin/initctl ] && UPSTART=yes

# initialize boosplash progressbar variables
runrc=/etc/rc.d/rc$runlevel.d
prerc=/etc/rc.d/rc$previous.d

SSC=($runrc/S*)
case "$SSC" in
	*\*) sscripts=0 ;;
	*) sscripts=${#SSC[*]}
esac

if [ "$previous" != "N" ]; then
	KSC=($prerc/K*)
	case "$KSC" in
		*\*)
		kscripts=0
		;;
		*)
		kscripts=${#KSC[*]}
		;;
	esac
fi

if [ "$previous" = "N" ]; then
	SSC=($runrc/S*)
	case "$SSC" in
		*\*)
		sscripts=0
		;;
		*)
		progress=${#SSC[*]}
		sscripts=$((${#SSC[*]} + $sscripts))
		;;
	esac
else
	progress=0
fi

#if [ "$previous" = "N" -a "$runlevel" = "5" ]; then
#	progress=20
#	sscripts=$(( $sscripts+19 ))
#else
#	progress=0
#fi

export sscripts kscripts progress

if [ "$runlevel" =  "0" -o "$runlevel" = "6" ]; then
#    if [ -f /etc/bootsplash/themes/current/config/bootsplash-`/sbin/fbresolution`.cfg ]; then
#        /sbin/splash -s /etc/bootsplash/themes/current/config/bootsplash-`/sbin/fbresolution`.cfg
#    fi
    rc_splash "splash start"
    rc_splash "shutdown"
fi


# See if we want to be in user confirmation mode
if [ "$previous" = "N" ]; then
    if grep -i confirm /proc/cmdline >/dev/null || [ -f /var/run/confirm ] ; then
      rm -f /var/run/confirm
      CONFIRM=yes
      echo $"Entering interactive startup"
    else
      CONFIRM=
      echo $"Entering non-interactive startup"
    fi
fi

export CONFIRM

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

# Is there an rc directory for this new runlevel?
if [ -d /etc/rc$runlevel.d ]; then
	# First, run the KILL scripts.
	for i in /etc/rc$runlevel.d/K*; do
		# 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
		check_runlevel "$i" || continue

		# send information to bootsplash handler.
		rc_splash "$i stop"

		# Bring the subsystem down.
		[ -n "$UPSTART" ] && initctl emit --quiet "stopping $subsys"
		if egrep -q "(killproc |action )" $i ; then
			$i stop
		else
			action $"Stopping $subsys: " $i stop
		fi
		[ -n "$UPSTART" ] && initctl emit --quiet "stopped $subsys"
	done

	# Now run the START scripts.
	for i in /etc/rc$runlevel.d/S*; do
		# Check if the subsystem is already up.
		subsys=${i#/etc/rc$runlevel.d/S??}
		[ -f /var/lock/subsys/$subsys ] && continue
		[ -f /var/lock/subsys/${subsys}.init ] && continue
		check_runlevel "$i" || continue
		    
		# If we're in confirmation mode, get user confirmation
		[ -n "$CONFIRM" ]  && 
		  { 
		    confirm $subsys
		    case $? in
		      0)
		        :
		      ;;
		      2)
		        CONFIRM=
		      ;;
		      *)
		        continue
		      ;;
		    esac 
		  }

		# send information to bootsplash handler.
		rc_splash "$i start"

		# Bring the subsystem up.
		[ -n "$UPSTART" ] && initctl emit --quiet "starting $subsys"
		if egrep -q "(daemon |action )" $i ; then
			if [ "$subsys" = "halt" -o "$subsys" = "reboot" ]; then
				unset LANG
				unset LC_ALL
				unset TEXTDOMAIN
				unset TEXTDOMAINDIR
				exec $i start
			else
			    $i start
			fi
		else
			if [ "$subsys" = "halt" -o "$subsys" = "reboot" -o "$subsys" = "single" -o "$subsys" = "local" ]; then
			    if [ "$subsys" = "halt" -o "$subsys" = "reboot" ]; then
				unset LANG
				unset LC_ALL
				unset TEXTDOMAIN
				unset TEXTDOMAINDIR
				exec $i start
			    fi
			    $i start
			else
			    action $"Starting $subsys: " $i start
			fi
		fi
		[ -n "$UPSTART" ] && initctl emit --quiet "started $subsys"
	done
fi

rc_splash "master"
