#!/bin/sh
# /etc/init.d/rcS - Initial boot script for SliTaz GNU/Linux. 
# Config file is : /etc/rcS.conf
#
# rcS is the main initialization script used to check fs, mount, clean,
# run scripts and start daemons.
#
. /etc/init.d/rc.functions
. /etc/rcS.conf

# Boot time.
bootdate=`date +%s`

if [ "$1" != "logged" ]; then # logged

echo "Processing /etc/init.d/rcS..."

# Mount /proc.
echo -n "Mounting proc filesystem..."
/bin/mount proc
status

# Parse cmdline args for earlier boot options. All other boot options
# are in /etc/init./bootopts.sh.
echo -n "Searching for early boot options..."
for opt in `cat /proc/cmdline`
do
	case $opt in
		fastbootx|fbx)
			export FAST_BOOT_X="yes" ;;
		cdrom=*)
			export CDROM=${opt#cdrom=} ;;
		modprobe=*)
			export MODPROBE="yes" ;;
		config=*)
			export CONFIG=${opt#config=} ;;
		*)
			continue ;;
	esac
done
status

# Start Udev to populate /dev and handle hotplug events
if [ "$UDEV" = "yes" ]; then
	echo -n "Starting udev daemon..."
	/sbin/udevd --daemon
	status
	echo -n "Udevadm requesting events from the Kernel..."
	udevadm trigger
	status
	echo -n "Udevadm waiting for the event queue to finish..."
	udevadm settle
	status
	echo "/sbin/udevd" > /proc/sys/kernel/hotplug
fi

# Before mounting filesystems we check fs specified in the file
# /etc/rcS.conf and variable $CHECK_FS.
if [ -n "$CHECK_FS" ]; then
	mount -o remount,ro /
	for i in $CHECK_FS
	do
		echo "Checking filesystem on : $i"
		/sbin/e2fsck -p $i
	done
fi

# Remount rootfs rw.
echo "Remounting rootfs read/write..."
/bin/mount -o remount,rw /

# Mount filesystems in /etc/fstab.
echo "Mounting filesystems in fstab..."
/bin/mount -a

# Store boot messages to log files.
/bin/dmesg > /var/log/dmesg.log &
conspy -d | sed 's/ *$//;/^$/d;/^Processi\|^.witchi/,$!d' > /var/log/boot.log
script -a -q -c '/etc/init.d/rcS logged' /var/log/boot.log

else # logged

# Clean up the system.
if [ "$CLEAN_UP_SYSTEM" = "yes" ]; then
	echo -n "Cleaning up the system..."
	find /var/run -name "*.pid" -type f | xargs /bin/rm -f
	/bin/rm -rf /tmp /var/run/dbus/* /var/run/hald/pid /var/lock/*
	/bin/mkdir -p /tmp && /bin/chmod 1777 /tmp
	status
else
	echo "System clean up is disabled in /etc/rcS.conf..."
	echo -n "Keeping all tmp and pid files..."
	status
fi

# Set up tmp X11 and ICE dir.
echo -n "Setting up tmp X11 and ICE unix dir..."
/bin/mkdir -p /tmp/.X11-unix /tmp/.ICE-unix
/bin/chmod 1777 /tmp/.X11-unix /tmp/.ICE-unix
status

# Fast boot into X for HD install or custom Live system. We need 
# keymap settings since Xvesa dumps the console mapping and a correct
# slim configuration for screen resolution. DBUS and HAL must also start
# before X session (manual login or autologin) to have devices in PCmanFM.
if [ "$FAST_BOOT_X" = "yes" ]; then
	/etc/init.d/i18n.sh
	/etc/init.d/dbus start
	/etc/init.d/hald start
	/etc/init.d/slim start &
fi

# Create /dev/cdrom if needed (symlink does not exist on LiveCD).
# Also add /dev/cdrom to fstab if entry does not exist.
if [ -n "$CDROM" ]; then
	DRIVE_NAME=${CDROM#/dev/}
else
	DRIVE_NAME=`grep -s "drive name" /proc/sys/dev/cdrom/info | cut -f 3`
fi
if [ -n "$DRIVE_NAME" -a ! "`readlink /dev/cdrom`" ]; then
	echo -n "Creating symlink : /dev/cdrom..."
	ln -s /dev/$DRIVE_NAME /dev/cdrom
	ln -s /dev/$DRIVE_NAME /dev/dvd
	status
fi
if ! grep -q "/dev/cdrom" /etc/fstab; then
	echo -n "Adding /dev/cdrom  to fstab..."
	echo '/dev/cdrom      /media/cdrom iso9660 user,ro,noauto       0       0' \
		>> /etc/fstab
	status
fi
# Chmod hack on each boot for Asunder and burnbox. Allowing all users
# to burn/rip CD/DVD.
if [ -n "$DRIVE_NAME" -a "`readlink /dev/cdrom`" ]; then
	echo -n "Chmoding cdrom device..."
	chmod 0666 /dev/cdrom
	chmod 0666 /dev/dvd
	chmod 0666 /dev/$DRIVE_NAME
	status
fi

# Handle kernel cmdline parameter modprobe=<module_list> 
if [ -n "$MODPROBE" ]; then
	MODULES=`sed -e 's/.* modprobe=\([^ ]*\).*/\1/' -e 's/,/\n/g' < /proc/cmdline`
	for i in $MODULES; do
		echo -n "Loading kernel module $i"
		/sbin/modprobe $i
		status 
	done
