#!/bin/sh
# bloody hack to speed up and clean up terminals' boot
# should be fixed properly by passing "fastboot" in kernel args
# and then enhanced by adding "diskless" support in startup

. install2-init-functions

[ -z "$AUTOINSTALL" ] || exit 0

ltspdir=$destdir/var/lib/ltsp/i586
mv $ltspdir/etc/rc.d/rc.sysinit{,-default}
cat > $ltspdir/etc/rc.d/rc.sysinit-ltsp << "__EOF__"
#!/bin/sh
#
# /etc/rc.d/rc.sysinit - run once at boot time
#
# Taken in part from Miquel van Smoorenburg's bcheckrc.
# Taken to parts for diskless LTSP clients.
#

trap '' SIGPIPE

WITHOUT_RC_COMPAT=1

# Source function library
. /etc/init.d/functions

# Fix console loglevel
dmesg -n "$LOGLEVEL"

HOSTNAME=`hostname`

if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
	hostname localhost
fi

# Set system font
ExecIfExecutable /sbin/setsysfont >/dev/null 2>&1 && SETSYSFONT_DONE=1 || SETSYSFONT_DONE=

PrintMargin()
{
	if [ "$BOOTUP" != serial ]; then
		local n
		n=`echo -n "$*" |wc -c` 2>/dev/null &&
			tput hpa $(( (COLUMNS - n)/2 ))
	fi
}

# Print a banner. ;)
WELCOME="Welcome to "
BANNER="ALT Linux"
PrintMargin "$WELCOME$BANNER"
echo -n "$WELCOME"
SETCOLOR_INFO
echo -n "$BANNER"
SETCOLOR_NORMAL
echo

# Mount /proc (done here so volume labels can work with fsck)
action "Mounting proc filesystem:" mount -n -t proc proc /proc
chgrp proc /proc >/dev/null 2>&1 ||:

# Mount /sys where appropriate
if grep -wqs sysfs /proc/filesystems; then
	action "Mounting sys filesystem:" mount -n -t sysfs sysfs /sys
fi

# Load modules
/etc/rc.d/scripts/load_modules

RUN_UDEV=
udevd_exe=/etc/init.d/udevd
[ ! -x "$udevd_exe" ] ||
	grep -iwqs noudev /proc/cmdline ||
	RUN_UDEV=1

# Start udev
if [ -n "$RUN_UDEV" ]; then
	RUN_FROM_SYSINIT=1 "$udevd_exe" start
	[ -c /dev/rtc ] || modprobe rtc >/dev/null 2>&1
fi

# Remount the root filesystem read-write
action "Remounting root filesystem in read/write mode:" ${REMOUNT_ROOTFS_RW_COMMAND:-mount -n -o remount,rw /}

# The root filesystem is now read-write, so we can now log via syslog() directly
[ -z "$IN_INITLOG" ] || IN_INITLOG=

# Configure kernel parameters
action "Configuring kernel parameters:" sysctl -e -p /etc/sysctl.conf

if [ -z "$RUN_UDEV" ]; then
	sysctl -w kernel.modprobe="/sbin/modprobe" >/dev/null 2>&1
	sysctl -w kernel.hotplug="/sbin/hotplug" >/dev/null 2>&1
fi

OLD_COLUMNS="$COLUMNS"

# Load framebuffer device settings
/etc/rc.d/scripts/framebuffer_init

# Set system font again, if required
if [ "$OLD_COLUMNS" != "$COLUMNS" ]; then
	ExecIfExecutable /sbin/setsysfont >/dev/null 2>&1 && SETSYSFONT_DONE=1 || SETSYSFONT_DONE=
fi

# Mount all other filesystems (except for NFS and /proc, which is already
# mounted). Contrary to standard usage,
# filesystems are NOT unmounted in single user mode
# (pixel) also do not mount loopback, will be done later
action 'Mounting local filesystems:' mount -a -t nocifs,davfs,ftpfs,loop,ncp,ncpfs,nfs,panfs,proc,smbfs,sshfs -O no_netdev

# Set system font again, if required
[ -n "$SETSYSFONT_DONE" ] || ExecIfExecutable /sbin/setsysfont >/dev/null 2>&1

# Do the following while waiting for an 'I' from the user...
{

# Now turn on swap again (in case we swap to files)
action "Activating swap space:" swapon -a

# Initialize the serial ports
ExecIfExecutable /etc/rc.d/rc.serial

# Now that we have all of our basic modules loaded and the kernel going,
# let's dump the syslog ring somewhere so we can find it later
dmesg >/var/log/dmesg
kill -TERM `pidof getkey` >/dev/null 2>&1

} &

if [ -n "$PROMPT" ]; then
	getkey i || PROMPT=
fi
wait

if [ -n "$PROMPT" ]; then
	touch /var/run/confirm
fi
__EOF__
chmod +x $ltspdir/etc/rc.d/rc.sysinit-ltsp
ln -s rc.sysinit-ltsp $ltspdir/etc/rc.d/rc.sysinit
:
