#!/bin/sh
export PATH=$PATH:/usr/bin:/bin:/sbin
export TERM=linux
tempfile=/tmp/test$$

if [ "`which openbox`" != "" ] && [ -f /usr/bin/mbsession ]; then
	MSELECT1K="O"
	MSELECT1L="OpenBox"
fi
if [ "`which icewm`" != "" ] && [ -f /usr/bin/ice-session ]; then
	MSELECT2K="I"
	MSELECT2L="IceWM"
fi
if [ "`which fluxbox`" != "" ] && [ -f /usr/bin/fbsession ]; then
	MSELECT3K="F"
	MSELECT3L="FluxBox"
fi
if [ "`which xfce`" != "" ] && [ -f /usr/bin/xfce-session ]; then
	MSELECT4K="X"
	MSELECT4L="XFCE"
fi
if [ "`which kde`" != "" ] && [ -f /usr/bin/kde-session ]; then
	MSELECT5K="K"
	MSELECT5L="KDE"
fi
if [ "`which enlightenment`" != "" ] && [ -f /usr/bin/e17-session ]; then
	MSELECT6K="E"
	MSELECT6L="Enlightment"
fi

dialog --sleep 3 \
       --title "X Window Manager Selector for pdaXii13" \
       --infobox "\\nSwitch default Window Manager." 10 52

dialog --clear --title "X Window Manager Selector for pdaXii13" \
       --menu "\nChoose a Window Manager:" 15 70 7 \
       $MSELECT1K $MSELECT1L \
       $MSELECT2K $MSELECT2L \
       $MSELECT3K $MSELECT3L \
       $MSELECT4K $MSELECT4L \
       $MSELECT5K $MSELECT5L \
       $MSELECT6K $MSELECT6L \
    		"Q" "Quit" 2> $tempfile
retval=$?
choice=`cat $tempfile`
clear
case $retval in
    0)
        case $choice in
	    O|o)
	        if [ -f /usr/bin/mbsession ]; then
		  ln -sf /usr/bin/mbsession $HOME/.xinitrc
		  sudo ln -sf /usr/bin/mbsession /etc/X11/xinit/xinitrc 2>/dev/null
		  echo "window manager changed to openbox"
	        else
		  echo "error: openbox not properly installed"
		fi
		break
		;;
	    I|i)
	        if [ -f /usr/bin/ice-session ]; then
		  ln -sf /usr/bin/ice-session $HOME/.xinitrc
		  sudo ln -sf /usr/bin/ice-session /etc/X11/xinit/xinitrc 2>/dev/null
		  echo "window manager changed to icewm"
	        else
		  echo "error: icewm not properly installed"
	        fi
		break
		;;
	    F|f)
	        if [ -f /usr/bin/fbsession ]; then
		  ln -sf /usr/bin/fbsession $HOME/.xinitrc
		  sudo ln -sf /usr/bin/fbsession /etc/X11/xinit/xinitrc 2>/dev/null
		  echo "window manager changed to fluxbox"
	        else
		  echo "error: fluxbox not properly installed"
		fi
		break
		;;
	    X|x)
	        if [ -f /usr/bin/xfce-session ]; then
		  ln -sf /usr/bin/xfce-session $HOME/.xinitrc
		  sudo ln -sf /usr/bin/xfce-session /etc/X11/xinit/xinitrc 2>/dev/null
		  echo "window manager changed to xfce"
	        else
		  echo "error: xfce not properly installed"
		fi
		break
		;;
	    K|k)
	        if [ -f /usr/bin/kde-session ]; then
		  ln -sf /usr/bin/kde-session $HOME/.xinitrc
		  sudo ln -sf /usr/bin/kde-session /etc/X11/xinit/xinitrc 2>/dev/null
		  echo "window manager changed to kde"
	        else
		  echo "error: kde not properly installed"
		fi
		break
		;;
	    E|e)
	        if [ -f /usr/bin/e17-session ]; then
		  ln -sf /usr/bin/e17-session $HOME/.xinitrc
		  sudo ln -sf /usr/bin/e17-session /etc/X11/xinit/xinitrc 2>/dev/null
		  echo "window manager changed to e17"
	        else
		  echo "error: e17 not properly installed"
		fi
		break
		;;
	    Q|q)
	        break;	
		;;
	esac
	;;
    1)
	exit 1
	;;
    255)
	exit 255
	;;
esac
exit 0

