#!/bin/sh
# /etc/init.d/bootopts.sh - SliTaz boot options from the cmdline.
#
# Earlier boot options are in rcS, ex: config= and modprobe=
#
. /etc/init.d/rc.functions

# Update fstab for swapon/swapoff 
add_swap_in_fstab()
{
	grep -q "$1	" /etc/fstab || cat >> /etc/fstab <<EOT
$1	swap	swap	default	0 0
EOT
}

# Check if swap file must be generated in /home: swap=size (Mb).
# This option is used with home=device.
gen_home_swap()
{
	if grep -q "swap=[1-9]*" /proc/cmdline; then
		SWAP_SIZE=`cat /proc/cmdline | sed 's/.*swap=\([^ ]*\).*/\1/'`
		# DD to gen a virtual disk.
		echo "Generating swap file: /home/swap ($SWAP_SIZE)..."
		dd if=/dev/zero of=/home/swap bs=1M count=$SWAP_SIZE
		# Make the Linux swap filesystem.
		mkswap /home/swap
		add_swap_in_fstab /home/swap
	fi
}

# Mount /home and check for user hacker home dir.
mount_home()
{
	echo "Home has been specified to $DEVICE..."
	USBDELAY=`cat /sys/module/usb_storage/parameters/delay_use`
	USBDELAY=$((1+$USBDELAY))
	echo "Sleeping $USBDELAY s to let the kernel detect the device... "
	sleep $USBDELAY
	USER=`cat /etc/passwd | grep 1000 | cut -d ":" -f 1`
	DEVID=$DEVICE
	if [ -x /sbin/blkid ]; then
		# Can be label, uuid or devname. DEVID give us first: /dev/name.
		DEVID=`/sbin/blkid | grep $DEVICE | cut -d: -f1`
		DEVID=${DEVID##*/}
	fi
	if [ -n "$DEVID" ] && grep -q "$DEVID" /proc/partitions ; then
		echo "Mounting /home on /dev/$DEVID... "
		mv /home/$USER /tmp/$USER-files
		mount /dev/$DEVID /home -o uid=1000,gid=1000 2>/dev/null \
			|| mount /dev/$DEVID /home
		gen_home_swap
	else
		echo "Unable to find $DEVICE... "
	fi
	# Move all user dir if needed.
	if [ ! -d "/home/$USER" ] ; then
		mv /tmp/$USER-files /home/$USER
		chown -R $USER.$USER /home/$USER
	else
		rm -rf /tmp/$USER-files
	fi
}

# Default user account without password (uid=1000). In live mode the option
# user=name can be used, but user must be add before home= to have home dir.
# This option is not handled by a loop and case like other and without
# effect on a installed system.
if ! grep -q "1000:1000" /etc/passwd; then
	if grep -q "user=" /proc/cmdline; then
		USER=`cat /proc/cmdline | sed 's/.*user=\([^ ]*\).*/\1/'`
		# Avoid usage of an existing system user or root.
		if grep -q ^$USER /etc/passwd; then
			USER=tux
		fi
	else
		USER=tux
	fi
	echo -n "Configuring user and group: $USER..."
	echo "$USER:x:1000:1000:SliTaz User,,,:/home/$USER:/bin/sh" >> /etc/passwd
	echo "$USER::14035:0:99999:7:::" >> /etc/shadow
	echo "$USER:x:1000:" >> /etc/group
	echo "$USER:!::" >> /etc/gshadow
	status
	# Audio group.
	sed -i s/"audio:x:20:"/"audio:x:20:$USER"/ /etc/group
	# /home/$USER files from /etc/skel.
	if [ -d /etc/skel ]; then
		cp -a /etc/skel /home/$USER
		# Path for user desktop files.
		for i in /home/$USER/.local/share/applications/*.desktop
		do
			sed -i s/"user_name"/"$USER"/g $i
		done
	else
		mkdir -p /home/$USER
	fi
	# set permissions.
	chown -R $USER.$USER /home/$USER
	# Slim default user.
	if [ -f /etc/slim.conf ]; then
		sed -i s/"default_user .*"/"default_user        $USER"/\
			/etc/slim.conf
	fi
fi

# Parse /proc/cmdline for boot options.
echo "Parsing kernel cmdline for SliTaz live options... "

for opt in `cat /proc/cmdline`
do
	case $opt in
		eject)
			# Eject cdrom.
			eject /dev/cdrom ;;
		autologin)
			# Autologin option to skip first graphic login prompt.
			echo "auto_login        yes" >> /etc/slim.conf ;;
		home=usb)
			# home=usb is a shoter and easy way to have home=/dev/sda1.
			DEVICE=sda1
			mount_home ;;
		home=*)
			# Check for a specified home directory (home=*).
			DEVICE=${opt#home=}
			mount_home ;;
		lang=*)
			# Check for a specified locale (lang=*).
			LANG=${opt#lang=}
			echo -n "Setting system locale to: $LANG... "
			echo "LANG=$LANG" > /etc/locale.conf
			echo "LC_ALL=$LANG" >> /etc/locale.conf
			status ;;
		kmap=*)
			# Check for a specified keymap (kmap=*).
			KEYMAP=${opt#kmap=}
			echo -n "Setting system keymap to: $KEYMAP..."
			echo "$KEYMAP" > /etc/keymap.conf
			status ;;
		laptop)
			# Laptop option to load ac and battery Kernel modules.
			echo "Loading laptop modules: ac, battery, fan, yenta_socket..."
			for mod in ac battery fan yenta_socket
			do
				modprobe $mod
			done ;;
		mount)
			# Mount all ext3 partitions found (opt: mount).
			# Get the list of partitions.
			DEVICES_LIST=`fdisk -l | grep 83 | cut -d " " -f 1`
			# Mount filesystems rw.
			for device in $DEVICES_LIST
			do
				name=${device#/dev/}
				# Device can be already used by home=usb.
				if ! mount | grep ^$device >/dev/null; then
					echo "Mounting partition: $name on /mnt/$name"
					mkdir /mnt/$name
					mount $device /mnt/$name
				fi
			done ;;
		mount-packages)
			# Mount and install packages-XXX.iso (usefull without Internet 
			# connection).
			PKGSIGN="LABEL=\"packages-$(cat /etc/slitaz-release)\" TYPE=\"iso9660\""
			PKGDEV=$(blkid | grep "$PKGSIGN" | cut -d: -f1)
			[ -z "$PKGDEV" -a -L /dev/cdrom ] && \
				PKGDEV=$(blkid /dev/cdrom | grep "$PKGSIGN" | cut -d: -f1)
			if [ -n "$PKGDEV" ]; then
				echo -n "Mounting packages archive from $PKGDEV..."
				mkdir /packages && mount -t iso9660 -o ro $PKGDEV /packages
				status
				/packages/install.sh
			fi ;;
		wm=*)
			# Check for a Window Manager (for a flavor, default WM can be changed
			# with boot option or with an addfile in /etc/X11/wm.default.
			WM=${opt#wm=}
			mkdir -p /etc/X11
			case $WM in
				jwm)
					echo "jwm" > /etc/X11/wm.default ;;
				ob|openbox|openbox-session)
					echo "openbox" > /etc/X11/wm.default ;;
				e17|enlightenment|enlightenment_start)
					echo "enlightenment" > /etc/X11/wm.default ;;
			esac ;;
		*)
			continue ;;
	esac
done

# If no default WM fallback to Openbox (we never know).
if [ ! -f /etc/X11/wm.default ]; then
	echo "openbox" > /etc/X11/wm.default
fi

# Activate an eventual swap file or partition.
if [ "`fdisk -l | grep swap`" ]; then
	for SWAP_DEV in `fdisk -l | grep swap | awk '{ print $1 }'`; do
		echo "Swap memory detected on: $SWAP_DEV"
		add_swap_in_fstab $SWAP_DEV
	done
fi
if grep -q swap /etc/fstab; then
	echo "Activating swap memory..."
	swapon -a
fi
