#!/bin/sh
#
# dRock Linux: /etc/init.d/nfsd
# Desc: low-level boot script
#
# (C) JAN 2001 by Valentin Ziegler <ziegler@informatik.hu-berlin.de>
#     FEB 2001 by Ren Rebe <rene.rebe@gmx.net>
#     JUN 2001 by Nikolaus Filus <NFilus@gmx.net>
#

. /etc/init.d/master.sh
. /etc/conf/bootdown

exec 2>&1
export PATH=/bin:/sbin:/usr/bin:/usr/sbin

c_writeln "*** Booting Desktop ROCK Linux ***"

[ -f /fastboot ] && FASTBOOT=1
[ $FASTBOOT = 1 ] && c_writeln "Fast boot mode enabled ..."
[ -f /forcefsck ] && FORCEFSCK=1

# mount virtual filesystems

# devfs must be mouted by the kernel!

c_writeln "Mounting /proc device ..."
sub_exec "mount -v -n /proc" 

# hardcoded filesystem! should be done from fstab
#c_writeln "Mounting shm device ..."
#sub_exec "mount -v -n /dev/shm"


# (start kerneld)
# .......

# (Multiple devices)
# .......

# Do conf Jobs

c_writeln "Loading kernel modules and configuring the kernel ..."
. /etc/conf/kernel
sub_exec true

c_writeln "Configuring the /dev filesystem ..."
. /etc/conf/devfs
sub_exec true

# activate swap.

c_writeln "Activating swap devices in /etc/fstab ..."
sub_exec "swapon -a" 

# update daemon - obsolete for lastest kernels? ...

# do fsck and start sulogin, if it fails.

do_fsck () 
{
    c_writeln "Checking File Systems ..."
    FSCK_OPTS="-A -a"
    [ $FORCEFSCK = 1 ] && c_writeln "Check forced !"
    [ $FORCEFSCK = 1 ] && FSCK_OPTS=${FSCK_OPTS}" -f" 
    fsck ${FSCK_OPTS}
    FSCK_RETURN=$?
    # A return code of 1 indicates that file system errors
    # were corrected, but that the boot may proceed.
    # A return code of 2 or larger indicates failure.
    test $FSCK_RETURN -gt 1 && return -1
    test $FSCK_RETURN -gt 0 && c_writeln "Some errors were fixed, seems ok to go on"
    return 0
}

fsck_panic ()
{
    if test -x /etc/init.d/kbd ; then
      /etc/init.d/kbd start
    fi
    echo
    echo "fsck failed.  Please repair manually and reboot. The root"
    echo "file system is currently mounted read-only. To remount it"
    echo "read-write do:"
    echo
    echo "   bash# mount -n -o remount,rw /"
    echo
    echo "Attention: Only CONTROL-D will reboot the system in this"
    echo "maintanance mode. shutdown or reboot will not work."
    echo
    PS1="(repair filesystem) > "
    export PS1
    
    # /sbin/sulogin /dev/console
    echo -ne '\004'
    /sbin/sulogin -p /dev/vc/1

    # if the user has mounted something rw, this should be umounted
    c_writeln "Brute-force unmounting file systems (ignore error messages ;-))"
    umount -avn
    mount -no remount,ro / 
    sync
    reboot -f
}

if [ $FASTBOOT != 1 ] ; then

    c_writeln "Remounting root filesystem read-only ..."
    sub_exec "mount -n -o remount,ro /"

    if test $? = 0; then
	case $(awk '/ \/ / {print $3}' < /proc/mounts) 
	in
	    ext2) sub_exec do_fsck || fsck_panic ;;
	    ext3) c_writeln "Found Ext3 no check needed (journaled)." ;;
	    reiserfs) c_writeln "Found ReiserFS no check needed (journaled)." ;;
	    *) c_warn "UNKNOWN fstype! can't run fsck!" ;;
	esac
	sync
	c_writeln "Remounting root filesystem rw ..."
	sub_exec "mount -n -o remount,rw /"
    else
	mounts=/etc/mtab
	test -r /proc/mounts && mounts=/proc/mounts
	while read des fs type rest; do
	    case "$fs" in
		/) break ;;
		*)       ;;
	    esac
	done < $mounts
	if test "$type" != "umsdos" ; then
	    echo
	    echo "*** ERROR!  Cannot fsck because root is not read-only!"
	    echo
	fi
    fi

fi

# initialize modules database
# ..........


# Mount local filesystems 
# we use the normal mtab way again. especially chroot was broken

rm /etc/mtab
cat /proc/mounts > /etc/mtab

c_writeln "Mounting local filesystems ..."
sub_exec "mount -av -t nonfs,noproc,nodevfs,noshm"

