#!/bin/sh
#
# Tazx - Ncurses X configuration for SliTaz GNU/Linux using Dialog boxes.
# This tinyutils is part of slitaz-tools. Tazx can configure Xvesa kdrive
# and Xorg with several Window Manager.
#
# (c) 2009 SliTaz GNU/Linux - GNU gpl v3.
# Authors: Christophe Lincoln <pankso@slitaz.org>
#          Pascal Bellard <pascal.bellard@slitaz.org>
#
: ${DIALOG=tazdialog}

# Default value.
XSERVER=Xvesa
KEYBD=keyboard
MOUSE=mouse,5,/dev/input/mice
WM=openbox

# Default user for config files in Live mode.
[ -z $USER ] && USER=`cat /etc/passwd | grep 1000 | cut -d ":" -f 1`

####################
#  Tazx functions  #
####################

# Patch xorg.conf to set keymap layout.
patch_xorg_kbd()
{
	KEYMAP=`cat /etc/keymap.conf`
	case $KEYMAP in
		fr_CH-latin1)
			# Swiss FrenCH
			patch -p0 <<EOF
--- /etc/X11/xorg.conf
+++ /etc/X11/xorg.conf
@@ -30,2 +30,3 @@
 	Driver      "kbd"
+	Option      "XkbLayout"	 "ch"
+	Option      "XkbVariant" "fr"
 EndSection
EOF
			;;
		en)
			# Englisk UK
			patch -p0 <<EOF
--- /etc/X11/xorg.conf
+++ /etc/X11/xorg.conf
@@ -30,2 +30,3 @@
 	Driver      "kbd"
+	Option      "XkbLayout"	 "en"
+	Option      "XkbVariant" "en"
 EndSection
EOF
			;;
		ru)
			# Russian
			patch -p0 <<EOF
--- /etc/X11/xorg.conf
+++ /etc/X11/xorg.conf
@@ -30,2 +30,3 @@
 	Driver      "kbd"
+	Option      "XkbLayout"	 "us,ru(winkeys)"
+	Option      "XkbVariant" "grp:alt_shift_toggle"
 EndSection
EOF
			;;
		slovene)
			# Slovenian
			patch -p0 <<EOF
--- /etc/X11/xorg.conf
+++ /etc/X11/xorg.conf
@@ -30,2 +30,3 @@
 	Driver      "kbd"
+	Option      "XkbLayout"  "si"
+	Option      "XkbOptions" "grp:alt_shift_toggle"
 EndSection
EOF
			;;
		*)
			# Use clean /etc/keymap.conf value.
			KEYMAP=${KEYMAP%-latin1}
			KEYMAP=${KEYMAP%-lat2}
			KEYMAP=${KEYMAP%-lat6}
			KEYMAP=${KEYMAP%-abnt2}
			patch -p0 <<EOF
--- /etc/X11/xorg.conf
+++ /etc/X11/xorg.conf
@@ -30,2 +30,3 @@
 	Driver      "kbd"
+	Option      "XkbLayout"	"$KEYMAP"
 EndSection
EOF
		;;
	esac
}

# Install xorg server
install_xorg()
{
	tazpkg recharge
	exec 3>&1
	value=`$DIALOG --clear --colors --title " Install Xorg " \
		--menu \
	"The 'tazx' application helps you to select your X driver." 16 70 5 \
	$(grep xorg-xf86-video /var/lib/tazpkg/packages.list | cut -d- -f4 | while read x; do echo $x; echo driver; done) \
	"quit" "Quitter" \
	2>&1 1>&3`
	retval=$?
	exec 3>&-
	# Continue, exit...
	case $retval in
		1)
			echo "Cancel pressed..."
			exit 0 ;;
		255)
			if test -z "$value"; then
				echo "ESC pressed..."
				exit 0
			fi ;;
	esac
	# Set selected value.
	case $value in
		quit)
			echo "Quit..."
			exit 0 ;;
		*)
			tazpkg get-install xorg-server
			tazpkg get-install xorg-xf86-video-$value
			Xorg -configure :1
			mv -f /root/xorg.conf.new /etc/X11/xorg.conf
			patch_xorg_kbd
			sed -i 's|/usr/bin/Xvesa|/usr/bin/Xorg|' /etc/slim.conf
			sed -i s/"^xserver_arguments"/'\#xserver_arguments'/ /etc/slim.conf
			XSERVER=Xorg ;;
	esac
}

