#!/bin/sh
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

	#/bin/mount -t sysfs none /sys -onodev,noexec,nosuid
	#/bin/mount -t proc none /proc -onodev,noexec,nosuid
	ln -sf /proc/mounts /etc/mtab

	for x in $(cat /proc/cmdline); do
		case $x in
			screen=*)
				export RES=${x#screen=}
				#echo $RES > /etc/xres.conf
			;;
			lang=*)
				export UILANG=${x#lang=}
			;;
			kmap=*)
				export KEYMAP=${x#kmap=}
			;;
			url=*)
				export URL=${x#url=}
			;;
		esac
	done 

	# keymap
	if [ ! -z $KEYMAP ] && [ -e /usr/share/keymap/$KEYMAP.kmap ]; then 
		loadkmap < /usr/share/keymap/$KEYMAP.kmap
		echo "pref(\"xpud.keymap\", \""$KEYMAP"\");" >> /usr/share/plate/defaults/preferences/prefs.js
	fi
	
	# screen resolution 
	
	if [ ! -z $RES ]; then
		cat >> /etc/X11/xorg.conf <<EOF
Section "Screen"
	Identifier	"Default Screen"
	Monitor		"Configured Monitor"
	Device		"Configured Video Device"
	DefaultDepth	24
    SubSection "Display"
        Modes      "$RES"
    EndSubSection
EndSection
EOF
	fi
	
	PLATE=/usr/share/plate/chrome/content
	# kiosk mode
	if [ ! -z $URL ]; then
		/bin/sed -e s/index.*html/kiosk.html/g $PLATE/main.default > $PLATE/main.xul 
		/bin/sed -e s#KIOSK_URL#$URL#g $PLATE/kiosk.html > $PLATE/kiosk.html.tmp 
		/bin/mv $PLATE/kiosk.html.tmp $PLATE/kiosk.html
	else
		# plate locale
		if [ ! -z $UILANG ]; then 
			UILANG=$UILANG
		else
			UILANG=en
		fi
		/bin/mv $PLATE/main.default $PLATE/main.xul
		echo "pref(\"xpud.locale\", \""$UILANG"\");" >> /usr/share/plate/defaults/preferences/prefs.js
		export LANG=$UILANG
	fi 
	
	# set locale
	export LC_ALL="en_US.UTF-8"
	
	# start udev daemon
	udevd --daemon
	udevadm trigger
	
	# init hook
	find /etc/init.d/ -type f -exec {} start \;

	if ! grep -q nox /proc/cmdline ; then
		# start X server
		startx
	else
		/usr/local/bin/post-boot.sh 1> /tmp/init.log 2> /tmp/init.err
	fi

	/bin/sh
