#!/bin/sh
# /etc/init.d/rcS - Initial boot script for SliTaz GNU/Linux. rcS is the main
# initialisation 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

# Start by sleeping a bit.
echo "Processing /etc/init.d/rcS... "
sleep 1

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

# 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
		sleep 2
	done
fi

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

# Mount all stuff from /etc/fstab.
echo "Mounting all staff from 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 "Executing : udevstart..."
	/sbin/udevstart
	status
	echo "/sbin/udevd" > /proc/sys/kernel/hotplug
fi

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

else #logged

# Creat /dev/cdrom if needed (symlink does not exist on LiveCD).
# Add also /dev/cdrom to fstab if entry dos not exist.
#
DRIVE_NAME=`cat /proc/sys/dev/cdrom/info | grep "drive name" | cut -f 3`
if [ -n "$DRIVE_NAME" -a ! "`readlink /dev/cdrom`" ]; then
	echo -n "Creating symlink : /dev/cdrom..."
	ln -s /dev/$DRIVE_NAME /dev/cdrom
	status
fi
if ! grep -q "/dev/cdrom" /etc/fstab; then
	echo -n "Adding /dev/cdrom  to fstab..."
	echo '/dev/cdrom      /media/cdrom iso9660 user,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/$DRIVE_NAME
	status
fi

# Handle kernel cmdline parameter modprobe=<module_list> 
if grep -q " modprobe=" /proc/cmdline; 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 grep -q " config=" /proc/cmdline; then
	CONFIG=`cat /proc/cmdline | sed 's/.* config=\([^ ]*\).*/\1/'`
	DEVICE=${CONFIG%,*}
	SCRIPT=${CONFIG#*,}
	echo -n "Source $SCRIPT from $DEVICE... "
	if /bin/mount -r $DEVICE /mnt; then
		. /mnt/$SCRIPT
		/bin/umount /mnt
	fi
	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/*
	rm -f /var/run/*.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

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

# Re-source main config file, in Live mode daemons list ca 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

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

fi #logged