fi

# Handle kernel cmdline parameter config=<device>,<path> to source a 
# disk init script
if [ -n "$CONFIG" ]; then
	DEVICE=${CONFIG%,*}
	SCRIPT=${CONFIG#*,}
	echo "Probing $DEVICE... "
	if ! /bin/mount -r $DEVICE /mnt; then
		if echo $DEVICE | grep -Eq "/dev/sd|UUID=|LABEL="; then
			USBDELAY=`cat /sys/module/usb_storage/parameters/delay_use`
			USBDELAY=$((1+$USBDELAY))
			echo "$DEVICE is potentially a USB device: sleep for $USBDELAY seconds"
			sleep $USBDELAY
		fi
		if ! /bin/mount -r $DEVICE /mnt; then
			CONFIG=""
		fi
	fi
	echo -n "Source $SCRIPT from $DEVICE..."
	if [ -n "$CONFIG" ]; then
		. /mnt/$SCRIPT
		/bin/umount /mnt 2> /dev/null || true
	fi
	status 
fi

# Mount /proc/bus/usb.
if [ -d /proc/bus/usb ]; then
	echo -n "Mounting /proc/bus/usb filesystem..."
	/bin/mount -t usbfs usbfs /proc/bus/usb
	status
fi

# Start syslogd and klogd.
if [ "$KERNEL_LOG_DAEMONS" = "yes" ]; then
	echo -n "Starting system log deamon: syslogd..."
	/sbin/syslogd -s $SYSLOGD_ROTATED_SIZE && status
	echo -n "Starting kernel log daemon: klogd..."
	/sbin/klogd && status
else
	echo "Kernel log daemons are disabled in /etc/rc.conf..."
fi

# Load all modules listed in config file.
if [ -n "$LOAD_MODULES" ]; then
	for mod in $LOAD_MODULES
	do
		modprobe $mod
	done
fi

# Be quiet on configuration to avoid messages overwriting ncurses dialog
echo "0 0 0 0" > /proc/sys/kernel/printk

# Detect PCI and USB devices with Tazhw from slitaz-tools. We load
# kernel modules only at first boot or in LiveCD mode.
if [ ! -s /var/lib/detected-modules ]; then
	/sbin/tazhw init
fi

# Call udevadm trigger to ensure /dev is fully populate now that all 
# modules are loaded.
if [ "$UDEV" = "yes" ]; then
	echo -n "Requesting events from the Kernel..."
	udevadm trigger
	status
fi

# Start all scripts specified with $RUN_SCRIPTS.
echo "Executing all initialization scripts..."
for script in $RUN_SCRIPTS
do
	if [ -x /etc/init.d/$script ]; then
		/etc/init.d/$script
	fi
done

# Re-source main config file. In Live mode, daemons list can be modified 
# by boot options (screen=text will remove slim).
. /etc/rcS.conf

# Start all daemons specified with $RUN_DAEMONS.
echo "Starting all daemons specified in /etc/rcS.conf..."
for daemon in $RUN_DAEMONS
do
	if [ -x /etc/init.d/$daemon ]; then
		/etc/init.d/$daemon start
	fi
done

# Back to a verbose mode.
echo "7 4 1 7" > /proc/sys/kernel/printk

# Reset screen and display a bold message.
if [ -n "$MESSAGE" ]; then
	/usr/bin/reset
	echo -e "\033[1m$MESSAGE\033[0m"
fi

# Display and log boot time.
time=$((`date +%s` - $bootdate))
echo $time > /var/log/boot-time
echo "SliTaz boot time: ${time}s"

fi # logged
