 
#!/bin/bash

MODPROBE=/sbin/modprobe
IFCONFIG=/sbin/ifconfig

ahostname(){
    MYHOST="$1_`$IFCONFIG eth0 | awk '/HWaddr/ {print $5}' | tr -d ':'`"
    echo "STATELESS: Setting Hostname to $MYHOST"
    echo "HOSTNAME=\"$MYHOST\"" > /etc/conf.d/hostname
    /bin/hostname "$MYHOST"
}

getparams() {
	local cmdline=$(dmesg | grep '^Kernel command line' | sed 's/^Kernel command line://g')
	for pp in $cmdline; do
		echo $pp | grep '^stateless=' >/dev/null 2>&1
		if [ $? -eq 0 ]; then
			echo $pp | sed 's/stateless=//g'
			return 0
		fi
	done
	echo ""
	return 1
}

isset() {
	for p in $(getparams | tr ',' ' '); do
		if [ "$p" == "$1" ]; then
			return 0
		fi
	done
	return 1
}

aunionfs() {
    isset unionfs
    if [ $? -eq 0 ]; then
        echo "STATELESS: Loading module unionfs ..."
        $MODPROBE unionfs
	while [ "$1" != "" ]; do
            echo "STATELESS: Mounting tmpfs $1 ..."
            mount -n -t tmpfs -o defaults none /mnt/unionfs/$1
            echo "STATELESS: Mounting $1 unionfs ..."
            mount -n -t unionfs -o dirs=/mnt/unionfs/$1=rw:/$1=ro none /$1
	    shift
        done
    else
        echo "STATELESS: Not using unionfs as requested ..."
    fi
}

aunionfs etc var
ahostname stateless
exec /sbin/init