#!/bin/sh
#
# --- T2-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# 
# T2 SDE: package/.../sysfiles/system.init
# Copyright (C) 2004 - 2005 The T2 SDE Project
# Copyright (C) 1998 - 2003 ROCK Linux Project
# 
# 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.
# --- T2-COPYRIGHT-NOTE-END ---
#
# Desc: System bootup and shutdown
# Runlevel: 01 rcX rc1 rc2 rc3 rc4 rc5
#

title() {
	local x w="$( stty size 2>/dev/null </dev/tty | cut -d" " -f2  )"
	[ -z "$w" ] && w="$( stty size </dev/console | cut -d" " -f2  )"
	for (( x=1; x<w; x++ )) do echo -n .; done
	echo -e "\e[222G\e[3D v \r\e[36m$* \e[0m"
	error=0
}

status() {
	if [ $error -eq 0 ]
	then
		echo -e "\e[1A\e[222G\e[4D\e[32m :-)\e[0m"
	else
		echo -e "\e[1A\e[222G\e[4D\a\e[1;31m :-(\e[0m"
	fi
}

case "$1" in
    start)
	title "Mounting /dev/shm, /dev/pts and /proc/bus/usb."
	mkdir -p /dev/shm
	mount -n /dev/shm || error=$?
	mkdir -p /dev/pts
	mount -n /dev/pts 2> /dev/null
	mount -n none /proc/bus/usb -t usbfs 2> /dev/null

	if ! grep -q /mnt/live /proc/mounts; then # not livecd ?
		title "Remounting / read-write."
		mount -n / -o remount,rw
	fi

        title "Mounting local file systems."
        cat /proc/mounts > /etc/mtab || error=$?
        mount -a -t nocoda,nfs,devfs,proc,sysfs || error=$?

	title "Activating swap devices."
	swapon -a || error=$?

    if [ -f /etc/conf/hardware ] ; then
	. /etc/conf/hardware
    fi
    [ -x /bin/dmesg ] && /bin/dmesg -n 3
    	status

	title "Refresh utmp, delete lock and tmp files and other stuff."
	find /var/lock /var/run -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
    	status

	title "Setting hostname to $(cat /etc/HOSTNAME)."
	hostname "$(cat /etc/HOSTNAME)" || error=$?
    	status

	title "Activating udevsend."
	echo "/sbin/udevsend" > /proc/sys/kernel/hotplug
	title "Activating hotplug++."
	/sbin/hotplug++ -synth

	title "Configuring the kernel with /etc/conf/kernel."
	sh /etc/conf/kernel || error=$?
	status

    [ -f /etc/conf/clock ] && . /etc/conf/clock
    if [ "$clock_tz" = localtime ] ; then
		status

	title "Setting kernel clock to local time."
	    hwclock --hctosys --localtime || error=$?
    fi
    if [ "$clock_rtc" ] ; then
		status

	title "Setting enhanced real time clock precision to $clock_rtc."
	if [ -w /proc/sys/dev/rtc/max-user-freq ] ; then
	    echo $clock_rtc > /proc/sys/dev/rtc/max-user-freq || error=$?
	else
	    echo "No /proc/sys/dev/rtc/max-user-freq found."
	fi
    fi
    	status

	title "Writing /var/log/boot.msg."
	dmesg > /var/log/boot.msg || error=$?
    	status

	title "Setting console screen font."
	if [ -L /etc/default.vcfont ] ; then
		fontfile=$(ls -l /etc/default.vcfont | sed 's,.* -> ,,')
		setfont $fontfile || error=$?
	elif [ -f /etc/default.vcfont ] ; then
		setfont /etc/default.vcfont || error=$?
	else
		echo "No /etc/default.vcfont found."
	fi
    	status

	title "Setting console terminal type and blank interval."
	con_term=linux; con_blank=0
        [ -f /etc/conf/console ] && . /etc/conf/console
        /usr/bin/setterm -term $con_term -blank $con_blank > /dev/console || error=$?
    	status

	title "Setting up loopback networking."
	ip link set lo up || error=$?
	[[ "$(ip addr show lo)" = *127.0.0.1/8* ]] ||
		ip addr add 127.0.0.1/8 dev lo || error=$?
    	status

	title "Setting overflow UID and GID kernel parameters."
	sysctl -w kernel.overflowuid=$(id -u nobody) > /dev/null || error=$?
	sysctl -w kernel.overflowgid=$(id -g nobody) > /dev/null || error=$?
    	status

	title "Reading /etc/sysctl.conf file."
	sysctl -p || error=$?
    	status

	title "Initializing kernel random number generator."
	if [ -e /var/state/random-seed ] ; then
		cat /var/state/random-seed >/dev/urandom || error=$?
	fi
    	status
	;;


    stop)
	title "Saving /var/log/init.msg and /var/log/boot.msg."
	touch /var/log/init.msg /var/log/boot.msg || error=$?
	mv /var/log/init.msg /var/log/init.old || error=$?
	mv /var/log/boot.msg /var/log/boot.old || error=$?
    	status

	title "Writing a wtmp record."
	if [ "$RUNLEVEL" = 0 ] ; then halt -w || error=$?
	else reboot -w || error=$? ; fi
    	status

	title "Saving kernel random seed."
	dd if=/dev/urandom of=/var/state/random-seed count=1 2> /dev/null
    	status

	title "Sending all processes a SIGTERM (15)."
	killall5 -15 || error=$? ; sleep 5 
    	status

	title "Sending all processes a 2nd SIGTERM (15)."
	killall5 -15 || error=$? ; sleep 5 
    	status

	title "Sending all processes a SIGKILL (9)."
	killall5 -9 || error=$? ; sleep 5 
    	status

	title "Turning off swap devices."
	swapoff -a || error=$?
    	status

	title "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
    	status

	title "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
    	status

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

    restart)
	$0 stop; $0 start
	;;

    *)
	echo "Usage: $0 { start | stop | restart }"
	exit 1 ;;

esac

exit 0