# Screen configuration dialog.
screen_config_dialog()
{
	exec 3>&1
	value=`$DIALOG \
		--clear --colors \
		--title " Configure X " \
		--menu \
	"The 'tazx' application helps you to configure your X session.\n\
	Window Manager : \Z2$WM\Zn" 16 70 5 \
	$(Xvesa -listmodes 2>&1 | grep ^0x | awk '{ printf "%s %s\n",$2,$3 }' | sort -nr | grep x[1-2][4-6]) \
	"xterm" "800x600x16" \
	"xorg" "Install Xorg" \
	"text" "Disable X autostart" \
	"quit" "Quitter" \
	2>&1 1>&3`
	retval=$?
	exec 3>&-
	# Continue, exit or help...
	case $retval in
		0)
			continue ;;
		1)
			echo "Cancel pressed..."
			exit 0 ;;
		255)
			if test -n "$value"; then
				continue
			else
				echo "ESC pressed..."
				exit 0
			fi ;;
	esac
	# Set selected value.
	case $value in
		xorg)
			install_xorg ;;
		xterm)
			Xvesa -ac -shadow -screen 800x600x16 -br &
			exec xterm -cr orange -geometry 80x35+0-0 ;;
		text)
			sed -i s/'slim'/''/ /etc/rcS.conf
			exit 0 ;;
		*)
			NEW_SCREEN=$value ;;
	esac
}

# Slim config if root.
slim_config()
{
	if test $(id -u) = 0; then
		# /etc/X11/screen.conf exists for Live mode, if this file does not
		# exist tazx is executed at boot time.
		mkdir -p /etc/X11
		echo "SCREEN=$NEW_SCREEN" > /etc/X11/screen.conf
		# Get current screen size and sed config file with the new value.
		if [ -f /etc/slim.conf ]; then
			RES=$(grep ^xserver_arguments /etc/slim.conf | \
				sed 's/xserver_arguments.*-screen *//' | awk '{ print $1 }')
			#sed -i "s/\(xserver_arguments.*-screen\).*/\1 $NEW_SCREEN/" /etc/slim.conf
			sed -i s/"-screen $RES"/"-screen $NEW_SCREEN"/ /etc/slim.conf
		fi
	fi
}

# Window manager specific configuration.
wm_config()
{
	case $WM in
		ob|openbox)
			WM=openbox-session
			XSEVER_OPTS="dpms +extension Composite"
			# Check if a personal autostart script exists if OB is installed.
			if [ -d "/var/lib/tazpkg/installed/openbox" ]; then
				if [ ! -f "$HOME/.config/openbox/autostart.sh" ]; then
					mkdir -p $HOME/.config/openbox
					cp /etc/xdg/openbox/autostart.sh $HOME/.config/openbox
				fi
				# Script for default user (uid=1000).
				if [ ! -f "/home/$USER/.config/openbox/autostart.sh" ]; then
					mkdir -p /home/$USER/.config/openbox
					cp /etc/xdg/openbox/autostart.sh /home/$USER/.config/openbox
				fi
				if [ ! -f "/home/$USER/.config/openbox/menu.xml" ]; then
					mkdir -p /home/$USER/.config/openbox
					cp /etc/xdg/openbox/menu.xml /home/$USER/.config/openbox
				fi
				chown -R $USER.$USER /home/$USER/.config
			fi ;;
		jwm)
			WM=jwm
			XSEVER_OPTS="dpms +extension Composite"
			JWM_CONFIG=$HOME/.jwmrc
			if [ -d "/var/lib/tazpkg/installed/jwm" ]; then
				if [ ! -f "$JWM_CONFIG" ]; then
					cp /etc/jwm/system.jwmrc $JWM_CONFIG
				fi
				# In Live mode default user/root JWM config does not exist and
				# $HOME is not set, this is because tazx is executed by boot 
				# scripts.
				if [ ! -f "/home/$USER/.jwmrc" ]; then
					cp /etc/jwm/system.jwmrc /home/$USER/.jwmrc
					chown $USER.$USER /home/$USER/.jwmrc
				fi
				if [ ! -f "/root/.jwmrc" -a `id -u` = 0 ]; then
					cp /etc/jwm/system.jwmrc /root/.jwmrc
				fi
			fi ;;
		pekwm)
			WM=pekwm
			XSEVER_OPTS="dpms"
			if [ -d "/var/lib/tazpkg/installed/pekwm" ]; then
				if [ -d "$HOME/.pekwm" ]; then
					cp -R /etc/pekwm $HOME/.pekwm
				fi
				# In Live mode we want config before starting pekwm the first time.
				if [ ! -d "/home/$USER/.pekwm" ]; then
					cp -R /etc/pekwm /home/$USER/.pekwm
					chown -R $USER.$USER /home/$USER/.pekwm
					chmod +x /home/$USER/.pekwm/start
				fi
				if [ ! -d "/root/.pekwm" -a `id -u` = 0 ]; then
					cp -R /etc/pekwm /root/.pekwm
					chmod +x /root/.pekwm/start
				fi
			fi ;;
		e17|enlightenment)
			WM=enlightenment_start
			XSEVER_OPTS="dpms -terminate" ;;
		fluxbox)
			WM=startfluxbox
			XSEVER_OPTS="dpms" ;;
		dwm|karmen)
			WM=$WM-session
			XSEVER_OPTS="dpms" ;;
		awesome)
			WM=awesome
			XSEVER_OPTS="dpms" ;;
	esac
}

