#!/bin/sh

## live-config(7) - System Configuration Scripts
## Copyright (C) 2006-2012 Daniel Baumann <daniel@debian.org>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


Xserver_xorg ()
{
	# Checking if package is installed
	if [ ! -e /var/lib/dpkg/info/xserver-xorg.list ] || \
	   [ -e /var/lib/live/config/xserver-xorg ]
	then
		return
	fi

	echo -n " xserver-xorg"

	for _PARAMETER in ${_CMDLINE}
	do
		case "${_PARAMETER}" in
			live-config.keyboard-layouts=*|keyboard-layouts=*)
				LIVE_KEYBOARD_LAYOUTS="${_PARAMETER#*keyboard-layouts=}"
				;;

			live-config.keyboard-model=*|keyboard-model=*)
				LIVE_KEYBOARD_MODEL="${_PARAMETER#*keyboard-model=}"
				;;

			live-config.keyboard-options=*|keyboard-options=*)
				LIVE_KEYBOARD_OPTIONS="${_PARAMETER#*keyboard-options=}"
				;;

			live-config.keyboard-variants=*|keyboard-variants=*)
				LIVE_KEYBOARD_VARIANTS="${_PARAMETER#*keyboard-variants=}"
				;;

			live-config.xorg-xsession-manager=*|x-session-manager=*)
				LIVE_X_SESSION_MANAGER="${_PARAMETER#*x-session-manager=}"
				;;

			live-config.xorg-driver=*|xorg-driver=*)
				LIVE_XORG_DRIVER="${_PARAMETER#*xorg-driver=}"
				;;

			live-config.xorg-resolution=*|xorg-resolution=*)
				LIVE_XORG_RESOLUTION="${_PARAMETER#*xorg-resolution=}"
				;;
		esac
	done

	Configure_xserver_xorg
}

Configure_xserver_xorg ()
{
	if [ -n "${LIVE_KEYBOARD_MODEL}" ]
	then
		echo "xserver-xorg xserver-xorg/config/inputdevice/keyboard/model select ${LIVE_KEYBOARD_MODEL}" >> /tmp/debconf.live
	fi

	if [ -n "${LIVE_KEYBOARD_LAYOUTS}" ]
	then
		echo "xserver-xorg xserver-xorg/config/inputdevice/keyboard/layout select ${LIVE_KEYBOARD_LAYOUTS}" >> /tmp/debconf.live
	fi

	if [ -n "${LIVE_KEYBOARD_VARIANTS}" ]
	then
		echo "xserver-xorg xserver-xorg/config/inputdevice/keyboard/variant select ${LIVE_KEYBOARD_VARIANTS}" >> /tmp/debconf.live
	fi

	if [ -n "${LIVE_KEYBOARD_OPTIONS}" ]
	then
		echo "xserver-xorg xserver-xorg/config/inputdevice/keyboard/options string ${LIVE_KEYBOARD_OPTIONS}" >> /tmp/debconf.live
	fi

	if [ -n "${LIVE_X_SESSION_MANAGER}" ]
	then
		case "${LIVE_X_SESSION_MANAGER}" in
			none)
				rm -f /etc/X11/default-display-manager
				;;

			*)
				update-alternatives --quiet --set x-session-manager "${LIVE_X_SESSION_MANAGER}"
				;;
		esac
	fi

	if [ -z "${LIVE_XORG_DRIVER}" ]
	then
		# If no driver was specified and one of the nvidia drivers is installed,
		# prefer the newest available nvidia driver over nouveau.

		# PCI ID of the first nvidia card found
		_NVIDIA_DEVICE="$(lspci -mn | awk '{ gsub("\"",""); if ($2 == "0300" && ($3 == "10de" || $3 == "12d2")) { print $3$4 } }' | tr [a-z] [A-Z] | head -1)"

		if [ -n "${_NVIDIA_DEVICE}" ]
		then
			for _NVIDIA_IDS in /usr/lib/nvidia/current/nvidia.ids $(ls /usr/lib/nvidia/legacy-*/nvidia.ids | sort -V -r)
			do
				if [ -e "${_NVIDIA_IDS}" ]
				then
					if grep -qs "${_NVIDIA_DEVICE}" ${_NVIDIA_IDS}
					then
						_NVIDIA_VERSION="$(basename $(dirname ${_NVIDIA_IDS}))"

						break
					fi
				fi
			done
		fi

		if [ -n "${_NVIDIA_VERSION}" ]
		then
			LIVE_XORG_DRIVER="nvidia"

			update-alternatives --quiet --set nvidia ${_NVIDIA_VERSION}
			modprobe --ignore-install nvidia-${_NVIDIA_VERSION}
		fi
	fi

	if [ -n "${LIVE_XORG_DRIVER}" ]
	then

		mkdir -p /etc/X11/xorg.conf.d

cat > /etc/X11/xorg.conf.d/99-live.conf << EOF
Section "Device"
	Identifier	"Default screen"
	Driver		"${LIVE_XORG_DRIVER}"
EndSection
EOF

		case "${LIVE_XORG_DRIVER}" in
			nvidia)
				# wheezy
				if [ -e /etc/alternatives/glx ]
				then
					update-alternatives --quiet --set glx nvidia
				fi
				;;

			*)
				# wheezy
				if [ -e /etc/alternatives/glx ]
				then
					update-alternatives --quiet --set glx mesa-diverted
				fi
				;;
		esac
	fi

	if [ -n "${LIVE_XORG_RESOLUTION}" ]
	then
		echo "xrandr -s ${LIVE_XORG_RESOLUTION} || /bin/true" >> /etc/X11/Xsession.d/21xvidemode
	else
		rm -f /etc/X11/Xsession.d/21xvidemode
	fi

	if [ -e /tmp/debconf.live ]
	then
		debconf-set-selections < /tmp/debconf.live
		rm -f /tmp/debconf.live

		dpkg-reconfigure -f noninteractive -p critical \
			xserver-xorg 2>&1 \
			| grep -v "overwriting possibly-customised configuration" \
			| grep -v "file; backup in /etc/X11/xorg.conf" || true

		# Creating state file
		touch /var/lib/live/config/xserver-xorg
	fi
}

Xserver_xorg