# Setup ld.so.cache
# ........


# start SVGATextmode 
# NOTE! SVGATextMode is _not_ delivered with dRock

#c_writeln "Switching Textmode to 100x37 ..."
#sub_exec "/usr/sbin/SVGATextMode -r 100x37x9_SVGA" 


# set and adjust the CMOS clock

sa_hwclock ()
{
    c_write "Setting up CMOS clock ..."
    ARC=false
    UTC=false
    if [ -f /etc/conf/clock ]; then
        . /etc/conf/clock
    fi

    if [ $UTC = "true" ]; then
        CLOCKFLAGS="$CLOCKFLAGS -u"
        c_write " (utc): "
    fi
    if [ $ARC = "true" ]; then
        CLOCKFLAGS="$CLOCKFLAGS -A"
        c_write " (arc): "
    fi
    
    hwclock -s $CLOCKFLAGS && hwclock -a $CLOCKFLAGS
    date
    return $?
}

sub_exec sa_hwclock

# initialize loopback device

c_writeln "Initializing loopback device ..."
sub_exec "ifconfig lo 127.0.0.1 netmask 255.0.0.0 up"
# route add -net 127.0.0.0 netmask 255.0.0.0 dev lo 


# set hostname (and domainname)

if type hostname > /dev/null 2>&1 ; then
    c_writeln "Set the hostname to [`cat /etc/HOSTNAME`] ..."
    sub_exec "hostname `cat /etc/HOSTNAME`"
else
    c_warn "Hostname is not set: no 'hostname' command found!"
fi

if type domainname > /dev/null 2>&1 ; then
    c_writeln "Set the domainname to [`cat /etc/DOMAINNAME`] ..."
    sub_exec "domainname `cat /etc/DOMAINNAME`"
else
    c_warn "Domainname is not set: no 'domainname' command found!"
fi


# clean up
c_writeln "Refresh the utmp and delete lock files ..."
rm -f /var/lock/* /var/lock/*/* \
      /var/run/*  /var/run/*/*  /var/spool/uucp/LCK* 2>/dev/null

rm -f /var/run/utmp
echo -n > /var/run/utmp
chmod 664 /var/run/utmp
chown root.tty /var/run/utmp

rm -f /etc/nologin /nologin /fastboot /forcefsck /fastdown

if [ "$TEMPCLEAN" = 1 ] ; then
    c_writeln "Clean up /tmp ..."
    for x in /tmp/* /tmp/.[!.]* ; do
	if [ -O "$x" ] ; then
	    [ "$x" != "/tmp/lost+found" ] &&
	    [ "$x" != "/tmp/quota.user" ] &&
	    [ "$x" != "/tmp/quota.group" ] &&
	    rm -rf -- "$x"
	else
	    rm -rf -- "$x"
	fi
    done
fi

# setup PNP 
# .........

# Read all kernel messages generated until now and put them in one file.

if type klogd > /dev/null 2>&1
then
    c_writeln "Creating /var/log/boot.msg ..."
    klogd -f /var/log/boot.new -o
    grep '.' < /var/log/boot.new > /var/log/boot.msg
    rm /var/log/boot.new
    sub_exec true
else
    c_warn "Kernel boot messages are not logged: no 'klogd' command found!"
fi

write_proc ()
{
    echo $1 > /proc/$2
    return $?
}


# (enable DEXE binary format)
# ........

# Enable "dynamic IP patch"
# (only needed for older Kernel (<2.3x??) )
#
# c_writeln "Set Dynamic IP patch: mode 7 ..."
# sub_exec "write_proc 7 sys/net/ipv4/ip_dynaddr"

# Enable syn flood protection

if [ -f /proc/sys/net/ipv4/tcp_syncookies ]; then
    c_writeln "Enabeling syn flood protection ..."
    sub_exec "write_proc 1 sys/net/ipv4/tcp_syncookies" 
else
    c_warn "the kernel lacks syn flood protection support"
fi

if type loadkeys > /dev/null 2>&1
then
    c_writeln "Set the keyboard keymappings ..."
    if [ -f /etc/default.keymap.map.gz ] ; then
        loadkeys /etc/default.keymap.map.gz
    else
        c_warn "No /etc/default.keymap.map.gz found."
    fi
else
    c_warn "Keymappings not configured: no 'loadkeys' command found."
fi

c_writeln "Initializing kernel random number generator ..."
if [ -f /var/state/random-seed ] ; then 
    cat /var/state/random-seed > /dev/urandom
else
    c_warn "No /var/state/random-seed found!"
fi
dd if=/dev/urandom of=/var/state/random-seed count=1 2> /dev/null

# write EOT mark for btee

echo -ne '\004'

exit 0
