#! /bin/bash
#
# Copyright (c) 1996-2002 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Florian La Roche <feedback@suse.de> 1996
#	  Werner Fink <werner@suse.de> 1994-99,2000-2001
#
# /etc/init.d/rc   --    The Master Resource Control Script
#
# This file is responsible  for  starting/stopping  services
# when the runlevel changes.  If the action for a particular
# feature in the new run-level is the same as  the action in
# the previous run-level, this script will neither start nor
# stop that feature.
#

#
# On previous runlevel identical with current runlevel do not
# re-examine current runlevel.
#
test "$PREVLEVEL" = "$RUNLEVEL" && exit 0

#
# Coming from runlevel 1 we do not need to enter S
#
test "$PREVLEVEL" = "1" -a "$RUNLEVEL" = "S" && exit 0

#
# Coming from runlevel S we have to check if the system was
# booted cold into single user mode and call /etc/init.d/boot.  
#
if test "$PREVLEVEL" = "S" -a -x /sbin/runlevel ; then
    # Double check
    utmp=$(LC_ALL=POSIX /sbin/runlevel 2> /dev/null)
    dpts="no"
    proc="no"
    if test -e /proc/mounts ; then
	proc="yes"
	while read des fs type rest; do
	    case "$fs" in /dev/pts) dpts="yes" ;; esac
	done < /proc/mounts
    fi
    unset des fs type rest
    if test "$utmp" = "unknown" -a "$dpts" = "no" ; then
	test "$proc" = "yes" && umount -n -t proc
	/etc/init.d/boot
    fi
fi

#
# Avoid being interrupted by child or keyboard
#
trap "echo" SIGINT SIGSEGV SIGQUIT
trap 'test "$RUNLEVEL" = "1" && exit 0' SIGTERM
set +e

#
# Get terminal size of standard input of the system console
#
test -z "$CONSOLE" && CONSOLE=/dev/console
set -- $(stty size < $CONSOLE)
  LINES=$1
COLUMNS=$2
export LINES COLUMNS CONSOLE

#
# Set I/O of this script and its childs to console
#
exec 0<> $CONSOLE 1>&0 2>&0

#
# This redirects all rc messages during reboot and halt
# to tty1 if the system console is bound on VGA (tty0).
#
REDIRECT="`showconsole 2>/dev/null`"
export REDIRECT
if test $COLUMNS -gt 0 ; then
    # VGA
    if test "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "6" ; then
	case "$REDIRECT" in /dev/tty[1-9]*)
	    REDIRECT=/dev/tty1
	    setconsole $REDIRECT < $CONSOLE
	    set -- $(stty size < $REDIRECT)
	    LINES=$1
	    COLUMNS=$2
	    export LINES COLUMNS
	esac
    fi
else
    # Serial: columns and lines are not defined
      LINES=24
    COLUMNS=80
    test "$TERM" = "linux" -o -z "$TERM" && TERM=vt102
fi

#
# Configuration and coloring of the boot messages
#

. /etc/rc.status
. /etc/rc.config

#
# Save old terminal settings and set -nl to avoid staircase
# effect, do not lock scrolling, and avoid break characters.
#
otty=$(stty -g)
stty -nl -ixon ignbrk -brkint 0>&1

#
# Start blogd if not in single user mode
#
if test "$RUNLEVEL" != "S" -a -x /sbin/blogd ; then
    /sbin/blogd $REDIRECT
fi

echo -n "Master Resource Control: "
echo -n "previous runlevel: $PREVLEVEL, "
echo -e "switching to runlevel: ${stat}${extd}${RUNLEVEL}${norm}"

runrc=/etc/init.d/rc${RUNLEVEL}.d
prerc=/etc/init.d/rc${PREVLEVEL}.d
rex="[0-9][0-9]"
failed=""
skipped=""

#
# First check and test services of previous and current (new) runlevel.
#
for i in $prerc/K${rex}*; do
    test -x "$i" || continue

    #
    # Don't stop service if the new runlevel requires the service.
    #
    service=${i#*/K$rex}
    set -- $runrc/S$rex$service
    test $# -gt 1 && echo -e "$attn$runrc/: more than one link for service $service$norm"
    test -x "$1"  && continue

    # send information to splash icon viewer.
    rc_splash  "$i stop"

    # Log to /var/log/boot.msg
    blogger "$i stop"

    # Stop the services of the previous runlevel if they are missed
    # within the current (new) runlevel.

    $i stop; status=$?
    case $status in
	[1-4])	 failed="${failed}  ${service}" ;;
	[56])	skipped="${skipped} ${service}" ;;
	[07]|*)	;;
    esac
    echo -en "$rc_reset"

    # Log to /var/log/boot.msg
    blogger "'$i stop' exits with status $status"
done

for i in $runrc/S${rex}*; do
    test -x "$i" || continue

    #
    # Don't start service if previous runlevel includes the service.
    #
    service=${i#*/S$rex}
    set -- $prerc/K$rex$service
    test $# -gt 1 && echo -e "$attn$prerc/: more than one link for service $service$norm"
    test -x "$1"  && continue

    # send information to splash icon viewer.
    rc_splash  "$i start"

    # Log to /var/log/boot.msg
    blogger "$i start"

    # Start the services of the current (new) runlevel if they are missed
    # within the previous runlevel.

    $i start; status=$?
    case $status in
	[1-47])	 failed="${failed}  ${service}" ;;
	[56])	skipped="${skipped} ${service}" ;;
	0|*)	;;
    esac
    echo -en "$rc_reset"

    # Log to /var/log/boot.msg
    blogger "'$i start' exits with status $status"
done

#
# Inform the users
#   Reaction on LSB return values:
#    OK   : 0 - success
#    FAIL : 1 - generic or unspecified error
#    FAIL : 2 - invalid or excess argument(s)
#    FAIL : 3 - unimplemented feature (e.g. "reload")
#    FAIL : 4 - insufficient privilege
#    SKIP : 5 - program is not installed
#    SKIP : 6 - program is not configured
#    FAIL : 7 - program is not running (doing "start")
#    OK   : 7 - program is not running (doing "stop")
#

rc_splash "master"

echo -n "Master Resource Control: "
echo -e "runlevel ${RUNLEVEL} has been ${stat}${extd}reached${norm}"
if test -n "$failed" ; then
    n=$((${#failed} + 7))
    echo -n  "Failed services in runlevel ${RUNLEVEL}: "
    test $n -lt 47 && echo -en "\033[${COLUMNS}C\033[${n}D"
    echo -e  "${warn}${failed}${norm}"
fi
if test -n "$skipped" ; then
    n=$((${#skipped} + 7))
    echo -n  "Skipped services in runlevel ${RUNLEVEL}: "
    test $n -lt 47 && echo -en "\033[${COLUMNS}C\033[${n}D"
    echo -e  "${attn}${skipped}${norm}"
fi

#
# Stop blogd if running
#
killproc -QUIT /sbin/blogd

#
# Restore old terminal settings
#
stty $otty 0>&1

#
# For the first logon on a virtual console.
#
test -e /etc/issue-SuSE-first-run && {
    cat /etc/issue-SuSE-first-run > $REDIRECT
    rm -f /etc/issue-SuSE-first-run 2>/dev/null
}

exit 0