#!/bin/sh
# @vasm : XWMSET
# @level: user
# @description: choose a X windows manager to be launched by startx
# 
# (c) Kocil, 2003
# (c) Vector Linux, 2003

if [ ! $DISPLAY = "" ]; then
  DCMD="Xdialog --wrap" 
  CMD="rxvt -e "
  BACKTITLE=''  
else
  DCMD="dialog"
  CMD=""
  BACKTITLE='Vector Linux ... at the speed of light'
fi

XINIT_DIR="/etc/X11/xinit"
XINIT_GLOBAL="$XINIT_DIR/xinitrc"
XINIT_HOME="$HOME/.xinitrc"

fmenu=`mktemp -q /tmp/menu.XXXXXX` && freply=`mktemp -q /tmp/reply.XXXXXX`
if [ $? != 0 ]; then
   echo Can not create temp file 
   exit 1
fi

# just a debugging aid.
dbug() {
  echo $* > /dev/null
}

clean_exit()
{
  rm -f $fmenu
  rm -f $freply
  exit
}

################################################
wm_description()
{
case "$1" in 
  KDE*|kde*) 
     echo "A desktop with everything you want"
     ;;
  GNOME*|gnome*) 
     echo "A desktop with everything you want, for free"
     ;;
  dropline|metacity)
     echo "When GNOME is too heavy for you"
     ;;
  XFCE*|xfce*)
     echo "Fast, good looking, medium weight desktop environment"
     ;;
  IceWM*|icewm*)
     echo "Cool, effective windows manager"
     ;;
  fluxbox*|blackbox*|openbox*)
     echo "No nonsense windows manager"
     ;;
  windowmaker*|wmmaker*)
     echo "The most decorated window manager"
     ;;
  ROX|rox|rof-filer)
     echo "This one rocks !!!"
     ;;
  *)
     echo "Another X window manager"
     ;;
esac
}

wm_personality()
{
case "$CHOICE" in
KDE*|kde*) 
echo "You are a star although some people say you're just a fat funny clown.
Eitherway, you do your best to make the world happy, cheerfull and less productive."
;;
GNOME*|gnome*)
echo "You were born in desperation. Although you are still under achiever,
a lot of loyal supporters always sing \"You'll never walk alone\""
;;
dropline)
echo "You like to be a hero, especially when someone you love is having a bad time"
;;
XFCE*|xfce*|xfsession*)
echo "You must be a romantic person that love sensuality as well as sensibility"
;;
fluxbox*)
echo "You are a geek, anti capitalist, anti comunist, anti everything.
Luckily, you are doing so well so everybody want you in the team"
;;
blackbox*)
echo "You are a wise old person. You don't do too much anymore but 
still happy to see your descendants are flourishing on the promised land"
;;
IceWM*|icewm*)
echo "You are a modest person that enjoy a simple, beautifull life.
Your intention is to be productive, although sometimes you are going for party too."
;;
XwmMenu*|xwmmenu*)
echo "On the bright side, you are a friendly guy. 
But you are also a compromist that can not make up your own mind"
;;
tvwm*)
echo "You are a pathetic. Nobody want you anymore, but you seem to enjoy your self"
;;
*)
echo "You are an adventourer, always trying something different although there is no support" 
;;
esac
}


###################################################

TITLE="SELECT WINDOWS MANAGER"

if [ "$GID" = "0" ]; then
TEXT="\nThis configurator setup a default window manager 
for all users that might be launched when the user
call startx from text console.\n
Please select one from the list:"
else
TEXT="\nThis configurator setup a window manager that will be 
launched when you call 'startx' from text console.\n
Please select your favourite:"
fi

DIMENSION="18 70 8"
[ "$CMD" ] && DIMENSION="20 70 8"

echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \' > $fmenu
ii=1
for XINIT in $XINIT_DIR/xinitrc.*; do
   if [ -x $XINIT ]; then
      NAME=`basename $XINIT | cut -d . -f 2`
      DESCRIPTION=`grep "@description:" $XINIT | cut -d : -f 2 | sed -e "s/^ //g"`
      if [ -z "$DESCRIPTION" ]; then
         DESCRIPTION=`wm_description $NAME`
      fi
      echo $NAME \"$DESCRIPTION\" \\ >> $fmenu
      let ii=ii+1
   fi
done
echo ' 2> $freply ' >> $fmenu

if [ $ii = 0 ]; then
  $DCMD "$BACKTITLE" --title " NO INIT SCRIPT " --msgbox \
"\nYou have a big big problem, mate.\n
I could not find any xinit script in $XINIT_DIR\n
You may need to reinstall this system.\n" 0 0
  clean_exit
fi

# cat $fmenu

. $fmenu

rm $fmenu
[ ! $? = 0 ] && clean_exit

CHOICE=`cat $freply`

dbug "Choice=$CHOICE"

[ -z "$CHOICE" ] && clean_exit

if [ "$GID" = 0 ]; then
  rm -rf $XINIT_GLOBAL
  ln -sf $XINIT_DIR/xinitrc.$CHOICE $XINIT_GLOBAL

  TITLE="DONE"
  TEXT="You have selected $CHOICE as the default windows manager
for users that does not set their own own default WM"

else

  rm -rf $XINIT_HOME
  cp $XINIT_DIR/xinitrc.$CHOICE $XINIT_HOME

  TITLE="Window Manager & You"
  TEXT="You chose $CHOICE.\n"`wm_personality $CHOICE`
fi

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --msgbox "$TEXT"  0 0

clean_exit

