#!/bin/sh
#
# Tazx - Ncurses X configuration for SliTaz GNU/Linux using Dialog boxes.
# This tinyutils is part of slitaz-tools.
#
# 20080313 <pankso@slitaz.org> - GNU gpl v3.
#
: ${DIALOG=dialog}

# Variables.
#
XSERVER=Xvesa
DOC=/usr/share/doc/slitaz-tools/tazx.txt
MOUSE=/dev/input/mice,5
# WM can be specified on cmdline.
if [ -n "$1" ]; then
	WM=$1
else
	WM=openbox
fi

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

# Install xorg server
install_xorg()
{
	tazpkg recharge
	exec 3>&1
	value=`$DIALOG --clear --colors --title " Install Xorg " \
		--menu \
	"L'application 'tazx' permet de configurer le driver X." 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 /root/xorg.conf.new /etc/X11/xorg.conf
			. /etc/kmap.conf
			patch -p0 <<EOF
--- /etc/X11/xorg.conf
+++ /etc/X11/xorg.conf
@@ -30,2 +30,3 @@
 	Driver      "kbd"
+	Option      "XkbLayout"	"${KMAP%.kmap}"
 EndSection
EOF
			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 --help-button \
		--clear --colors \
		--title " Configure X " \
		--menu \
	"L'application 'tazx' permet de configurer une session X.\n\
	Gestionnaire de fentres : \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" "Installer Xorg" \
	"quit" "Quitter" \
	2>&1 1>&3`
	retval=$?
	exec 3>&-
	# Continue, exit or help...
	case $retval in
		0)
			continue ;;
		1)
			echo "Cancel pressed..."
			exit 0 ;;
		2)
			$DIALOG --clear \
			--title " Aide - Help " --textbox "$DOC" 16 70
			exec tazx ;;
		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 ;;
		*)
			NEW_SCREEN=$value ;;
	esac
}

# Slim config if root.
slim_config()
{
	if test $(id -u) = 0; then
		# /etc/X11/screen.conf exist for Live mode, if this file dos 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 *//')
			sed -i "s/\(xserver_arguments.*-screen\).*/\1 $NEW_SCREEN/" /etc/slim.conf
		fi
		JWM_CONFIG=/etc/jwm/system.jwmrc
	fi
}

# e17 start with enlightenment_start.
e17_stuff()
{
	if [ "$WM" == "e17" ]; then
		 WM=enlightenment_start
		 XSEVER_OPTS="dpms -terminate"
	fi
}

# Openbox with openbox-session.
openbox_stuff()
{
	if [ "$WM" == "openbox" -o "$WM" == "ob" ]; then
		 WM=openbox-session
		 XSEVER_OPTS="dpms +extension Composite"
	fi
	# Check if a personnal autostart script exist 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 hacker.
		if [ ! -f "/home/hacker/.config/openbox/autostart.sh" ]; then
			mkdir -p /home/hacker/.config/openbox
			cp /etc/xdg/openbox/autostart.sh /home/hacker/.config/openbox
		fi
		if [ ! -f "/home/hacker/.config/openbox/menu.xml" ]; then
			mkdir -p /home/hacker/.config/openbox
			cp /etc/xdg/openbox/menu.xml /home/hacker/.config/openbox
		fi
		chown -R hacker.hacker /home/hacker/.config
	fi
}

# 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 or 'tazx' to configure permanantly 
# your default WM: tazx e17
#
WM=openbox-session

case $1 in
	e17|enlightenment_start)
		exec enlightenment_start
		;;
	openbox|openbox-session|ob)
		exec openbox-session
		;;
	jwm)
		lxpanel &
		exec jwm
		;;
	*)
		exec $WM
		;;
esac
EOF
	# Set default WM.
	RES=`grep ^WM=[e-o] $FILE`
	sed -i s/"$RES"/"WM=$WM"/ $FILE
}

# ~/.xinitrc for slim login.
creat_xinitrc()
{
	FILE=$HOME/.xinitrc
	xinitrc_sample
	
	# In Live mode hacker needs a xinitrc, since tazx 
	# is executed only by root.
	if [ ! -f /home/hacker/.xinitrc ]; then
		FILE=/home/hacker/.xinitrc
		xinitrc_sample
		chown hacker.hacker $FILE
	fi
}

# Copy a JWM system config if any present in user home.
jwm_config()
{
	if [ -d "/var/lib/tazpkg/installed/jwm" ]; then
		JWM_CONFIG=$HOME/.jwmrc
		if [ ! -f "$JWM_CONFIG" ]; then
			cp /etc/jwm/system.jwmrc $JWM_CONFIG
		fi
		# In Live mode hacker/root JWM config dose not exist ans $HOME is 
		# ot set, this is because tazx is executed by boot scripts.
		if [ ! -f "/home/hacker/.jwmrc" ]; then
			JWM_CONFIG=/home/hacker/.jwmrc
			cp /etc/jwm/system.jwmrc $JWM_CONFIG
			chown hacker.hacker $JWM_CONFIG
		fi
		if [ ! -f "/root/.jwmrc" -a `id -u` = 0 ]; then
			JWM_CONFIG=/root/.jwmrc
			cp /etc/jwm/system.jwmrc $JWM_CONFIG
		fi
	fi
}

# Creat ~/.xsession to keep the configuration selected (used 
# only by startx, Slim login manager use .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
		echo "$XSERVER -ac -shadow $XSEVER_OPTS -screen $NEW_SCREEN -mouse $MOUSE &" \
			>> $HOME/.xsession
	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  #
###################

[ -n "$NEW_SCREEN" ] || screen_config_dialog
slim_config
e17_stuff
openbox_stuff
creat_xinitrc
jwm_config
creat_xsession

exit 0
