#!/sbin/runscript
# Copyright 2004-2009 Fabio Erculiani
# Distributed under the terms of the GNU General Public License v2 

. /sbin/rogentos-functions.sh

depend() {
	after x-setup
	before hald
	before xdm
}

prepare_fluxbox_locked_autologin() {

	# change default wm to fluxbox
	echo "[Desktop]" > /home/${LIVE_USER}/.dmrc
	echo "Session=fluxbox" >> /home/${LIVE_USER}/.dmrc
	chown ${LIVE_USER} /home/${LIVE_USER}/.dmrc
	# Change passwords for security
	echo root:\`pwgen -s 16\` | chpasswd  > /dev/null 2>&1
	echo ${LIVE_USER}:\`pwgen -s 16\` | chpasswd  > /dev/null 2>&1

}

my_setup_desktop() {

	# create LIVE_USER if it does not exist
	sabayon_setup_live_user "${LIVE_USER}" "1000"
	if [ "${?}" = "1" ]; then
		# if user is already available, then setup skel
		# Copy ${LIVE_USER} directory
		rm -rf /home/${LIVE_USER}
		cp /etc/skel /home/${LIVE_USER} -Rp
		chown ${LIVE_USER}:users /home/${LIVE_USER} -R
	fi

	liveinst_desktop="/usr/share/applications/liveinst.desktop"
	liveinst_desktop_name="$(basename ${liveinst_desktop})"
	if [ -f "${liveinst_desktop}" ]; then
		[[ -d "/home/${LIVE_USER}/Desktop" ]] || mkdir -p /home/${LIVE_USER}/Desktop 
		cp "${liveinst_desktop}" /home/${LIVE_USER}/Desktop
		chown ${LIVE_USER}:users /home/${LIVE_USER}/Desktop -R
		chmod +x /home/${LIVE_USER}/Desktop/${liveinst_desktop_name}
		rm -f /etc/skel/Desktop/Anaconda*.desktop
		rm -f /home/${LIVE_USER}/Desktop/Anaconda*.desktop
	fi

	# Disable memory eating services
	rm -f /etc/xdg/autostart/hplip-systray.desktop
	rm -f /etc/xdg/autostart/beagle-search-autostart.desktop
	rm -f /etc/xdg/autostart/tracker*.desktop
	rm -f /etc/xdg/autostart/magneto.desktop
	rm -f /etc/xdg/autostart/beagled-autostart.desktop
	rm -f /usr/share/autostart/magneto.desktop
	rm -f /usr/share/autostart/nepomukserver.desktop

        # Remove broken entries in /etc/mtab
        sed -i '/.*newroot.*/d' /etc/mtab

	# Create /media for removable devices
	if [ ! -d /media ]; then
		mkdir /media
		chmod 755 /media
	fi

	# Add sudo to gparted exec=
	gparted_file="/home/${LIVE_USER}/Desktop/gparted.desktop"
	if [ -f "${gparted_file}" ]; then
		sed -i 's/Exec=/Exec=sudo /' "${gparted_file}"
	fi

	# create /overlay, this way df -h won't bitch
	[[ -d "/overlay" ]] || mkdir /overlay

}

my_setup_password() {

	cmdline_autoscramble_exist=$(cat /proc/cmdline | grep autoscramble)
	if [ -n "$cmdline_autoscramble_exist" ]; then
		echo
		echo -e "\E[33;36m * \E[0m\E[01;36m Autoscrambling root passwords for   S E C U R I T Y"
		echo root:\`pwgen -s 16\` | chpasswd  > /dev/null 2>&1
		echo ${LIVE_USER}:\`pwgen -s 16\` | chpasswd  > /dev/null 2>&1
		echo
	fi

}

my_setup_keymap() {

	# Check if KEYMAP is forced by cmdline
	cmdline_keymap_exist=$(cat /proc/cmdline | grep -i "KEYMAP=")
	cmdline_keymap_isolinux_exist=$(cat /proc/cmdline | grep -i "console-setup/layoutcode=")

	if [ -n "$cmdline_keymap_isolinux_exist" ]; then
		cmdline_keymap_exist="console-setup"
	fi

        if [ -n "$cmdline_keymap_exist" ];then

		if [ "$cmdline_keymap_exist" == "console-setup" ]; then
			# detect keymap
			for word in `cat /proc/cmdline` ; do
				case $word in
					console-setup/layoutcode=*)
					keymap_toset=$(echo $word | cut -d "=" -f 2)
					;;
					console-setup/modelcode=*)
					keymap_toset_model="-$(echo $word | cut -d "=" -f 2)"
					;;
				esac
			done
		else

			# detect keymap
			for word in `cat /proc/cmdline` ; do
				case $word in
					KEYMAP=*)
					keymap_toset=$(echo $word | cut -d "=" -f 2)
					;;
					keymap=*)
					keymap_toset=$(echo $word | cut -d "=" -f 2)
					;;
				esac
			done

		fi

		if [ -n "$keymap_toset" ]; then
			aggregated_keymap="${keymap_toset}${keymap_toset_model}"
			/sbin/keyboard-setup-2 ${aggregated_keymap} all &> /dev/null
			if [ "${?}" = "0" ]; then
				/etc/init.d/keymaps restart --nodeps
			fi
		fi
	
	fi

}

my_wait_opengl_config() {

	echo -en "\E[33;36m * \E[0m \E[01;32m Waiting for Graphical Configurator to finish... \E[0m"
	while [ -e "/etc/x-setup-configuration-running" ]
           do
	      # waiting...
	      sleep 1
	   done
	echo -e "Done"

}

my_setup_locale() {

        cmdline_lang_exist=$(cat /proc/cmdline | grep -i "lang=")
        cmdline_locale_exist=$(cat /proc/cmdline | grep -i "locale=")

	if [ -n "$cmdline_locale_exist" ]; then
		cmdline_lang_exist="locale-setup"
	fi

        if [ -n "$cmdline_lang_exist" ];then

		if [ "$cmdline_lang_exist" == "locale-setup" ]; then
			# detect keymap
			for word in `cat /proc/cmdline` ; do
				case $word in
					locale=*)
					lang_toset=$(echo $word | cut -d "=" -f 2)
					;;
				esac
			done
		else
			for word in `cat /proc/cmdline` ; do
				case $word in
					LANG=*)
					lang_toset=$(echo $word | cut -d "=" -f 2)
					;;
					lang=*)
					lang_toset=$(echo $word | cut -d "=" -f 2)
					;;
				esac
			done
		fi
		
		# Setup Language
		if [ -n "$lang_toset" ]; then
			/sbin/language-setup $lang_toset &> /dev/null
		fi

        fi

}

start() {

	# Perform configuration only in live mode
	if ! sabayon_is_live; then
		einfo "Skipping live mode configuration"
		return 0
	fi

	my_setup_desktop
	my_setup_password
	my_setup_keymap
	my_wait_opengl_config
	# MOVED HERE TO AVOID RACE CONDITIONS ON WRITING /etc/profile.env variables
	# Check if LANG is forced by cmdline
	my_setup_locale
	# setup autologin for all the supported Login managers
	sabayon_setup_autologin
	# override autostart stuff if required:
	sabayon_setup_motd
	sabayon_setup_vt_autologin
	sabayon_setup_oem_livecd

}