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

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

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

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

# Before mounting filesystems we check fs specified in the file
# /etc/rcS.conf and variable $CHECK_FS.
if [ ! "$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

# 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

/bin/dmesg > /var/log/dmesg.log
vcsa2txt < /dev/vcsa1 | awk 'BEGIN {s=0} /^Processing|^.witching/ {s=1} { if (s) print }' >/var/log/boot.log
script -a -q -c '/etc/init.d/rcS logged' /var/log/boot.log

else #logged

# Create /dev/cdrom if needed (symlink does not exist on LiveCD).
# Also add /dev/cdrom to fstab if entry does not exist.
#
DRIVE_NAME=`cat /proc/sys/dev/cdrom/info | grep "drive name" | cut -f 3`
if grep -q " cdrom=" /proc/cmdline; then
	DRIVE_NAME=`cat /proc/cmdline | sed 's/.* cdrom=\([^ ]*\).*/\1/'`
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> 
MODULES=$(sed '/modprobe=/!d;s/.* modprobe=\([^ ]*\).*/\1/;s/,/ /g' /proc/cmdline)
for i in $MODULES; do
	echo -n "Loading kernel module $i"
	/sbin/modprobe $i
	status 
done

if [ ! -s /var/lib/detected-modules ]; then
	mount -t usbfs usbfs /proc/bus/usb
	/sbin/tazhw init
fi


# Try to release http link (from slitaz-loram-http package)
 release_http()
 {
	FREEMEM=$(free | awk '{ n = $NF } END { print n }')	
	if [ $FREEMEM -gt $(du -s $FS | awk '{ print $1+40000 }') ]; then
		status
		echo -n "Loading /usr in memory..."
		cp -a $FS /.usr.fs
		status
		echo -n "Releasing HTTP link..."
		umount /cdrom && umount /cdrom
		FS=/.usr.fs
		return 0
	fi
	return 1
 }

# Mount /usr (from slitaz-loram-http package)
 if [ -d /cdrom ]; then
	/etc/init.d/network.sh
	RUN_SCRIPTS="$(echo $RUN_SCRIPTS | sed 's/network.sh//')"
	ISO=iso/$(cat /etc/slitaz-release)/flavors/slitaz-loram-cdrom.iso
	for URLISO in $(sed '/urliso=/!d;s/.* urliso=\([^ ]*\).*/\1/' /proc/cmdline) \
		http://mirror.switch.ch/ftp/mirror/slitaz/$ISO \
		http://download.tuxfamily.org/slitaz/$ISO \
		http://mirror.slitaz.org/$ISO \
		http://mirror1.slitaz.org/$ISO \
		http://mirror2.slitaz.org/$ISO \
		http://mirror3.slitaz.org/$ISO ; do
			/bin/httpfs $URLISO /cdrom && rm -rf /usr/* && break
	done
	mount -o loop,ro /cdrom/*.iso /cdrom
	if [ -d /.usr.rw -a -x /bin/funionfs ]; then
		echo -n "Mounting /usr read-write... "
		usr=.usr.ro
	else
		echo -n "Mounting /usr read-only... "
		usr=usr
	fi
	FS=/cdrom/usr.cromfs
	if [ -f $FS ]; then
		release_http
		mkdir /$usr
		/bin/cromfs-driver $FS /$usr -o ro,dev,suid,allow_other
	elif [ -f /cdrom/usr.sqfs ]; then
		FS=/cdrom/usr.sqfs
		release_http
		mkdir /$usr
		mount -o loop,ro -t squashfs $FS /$usr
	else
		FS=/cdrom/usr
		ln -s $FS /$usr
	fi
	status 
	if [ -d /.usr.rw -a -x /bin/funionfs ]; then
		/bin/funionfs -o dirs=/.usr.ro=RO:/.usr.rw -o allow_other,nonempty -o suid,dev NONE /usr
	fi
	if [ -d /usr/.moved/lib/modules ]; then
		rm -rf /lib/modules
		ln -s /usr/.moved/lib/modules /lib/modules
	fi
 fi

# Handle kernel cmdline parameter config=<device>,<path> to source a 
# disk init script
if grep -q " config=" /proc/cmdline; then
	CONFIG=`cat /proc/cmdline | sed 's/.* config=\([^ ]*\).*/\1/'`
	DEVICE=${CONFIG%,*}
	SCRIPT=${CONFIG#*,}
	echo "Probing $DEVICE... "
	if ! /bin/mount -r $DEVICE /mnt; then
		if echo $DEVICE | grep -q "/dev/sd"; 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
	fi
	status 
fi

# Eject option
if grep -q -w "eject" /proc/cmdline; then
	eject /dev/cdrom
fi

# Mount /proc/bus/usb.
if [ -d /proc/bus/usb ] && ! grep -q /proc/bus/usb /proc/mounts; 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

# Clean up the system.
if [ "$CLEAN_UP_SYSTEM" = "yes" ]; then
	echo -n "Cleaning up the system... "
	rm -rf /tmp/* /tmp/.* 2> /dev/null
	rm -f /var/run/*.pid /var/run/dbus/* /var/run/hald/pid
	rm -f /var/lock/*
	status
else
	echo "System clean up is disabled in /etc/rcS.conf... "
	echo "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
/bin/mkdir -p /tmp/.ICE-unix
/bin/chmod 1777 /tmp/.X11-unix
/bin/chmod 1777 /tmp/.ICE-unix
status

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

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

# 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.
. /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

fi #logged
