#! /bin/bash
#
# Copyright (c) 1996-2004 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Florian La Roche, 1996
#	  Werner Fink <werner@suse.de> 1994-99,2000-2004
#
# Please send feedback to http://www.suse.de/feedback
#
# /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.
#

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

#
# 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

#
# Up from runlevel N we may want to run in confirm mode
#
if test "$PREVLEVEL" = "N" ; then
    test -e /proc/cmdline || mount -n -t proc proc /proc
    read -t 2 check < /proc/cmdline 2> /dev/null
    case "$check" in
	*confirm*) DO_CONFIRM=yes ;;
    esac
    test -e /var/run/do_confirm && DO_CONFIRM=yes
    rm -f   /var/run/do_confirm
    export DO_CONFIRM
fi

runrc=/etc/init.d/rc${RUNLEVEL}.d
prerc=/etc/init.d/rc${PREVLEVEL}.d

#
# Initialize boosplash progressbar variables
#
. /etc/rc.splash $RUNLEVEL

#
# Up from runlevel S we have to check if the system was
# cold booted into single user mode and call /etc/init.d/boot.
#
if test "$PREVLEVEL" = "S" ; then
    # Double check
    single="no"
    hvname="yes"
    #
    check=""
    test -e /proc/cmdline || mount -n -t proc proc /proc
    read -t 2 check < /proc/cmdline 2> /dev/null
    case "$check" in
	*single*)   single="yes"
    esac
    read -t 2 check < /proc/sys/kernel/hostname 2> /dev/null
    case "$check" in
	unknown|"") hvname="no"
    esac
    unset check

    if test "$single" = "yes" -a "$hvname" = "no" ; then
	# This is the case if we're up from cold boot
	echo "Sending all processes the TERM signal..."
	killall5 -15
	sleep 3
	echo "Sending all processes the KILL signal..."
	killall5 -9
	cat /proc/mounts | while read des fs type rest; do
	    case "$type" in
		proc)	continue ;;
		nfs)	continue ;;
		smbfs)	continue ;;
	    esac
	    umount -n -r $fs
	done
	cat /proc/swaps | while read des rest ; do
	    test "$des" = "Filename" && continue
	    swapoff $des &> /dev/null
	done
	umount -n -t proc /proc
	/etc/init.d/boot
	exit 0
    fi
fi

#
# 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

#
# Dummy shell function
#
chvt () { true; }

#
# 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 -x /sbin/isserial && /sbin/isserial ; then
    # Serial: columns and lines are not defined
    : ${LINES:=24}
    : ${COLUMNS:=80}
    test "$TERM" = "linux" -o -z "$TERM" && TERM=vt102
else
    # VGA
    if test "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "6" ; then
	case "$REDIRECT" in /dev/tty[1-9]*)
	    REDIRECT=/dev/tty1
	    setconsole $REDIRECT < $CONSOLE
	    # Overwrite chvt shell function
	    chvt () { test -x /usr/bin/chvt && /usr/bin/chvt 1 ; }
	    set -- $(stty size < $REDIRECT)
	    LINES=$1
	    COLUMNS=$2
	    export LINES COLUMNS
	esac
    fi
fi

#
# May change to first terminal, but first we kill all vlock sessions
#
killall -q /usr/bin/vlock &> /dev/null || true
chvt

#
# Configuration and coloring of the boot messages
#

. /etc/rc.status
. /etc/sysconfig/boot

#
# Save old terminal settings and set -nl to avoid staircase
# effect, do not lock scrolling, and avoid break characters.
#
otty=$(stty -g)
if test "$FLOW_CONTROL" = "yes" ; then
    stty -nl  ixon ignbrk -brkint
else
    stty -nl -ixon ignbrk -brkint
fi

#
# Start blogd if not in single user mode
#
if test "$RUNLEVEL" != "S" -a -x /sbin/blogd -a -n "$REDIRECT" ; then
    /sbin/blogd $REDIRECT
fi
if test -z "$REDIRECT" ; then
    if (echo -n > /dev/tty) 2>/dev/null ; then
	REDIRECT=/dev/tty
    else
	REDIRECT=/dev/console
    fi
fi