# Sample xinitrc for user (WM can be specified with F1 at slim login).
xinitrc_sample()
{
	cat >  $FILE << "EOF"
# ~/.xinitrc: Executed by slim login manager to startx X session.
# You can use F1 with Slim to chage your window manager or configure
# it permanently with your personnal applications.conf file.
#
. $HOME/.config/slitaz/applications.conf

case $1 in
	e17|enlightenment*)
		exec enlightenment_start ;;
	openbox|openbox-session|ob)
		exec openbox-session ;;
	dwm|dwm-session)
		exec dwm-session ;;
	fluxbox|startfluxbox)
		exec startfluxbox ;;
	awesome)
		exec awesome ;;
	pekwm)
		exec pekwm ;;
	karmen|karmen-session)
		exec karmen-session ;;
	jwm)
		lxpanel &
		exec jwm ;;
	*)
		exec $WINDOW_MANAGER ;;
esac
EOF
	# Set default WM in applications.conf user file. Default WM can be
	# configured graphicaly with 'desktopbox tazapps'
	. $CONFIG
	sed -i s/"WINDOW_MANAGER=.*"/"WINDOW_MANAGER=\"$WM\""/ \
		$CONFIG
}

# ~/.xinitrc for slim login.
creat_xinitrc()
{
	FILE=$HOME/.xinitrc
	CONFIG=$HOME/.config/slitaz/applications.conf
	if [ ! -f $CONFIG ]; then
		mkdir -p $HOME/.config/slitaz
		cp /etc/slitaz/applications.conf $CONFIG
	fi
	xinitrc_sample
	# In Live mode default user needs a xinitrc, since tazx
	# is executed only by root.
	if [ ! -f /home/$USER/.xinitrc ]; then
		FILE=/home/$USER/.xinitrc
		CONFIG=/home/$USER/.config/slitaz/applications.conf
		if [ ! -f $CONFIG ]; then
			mkdir -p /home/$USER/.config/slitaz
			cp /etc/slitaz/applications.conf $CONFIG
		fi
		xinitrc_sample
		chown $USER.$USER $FILE
		chown -R $USER.$USER /home/$USER/.config/slitaz
	fi
}

# Create ~/.xsession to keep the configuration selected (used
# only by startx, Slim login manager uses .xinitrc).
creat_xsession()
{
	cat > $HOME/.xsession << _EOF_
# ~/.xsession: Start X window session manually on your system (startx).
#
_EOF_
	if [ "$XSERVER " == "Xorg" ]; then
		echo 'Xorg &' >> $HOME/.xsession
	else
		cat >> $HOME/.xsession << _EOT_
$XSERVER -ac -shadow $XSEVER_OPTS \\
	-screen $NEW_SCREEN \\
	-keybd $KEYBD \\
	-mouse $MOUSE &
_EOT_
	fi
	echo '#xterm &' >> $HOME/.xsession
	echo '#xpad &' >> $HOME/.xsession
	# LXpanel by default with JWM.
	if [ "$WM" = "jwm" ]; then
		echo 'lxpanel &' >> $HOME/.xsession
	fi
	echo "exec $WM" >> $HOME/.xsession
	chmod 700 $HOME/.xsession
}

###################
#  Tazx sequence  #
###################

case "$1" in
	show-config)
		. /etc/X11/screen.conf
		echo ""
		echo "X11 screen resolution: $SCREEN"
		echo ""
		echo "Slim configuration for X server:"
		cat /etc/slim.conf | grep ^default_xserver
		cat /etc/slim.conf | grep ^xserver_arguments
		echo "" ;;
	install-xorg)
		# WM can be specified on cmdline.
		if [ -n "$2" ]; then
			WM=$2	
		fi
		install_xorg
		slim_config
		wm_config
		creat_xinitrc
		creat_xsession ;;
	*)
		# WM can be specified on cmdline.
		if [ -n "$1" ]; then
			WM=$1	
		fi
		[ -n "$NEW_SCREEN" ] || screen_config_dialog
		slim_config
		wm_config
		creat_xinitrc
		creat_xsession ;;
esac

exit 0
