#!/bin/sh
#
# 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.
#
# $Id: rc,v 1.26 2000/11/16 16:01:17 baggins Exp $
#
# Original Author:       
#               Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
# Changes:      Arkadiusz Mikiewicz <misiek@pld.org.pl>
#

# NLS
if [ -f /etc/sysconfig/i18n ]; then
        . /etc/sysconfig/i18n
        [ -n "$LANG" ] && export LANG || unset LANG
        [ -n "$LC_CTYPE" ] && export LC_CTYPE || unset LC_CTYPE
        [ -n "$LC_COLLATE" ] && export LC_COLLATE || unset LC_COLLATE
        [ -n "$LC_MESSAGES" ] && export LC_MESSAGES || unset LC_MESSAGES
        [ -n "$LC_NUMERIC" ] && export LC_NUMERIC || unset LC_NUMERIC
        [ -n "$LC_MONETARY" ] && export LC_MONETARY || unset LC_MONETARY
        [ -n "$LC_TIME" ] && export LC_TIME || unset LC_TIME
        [ -n "$LC_ALL" ] && export LC_ALL || unset LC_ALL
        [ -n "$LANGUAGE" ] && export LANGUAGE || unset LANGUAGE
        [ -n "$LINGUAS" ] && export LINGUAS || unset LINGUAS
fi

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

# Read system config data.
if [ -f /etc/sysconfig/system ]; then
	. /etc/sysconfig/system
else
	RUN_SULOGIN_ON_ERR=yes
	RUN_ISAPNP=yes
	PANIC_REBOOT_TIME=0
	DELAY_LOGIN=yes
	CLEAN_TMP=no
	CONSOLE_LOGLEVEL=1
	LOAD_SOUND=yes
	SET_SLINKS=yes
	RUN_LDCONFIG=yes
fi

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

# if runlevel is 0 (halt) or 6 (reboot) change to first
# virtual console, and send messages to /dev/console 
# (it can be serial console too) 
if [ "$runlevel" = "0" ] || [ "$runlevel" = "6" ]; then
	[ -x /usr/bin/chvt ] && /usr/bin/chvt 1 && echo > /dev/console
	exec 0<> /dev/console 1>&0 2>&0
else
# if previous runlevel = current runlevel do nothing
	[ "$runlevel" = "$previous" ] && exit 0
fi

# See if we want to be in user confirmation mode
if [ "$previous" = "N" ]; then
	if grep -qi confirm /proc/cmdline >/dev/null \
		|| [ -f /var/run/confirm ]; then
		rm -f /var/run/confirm
		CONFIRM="yes"
		nls "Entering interactive startup"
	else
		CONFIRM=
		nls "Entering non-interactive startup"
	fi
fi
					  
# set onlcr to avoid staircase effect and do not lock scrolling
stty onlcr -ixon 0>&1

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

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


# Say something ;)
af2="`termput setaf 2`"
af6="`termput setaf 6`"
af7="`termput setaf 7`"
text="`nls "%sResource Manager: %sEntering runlevel number" "$af2" "$af7"`"
text_size="`nls "%sResource Manager: %sEntering runlevel number" "" ""`"
resp_size="`nls "DONE"`"
echo -n "$text"
awk "BEGIN { for (j=length(\"$text_size\"); j<$INIT_COL+${#resp_size}-${#runlevel}; j++) printf \".\" }"
echo "${af6}[${af2} $runlevel ${af6}]${af7}"

# Is there an rc directory for this new runlevel?
if [ -d /etc/rc.d/rc$runlevel.d ]; then
	# First, run the KILL scripts.
	for i in /etc/rc.d/rc$runlevel.d/K*; do
		# Check if the script is there.
		[ ! -f $i ] && continue

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

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

		# Bring the subsystem down.
		$i stop
	done

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

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

		# Check if the subsystem is already up.
		subsys=${i#/etc/rc.d/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
		}

		# Bring the subsystem up.
		$i start
	done
fi

# if runlevel is 0 (halt) or 6 (reboot) run rc.shutdown
if [ "$runlevel" = "0" ] || [ "$runlevel" = "6" ]; then
	/etc/rc.d/rc.shutdown
	if [ "$runlevel" = "0" ] ; then
        	show "The system is halted"; ok
		[ -f /fastboot ] && (show "On the next boot fsck will be skipped."; ok)
		eval halt -d -p
	else
		show "Please stand by while rebooting the system"; ok
		[ -f /fastboot ] && (show "On the next boot fsck will be skipped."; ok)
		eval reboot -d
	fi
else
	if is_yes "$RUN_LDCONFIG" || [ ! -f /etc/ld.so.cache ] ; then
		if [ -x /sbin/ldconfig ]; then
			run_cmd "Setting up /etc/ld.so.cache" /sbin/ldconfig -X
		fi
	fi
fi

# Say something ;)
text="`nls "%sResource Manager: %sRunlevel has been reached" "$af2" "$af7"`"
text_size="`nls "%sResource Manager: %sRunlevel has been reached" "" ""`"
echo -n "$text"
awk "BEGIN { for (j=length(\"$text_size\"); j<$INIT_COL+${#resp_size}-${#runlevel}; j++) printf \".\" }"
echo "${af6}[${af2} $runlevel ${af6}]${af7}"
unset af2 af6 af7

# This must be last line !
# vi:syntax=sh:tw=78:ts=8:sw=4