if test "$DO_CONFIRM" = "yes" ; then
    echo -n "Master Resource Control: "
    echo -e "${stat}${extd}Interactive startup${norm}"
fi
echo -n "Master Resource Control: "
echo -n "previous runlevel: $PREVLEVEL, "
echo -e "switching to runlevel: ${stat}${extd}${RUNLEVEL}${norm}"

splashtrigger "rlchange $RUNLEVEL"

#
# Start the user defined resource control script which
# should be executed _before_ runlevel is entered.
#
if test -f /etc/init.d/before.local ; then
    echo -n "Master Resource Control: "
    echo "Running /etc/init.d/before.local"
    /bin/sh /etc/init.d/before.local
    rc_status -v1 -r
    splashtrigger "before.local"
fi

rex="[0-9][0-9]"
failed=""
skipped=""
callord=""

#
# Check if we are able to use make like booting
#
test -s /etc/init.d/.depend.start || RUN_PARALLEL="no"
test -s /etc/init.d/.depend.stop  || RUN_PARALLEL="no"
test "$DO_CONFIRM"   = "yes"	  && RUN_PARALLEL="no"
type -p startpar &> /dev/null     || RUN_PARALLEL="no"
startpar -v      &> /dev/null	  || RUN_PARALLEL="no"

#
# Stop/Start services to enter new runlevel
#
if test "$RUN_PARALLEL" = "yes" ; then

    #
    # Stop/Start services in parallel with make behaviour of startpar
    #

    startopt="-p4 -t 30 -T 3 $(splashmake)"
    eval $(startpar $startopt -M stop  -P $PREVLEVEL -R $RUNLEVEL)
    failed="${failed:+$failed }$failed_service"
    skipped="${skipped:+$skipped }$skipped_service"

    eval $(startpar $startopt -M start -P $PREVLEVEL -R $RUNLEVEL)
    failed="${failed:+$failed }$failed_service"
    skipped="${skipped:+$skipped }$skipped_service"

    unset failed_service skipped_service startopt 

else

    #
    # 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.
	#
	K=${i##*/}
	service=${K#*K$rex}
	K=${K%$service}
	set -- $runrc/S$rex$service
	test $# -gt 1 && echo -e "$attn$runrc/: more than one link for service $service$norm"
	test -x "$1"  && continue

	blogger "$service stop"
	$i stop; status=$?
	echo -en "$rc_reset"
	splashprogress "$service stop"

	case $status in
	[1-4])	 failed="${failed:+$failed }$service"	;;
	[56])	skipped="${skipped:+$skipped }$service" ;;
	[07]|*)	;;
	esac

	blogger "'$service 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.
	#
	S=${i##*/}
	service=${S#*S$rex}
	S=${S%$service}
	set -- $prerc/K$rex$service
	test $# -gt 1 && echo -e "$attn$prerc/: more than one link for service $service$norm"
	test -x "$1"  && continue

	#
	# Ask the user if wanted
	#
	if test "$DO_CONFIRM" = "yes" ; then
	    rc_confirm -t 5 "Start service $service"
	    case "$?" in
	    0) true	;;
	    1) continue	;;
	    2) DO_CONFIRM=""
	    esac
	fi

	blogger "$service start"
	$i start; status=$?
        echo -en "$rc_reset"
	splashprogress "$service start"

	case "$status" in
	[1-47])	 failed="${failed:+$failed }$service"	;;
	[56])	skipped="${skipped:+$skipped }$service" ;;
	0|*)	;;
	esac

	blogger "'$service start' exits with status $status"
    done
fi

#
# 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")
#

#
# Start the user defined resource control script which
# should be executed _after_ runlevel is reached.
#
if test -f /etc/init.d/after.local ; then
    echo -n "Master Resource Control: "
    echo "Running /etc/init.d/after.local"
    /bin/sh /etc/init.d/after.local
    rc_status -v1 -r
    splashtrigger "after.local"
fi

splashtrigger "rlreached $RUNLEVEL"

#
# Be verbose and print stats
#
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

#
# Check for boot cycle detection
#
test -s /lib/bootcycle/stats && . /lib/bootcycle/stats

#
# Stop blogd if running
#
test -x /sbin/blogd && killproc -QUIT /sbin/blogd

#
# Restore old terminal settings
#
stty $otty

#
# 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
