#!/bin/ash

# $Id: init 4622 2015-10-06 08:56:28Z joergs $

#set -x

# include profile
. /etc/profile

# set kernel log messages to console
dmesg -n $Log_Level_Kernel_Console

#
# display logo and version
#

# clears the screen
clear
screen_blank_off

printline
display_logo
printline

log $LOG_INFO "Updating your system ..."

log $LOG_DEBUG "mounting pseudo-filesystems"
mount -n -t proc proc /proc
mount -n -t sysfs sysfs /sys
mount -n -t devpts -o mode=0620 devpts /dev/pts

if [ "$KeyboardMap" ] && [ -r "$KeyboardMap" ]; then
    log -n $LOG_INFO "Loading keyboard map $KeyboardMap "
    loadkmap < "$KeyboardMap"
    rc_status -v
fi

openvt -c 2 ash --login
openvt -c 5 top
openvt -c 6 watch -n 5 df -h
openvt -c 7 tail -n 100 -f /var/log/messages
# $VARS_FILE is recreated periodicaly
# and therefore not displayed correctly
#openvt 8 tail -n 100 -f $VARS_FILE

activate_udev
activate_usb

# get the debug status from kernel cmdline
if [ "$Debug" = "off" ]; then
    kernel_cmdline=`cat /proc/cmdline`
    cmdline_Debug=`expr match "$kernel_cmdline" ".* netboot_debug=\([^ ]\+\)"`
    test "$cmdline_Debug" = "on" && Debug=on
fi

printline

action_on_timeout "$NETBOOT_GLOBAL_TIMEOUT" "reboot"
printline



log $LOG_INFO "Loading kernel modules ..."

# initialize hardware detection
sc_hardware_init


# load kernel modules
for i in $Modules_misc; do
    echo -n "loading module $i"
    modprobe $i
    rc_status -v
done

printline

log $LOG_INFO "Initializing network ..."

# auto detection of network modules
get_netcard

log -n $LOG_INFO  "Starting dhcp client on $NetDev"
# enable network interface
# also use output as entropy for random number generation
ifconfig $NetDev 0.0.0.0 up > /dev/urandom 2>&1
sleep 1
my_dhcp start
rc_status -v

# check the lease time, to see if it is a permanent one.
# if the lease time is smaller than Dynlease_Time,
# a new IP is requested from DHCP 
log -n $LOG_INFO "Waiting for permanent IP address ... "
my_lease $Ldap2dhcp_Dynlease_Time
rc_status -v

# show network information
ifconfig $NetDev | head -n 2

printline
debug_func ash

log $LOG_INFO "Looking up workstation LDAP name"
available_test $Ldap_Server reboot
my_dn="$(get_my_dn)"
if [ -z "$my_dn" ]; then
	# MAC address not found in LDAP. Add?
	log $LOG_CRIT "Workstation not found. Update can not continue"
	log $LOG_INFO "Inform your System Administrator"
	action_on_error reboot
else
	log $LOG_INFO "Workstation LDAP name: $my_dn"
fi
printline

# dbkRefImage vom LDAP-Server holen
log $LOG_INFO "Looking up Image LDAP name"
my_ref_image=$(get_my_attrib "$my_dn" dbkRefImageDn)
log $LOG_INFO "Image LDAP name: $my_ref_image"
printline

available_test $Rsync_Server reboot

# creates e.g. $Tmp_Path/initrd_script 
get_initrd_script

[ -r $Tmp_Path/initrd_script ] || action_on_error reboot "failed to load control script from server $Rsync_Server"

source $Tmp_Path/initrd_script

printline

log $LOG_INFO "next action: $action_when_finished"
DELAY=5
case $action_when_finished in
	bootHd)
		# look time not needed nor tested
		# chances are, that it does not work
		if [ -z ROOTDEVICE ]; then 
			ROOTDEVICE="0x302"
		fi
		
		# stop the dhcp client
		my_dhcp stop

		log $LOG_INFO "mounting root device $ROOTDEVICE"
		echo "$ROOTDEVICE" > /proc/sys/kernel/real-root-dev
		umount /proc
		;;
	reboot)
		# reboot erst, wenn Option LOCALBOOT ueber DHCP uebermittelt wurde
		log $LOG_INFO "waiting for LOCALBOOT settings from DHCP ..."
		my_lease $Ldap2dhcp_Netboot_Lease_Time
        log $LOG_INFO "LOCALBOOT is established. Rebooting in ${DELAY}s ..."
        my_sleep $DELAY
		my_reboot
		;;
	halt)
        log $LOG_INFO "system will be turned off in ${DELAY}s ..."
        my_sleep $DELAY
		my_halt
		;;
    kexec)
        log $LOG_INFO "trying to execute new kernel in ${DELAY}s ..."
        umount -a
        mount
        # bug: there are versions kexec/kernel
        # that are broken.
        # In this case, the system reboots.
        # To prevent starting the update immediabtly again,
        # we wait for some seconds. 
        # Hopefully, the network configuration 
        # has changed during this time,
        # otherwise the system do a second netboot
        my_sleep $DELAY
        kexec_execute
        # if this fails, perform a reboot
        # calling my_lease isn't useful,
        # because network might be down
        log $LOG_ERR "kexec failed. Falling back to reboot ..."
        my_sleep $DELAY
        my_reboot
        ;;
	*)
		# debug, if everything else has failed, start a shell
		ash
		;;
esac

exit 0
