#!/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>
#
[ -f /lib/aurora/functions -a -f /etc/aurora/Monitor ] && /etc/aurora/Monitor startable && aurora=1

# Source function library.
. /etc/init.d/functions
[ -n "$aurora" ] && . /lib/aurora/functions
    
# Make sure console is using correct map table
[ -f /etc/init.d/mandrake_consmap ] && . /etc/init.d/mandrake_consmap

# We do not want these to be recalculated, especially if Aurora is running
# (Named pipes has same strange affectance on /sbin/consoletype).
export BOOTUP MOVE_TO_COL SETCOLOR_SUCCESS SETCOLOR_FAILURE SETCOLOR_WARNING SETCOLOR_NORMAL LOGLEVEL

# 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

# Start Aurora output
exec 3<&0
exec 4>&1
exec 5>&2
if [ -n "$aurora" ] && /etc/aurora/Monitor query ; then
 /usr/bin/chvt 11
 export DISPLAY=:1
 exec < /lib/aurora/output
 exec > /lib/aurora/input
 exec 2>&1
 aurora_cmd
  aurora_set_runlevel $newrunlevel
  aurora_set_missionsize $(ls /etc/rc.d/rc$newrunlevel.d | grep "" -c)
  aurora_reset_dialog ync
 aurora_end
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

initsplash

# See if we want to be in user confirmation mode
if [ -n "$aurora" ] && /etc/aurora/Monitor query ; then
 aurora_cmd
  aurora_get_confirm CONFIRM
 aurora_end
 if [ "$CONFIRM" = "no" ] ; then
  CONFIRM=
 fi
else
  if [ "$previous" = "N" ]; then
    if grep -i confirm /proc/cmdline >/dev/null || [ -f /var/run/confirm ] ; then
      rm -f /var/run/confirm
      CONFIRM=yes
      gprintf "Entering interactive startup\n"
      if [ "$ASKRUNLEVEL" != "no" ] && [ -x /sbin/askrunlevel ];then
	    /sbin/askrunlevel --interactive
      fi
    else
      /bin/grep -q quiet /proc/cmdline && /bin/grep -vq noquiet /proc/cmdline && export quiet=yes
      [[ "$splash" = "yes" && -n "$pw" && -f /proc/splash ]] && grep -q "Splash.*on" /proc/splash && export progress=yes
      CONFIRM=
      gprintf "Entering non-interactive startup\n"
    fi
  elif [[ "$newrunlevel" = "0" || "$newrunlevel" = "6" ]]; then
      if [[ "$splash" = "yes" && -n "$pw" && -f /proc/splash ]]; then 
	  export progress=yes
	  echo 1 > /proc/splash
      fi
      if /bin/grep -q quiet /proc/cmdline && /bin/grep -vq noquiet /proc/cmdline; then
	  export quiet=yes
	  [[ -x /usr/bin/clear ]] && /usr/bin/clear
	  echo
	  [[ "$newrunlevel" = "0" ]] && gprintf "Shutting down, please wait..."
          [[ "$newrunlevel" = "6" ]] && gprintf "Rebooting, please wait..."
	  echo
      fi
  fi
fi

runlevel="$newrunlevel"

initquiet
quiet on

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

# 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 script is there.
		[ ! -f $i ] && continue

		# Don't run [KS]??foo.{rpmsave,rpmorig} scripts
		[ "${i%.rpmsave}" != "${i}" ] && continue
		[ "${i%.rpmorig}" != "${i}" ] && continue
		[ "${i%.rpmnew}" != "${i}" ] && continue

		a=$[service++]

		progress $service

		if egrep -q '^#[[:space:]]+halt:[[:space:]]+yes' $i;then
		    $i stop
		fi

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

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

        [[ "$runlevel" = 6 || "$runlevel" = 0 ]] && quiet off
	# Now run the START scripts.
	for i in /etc/rc$runlevel.d/S*; do
		# Check if the script is there.
		[ ! -f $i ] && continue

		# Don't run [KS]??foo.{rpmsave,rpmorig} scripts
		[ "${i%.rpmsave}" != "${i}" ] && continue
		[ "${i%.rpmorig}" != "${i}" ] && continue
		[ "${i%.rpmnew}" != "${i}" ] && continue

		a=$[service++]

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

		if [[ "$subsys" = "kudzu" && "$quiet" = "yes" ]]; then
		        quiet off
			export quiet=stop
		fi
		  
		progress $service

		# Bring the subsystem up.
		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
			    if [[ "$splash" = "yes" ]];then
				if [[ $LOGO_CONSOLE != yes ]] && [[ $LOGO_CONSOLE != YES ]] && [[ $LOGO_CONSOLE != Yes ]]; then
				    if [[ -e /proc/splash ]];then
					action "Disabling Boot logo" echo 0 > /proc/splash
					export pc=0x000000
					progress $nbservices
				    fi
				fi
			    fi
			    $i start
			else
			    action "Starting %s: " $subsys $i start
			fi
		fi
		  
		if [[ "$quiet" = "stop" ]]; then
			export quiet=yes
		        quiet on
		fi
	done
fi

# If we are using Xdm/Gdm/Kdm, we definitely
# do not want to be in X when returning to init...
# Starting Xdm from within X sometimes bugs strangely...
if [ -n "$aurora" ] && /etc/aurora/Monitor query ; then
 /usr/bin/chvt 1
fi
