#!/bin/sh
#
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../sysvinit/system.init
# Copyright (C) 2006 - 2008 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
# Copyright (C) 1998 - 2003 Clifford Wolf
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---
#
# Desc: System bootup and shutdown
# Runlevel: 01 rcX rc1 rc2 rc3 rc4 rc5
#
export PATH="/sbin:$PATH"

main_begin
    # we are assuming /proc and /dev are already mounted
    block_begin(start, `Populating /dev.')
       for x in $( cd /lib/udev/devices/; ls -1 ); do
	       if [ ! -L "/dev/./$x" -a ! -e "/dev/./$x" ];
	       then check(`cp -a /lib/udev/devices/$x /dev/$x')
	       fi
	done
dnl
    block_split(`Mounting /dev/shm and /dev/pts.')
	check(`mount -n /dev/shm')
	mount -n /dev/pts 2> /dev/null
dnl
    if [ -f /etc/conf/hardware ] ; then
	. /etc/conf/hardware
    fi
dnl
    [ -x /bin/dmesg ] && /bin/dmesg -n 3
dnl
    block_split(`Starting udevd ...')
	echo "" > /sys/kernel/uevent_helper
	check(`udevd --daemon')
    block_split(`Triggering coldplug ...')
	check(`udevtrigger')
	check(`udevsettle')

dnl
    if [ -s /etc/lvmtab -o -s /etc/lvm/.cache ]; then
	block_split(`Activating Volume Groups...')
	    modprobe dm-mod 2> /dev/null
	    check(`/sbin/vgchange -ay --ignorelockingfailure 2> /dev/null')
    fi
dnl
    block_split(`Checking file systems.')
	fsck -A -C -a ; fsckrc=$?
	if   [ $(( $fsckrc & ~3 )) != 0 ] ; then
		echo " **"
		echo " ** Filesystem check failed (returncode=$fsckrc)."
		echo " ** Please repair the broken disk(s) manually."
		echo " **"
		sulogin -t 600 /dev/console
		umount -arv ; /sbin/reboot -d -f
		while true ; do sleep 1 ; done
	elif [ $(( $fsckrc &  2 )) != 0 ] ; then
		for x in 10 9 8 7 6 5 4 3 2 ; do
			echo -en "\rSystem reboot in $x seconds ... "
			sleep 1
		done ; echo -e "\rSystem reboot now!                 "
		umount -arv ; /sbin/reboot -d -f
		while true ; do sleep 1 ; done
	fi
dnl
    block_split(`Mounting local file systems.')
	check(`mount -n -o remount,rw /')
	rm -f /etc/mtab~* # lingering lock files
	cat /proc/mounts > /etc/mtab
	check(`mount -a -t nocoda,nfs,devfs,proc,sysfs')
dnl
    block_split(`Activating swap devices.')
	check(`swapon -a')
dnl
    block_split(`Refresh utmp, delete lock and tmp files and other stuff.')
	find /var/lock /var/run /tmp -mindepth 1 -print0 2> /dev/null | xargs --null rm -rf
	rm -f /etc/nologin /nologin /fastboot ; touch /var/run/utmp
	chmod 664 /var/run/utmp ; chown root:tty /var/run/utmp
	mkdir /tmp/.ICE-unix && chmod 1777 /tmp/.ICE-unix
dnl
    block_split(`Setting hostname to $(cat /etc/HOSTNAME).')
	check(`hostname "$(cat /etc/HOSTNAME)"')
dnl
    block_split(`Configuring the kernel with /etc/conf/kernel.')
	check(`sh /etc/conf/kernel')
dnl
    [ -f /etc/conf/clock ] && . /etc/conf/clock
    if [ "$clock_tz" = localtime ] ; then
	block_split(`Setting kernel clock to local time.')
	    check(`hwclock --hctosys --localtime')
    fi
    if [ "$clock_rtc" ] ; then
	block_split(`Setting enhanced real time clock precision to $clock_rtc.')
	if [ -w /proc/sys/dev/rtc/max-user-freq ] ; then
	    check(`echo $clock_rtc > /proc/sys/dev/rtc/max-user-freq')
	else
	    echo "No /proc/sys/dev/rtc/max-user-freq found."
	fi
    fi
dnl
    block_split(`Writing /var/log/boot.msg.')
	check(`dmesg > /var/log/boot.msg')
dnl
    block_split(`Setting console screen font.')
	if [ -L /etc/default.vcfont ] ; then
		fontfile=$(ls -l /etc/default.vcfont | sed 's,.* -> ,,')
		check(`setfont $fontfile')
	elif [ -f /etc/default.vcfont ] ; then
		check(`setfont /etc/default.vcfont')
	else
		echo "No /etc/default.vcfont found."
	fi
dnl
    block_split(`Setting console terminal type and blank interval.')
	con_term=linux; con_blank=0
        [ -f /etc/conf/console ] && . /etc/conf/console
        check(`D_prefix/bin/setterm -term $con_term -blank $con_blank > /dev/console')
dnl
    block_split(`Setting up loopback networking.')
	check(`ip link set lo up')
	[[ "$(ip addr show lo)" = *127.0.0.1/8* ]] ||
		check(`ip addr add 127.0.0.1/8 dev lo')
	check(`ip route add 127/8 dev lo')
dnl
    block_split(`Setting overflow UID and GID kernel parameters.')
	check(`sysctl -w kernel.overflowuid=$(id -u nobody) > /dev/null')
	check(`sysctl -w kernel.overflowgid=$(id -g nobody) > /dev/null')
dnl
    block_split(`Reading /etc/sysctl.conf file.')
	check(`sysctl -p')
dnl
    block_split(`Initializing kernel random number generator.')
	if [ -e /var/state/random-seed ] ; then
		check(`cat /var/state/random-seed >/dev/urandom')
	fi
    block_end


    block_begin(stop, `Saving /var/log/init.msg and /var/log/boot.msg.')
	check(`touch /var/log/init.msg /var/log/boot.msg')
	check(`mv /var/log/init.msg /var/log/init.old')
	check(`mv /var/log/boot.msg /var/log/boot.old')
    block_split(`Writing a wtmp record.')
	if [ "$RUNLEVEL" = 0 ] ; then check(`halt -w')
	else check(`reboot -w') ; fi
    block_split(`Saving kernel random seed.')
	dd if=/dev/urandom of=/var/state/random-seed count=1 2> /dev/null
dnl
    block_split(`Sending all processes a SIGTERM (15).')
	check(`killall5 -15') ; sleep 5
    block_split(`Sending all processes a 2nd SIGTERM (15).')
	check(`killall5 -15') ; sleep 5
    block_split(`Sending all processes a SIGKILL (9).')
	check(`killall5 -9') ; sleep 5
dnl
    block_split(`Turning off swap devices.')
	check(`swapoff -a')
dnl
    block_split(`Remounting sync/ro and umount filesystems.')
	sync
	cut -d' ' -f-3 /etc/mtab /proc/mounts | sort -k2 -u -r |
	while read dev dir fs ; do
		case "$dir" in
		/|/dev|/proc|/sys|/tmp)	continue ;;
		*)	echo "Umounting $dev on $dir ($fs)."
			mount -o remount,sync $dir
			mount -o remount,ro $dir
			umount $dir ;;
		esac
	done
dnl
    block_split(`Unmounting remaining file systems.')
	grep -v -e '^none /[a-z]\+' -e ' / ' /proc/mounts > /etc/mtab
	mount -vn -o remount,sync /
	sync
	umount -vran -t nodevfs,proc,sysfs
	mount -vn -o remount,ro /
	sync ; sleep 1
dnl
    block_split()
	if [ "$INIT_DOES_REBOOTHALT" != "1" ]; then
	command=
	[ "$RUNLEVEL" = 0 ] && command=halt
	[ "$RUNLEVEL" = 6 ] && command=reboot
	if [ -n "$command" ] ; then
		echo "Going to $command the system ..."
		$command -d -f -i -p
		[ "$INIT_WAITS" != "1" ] && \
			while true ; do sleep 1 ; done
	fi
	fi
    block_end

main_end