#! /bin/bash
#
# Copyright (c) 1996-2004 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Ruediger Oertel <ro@suse.de>, 1996-2002
#         Werner Fink <werner@suse.de>, 1996-2004
#         Burchard Steinbild, 1996-2000
#         Florian La Roche, 1996
#
# Please send feedback to http://www.suse.de/feedback
#
# /etc/init.d/boot
#
# first script to be executed from init on system startup
#

#
# Avoid be interrupted by child or keyboard
# 
trap "echo" SIGINT SIGSEGV SIGQUIT
set +e

#
# Before we do anything else, try at first to activate SELinux support
#
test -d /selinux && mount -n -t selinuxfs none /selinux &> /dev/null

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

#
# Export the real device in variable REDIRECT.
#
REDIRECT="`showconsole 2>/dev/null`"
export REDIRECT
if test $COLUMNS -eq 0 ; then
    # 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/sysconfig/boot

echo -n "System Boot Control: "
echo "Running $0"

#
# Start of e.g. blogd, requires /proc and /dev/pts.
#
rc_reset
echo -n "Mounting /proc filesystem"
mount -n -t proc proc /proc
rc_status -v -r

test -d /sys && {
    while read a b c  ; do
	case "$b" in 
	    sysfs)
		echo -n "Mounting sysfs on /sys"
		mount -n -t sysfs sysfs /sys
		rc_status -v -r
		break
		;;
	esac
    done < /proc/filesystems
}

echo -n "Mounting /dev/pts"
optpts="-o mode=0620,gid=5"
mount -n -t devpts $optpts devpts /dev/pts
rc_status -v -r

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

#
# Should we ask for interactive boot mode
#
DO_CONFIRM=""
read -t 2 check < /proc/cmdline 2> /dev/null
case "$check" in
    *confirm*) DO_CONFIRM=yes ;;
esac
test -z "$CONFIRM_PROMPT_TIMEOUT" && CONFIRM_PROMPT_TIMEOUT=5
if test "$PROMPT_FOR_CONFIRM" = "yes" -a "$DO_CONFIRM" != "yes" ; then
    echo -en "${extd}Enter Interactive startup mode?${norm}"
    rc_timer_on "$CONFIRM_PROMPT_TIMEOUT" 37
    read -t "$CONFIRM_PROMPT_TIMEOUT" -n 1 \
	-p " ${extd}y${norm}/[${extd}n${norm}]("${CONFIRM_PROMPT_TIMEOUT}s") " answer
    rc_timer_off
    case "$answer" in
    [yYiI]) DO_CONFIRM=yes ;;
    *)      DO_CONFIRM=    ;;
    esac
    unset answer
    echo
fi
export DO_CONFIRM

#
# Start blogd, requires /proc and /dev/pts.
# Export the real device in variable REDIRECT.
#
test -x /sbin/blogd -a -n "$REDIRECT" && /sbin/blogd $REDIRECT
if test -z "$REDIRECT" ; then
    if (echo -n > /dev/tty) 2>/dev/null ; then
	REDIRECT=/dev/tty
    else
	REDIRECT=/dev/console
    fi
fi

#
# Common used variables
#
bootrc=/etc/init.d/boot.d
rex="[0-9][0-9]"
failed=""
skipped=""
callord=""

#
# Initialize boosplash progressbar variables
#
. /etc/rc.splash B
splashtrigger "rlchange B"

#
# Check if we are able to use make for booting
#
test -s /etc/init.d/.depend.boot || RUN_PARALLEL="no"
test -s /etc/init.d/Makefile	 || 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"

#
# Start bootup client scripts.
#
if test "$RUN_PARALLEL" = "yes" ; then

    #
    # Start scripts in parallel with make behaviour of startpar
    #

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

    unset failed_service skipped_service startopt

elif test -d ${bootrc}/ ; then

    #
    # Start scripts in given link order
    #
    for i in ${bootrc}/S${rex}*; do

	test -f $i || continue

	B=${i##*/}
	script=${B#*S$rex}
	B=${B%$script}

	if test "$DO_CONFIRM" = "yes" ; then
	    rc_confirm -t 5 "Start bootup script ${script}" < $REDIRECT > $REDIRECT 2>&1
	    case "$?" in
	    0) true     ;;
	    1) continue ;;
	    2) DO_CONFIRM=""
	    esac
	fi

	blogger "$script start"

	if test -x "$i" ; then
		$i start
	else
	    echo Running $i
	    /bin/sh $i b
	    rc_status -v1 -r
	fi
	status=$?
	echo -en "$rc_reset"
	splashprogress "$script start"

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

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

splashtrigger "rlreached B"

#
# Be verbose and print stats
#
echo -n "System Boot Control: "
echo -e "The system has been ${stat}${extd}set up${norm}"
if test -n "$failed" ; then
    n=$((${#failed} + 7))
    echo -n  "Failed features: "
    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 features: "
    test $n -lt 47 && echo -en "\033[${COLUMNS}C\033[${n}D"
    echo -e  "${attn}${skipped}${norm}"
fi

#
# Start user defined bootup script.
#
if test -f /etc/init.d/boot.local ; then
    echo -n "System Boot Control: "
    echo "Running /etc/init.d/boot.local"
    /bin/sh /etc/init.d/boot.local
    rc_status -v1 -r
    splashtrigger "boot.local"
fi

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

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

#
# Remember for master resource script
#
if test "$DO_CONFIRM" = "yes" ; then
    touch /var/run/do_confirm
fi

#
# Restore old terminal settings
#
stty $otty

#
# Let YaST2 finish its installation, if you installed with YaST2
#
if test -f /var/lib/YaST2/runme_at_boot ; then
    HOSTTYPE=$(uname -m)
    splashtrigger "YaST"
    exec 0<> $REDIRECT 1>&0 2>&0
    # if yast2 failed, this ensures proper system setup
    touch /var/lib/YaST2/run_suseconfig
    if test -x /usr/lib/YaST2/startup/YaST2.Second-Stage; then
	/usr/lib/YaST2/startup/YaST2.Second-Stage
    elif test -x /usr/lib/YaST2/bin/YaST2.firstboot; then
	/usr/lib/YaST2/bin/YaST2.firstboot
    else
	# oops, yast2 not installed
	rm -f /var/lib/YaST2/runme_at_boot
    fi
fi
# run SuSEconfig (with args) if needed
if test -f /var/lib/YaST2/run_suseconfig ; then
    /sbin/SuSEconfig `cat /var/lib/YaST2/run_suseconfig`
    rm -f /var/lib/YaST2/run_suseconfig
fi

rc_reset
exit 0
