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

vdir=$(dirname $0)
. $vdir/vasm-functions

XINIT_DIR="/etc/X11/xinit"
XINIT_GLOBAL="$XINIT_DIR/xinitrc"
XINIT_HOME="$HOME/.xinitrc"
XSESSION_HOME="$HOME/.xsession"
CONFIG="/etc/sysconfig/X11"

WM_DEFAULT=""

new_wmconfig() {

cat<<EOF > $CONFIG
## X11 system configuration
## This file is read by /etc/rc.d/rc.X during init level 4/5
DISPLAY_MANAGER="WDM"
WINDOW_MANAGER="$1"
SOUND_SERVER=
EOF

}

## write down the configuration
set_wmconfig() {
  if [ -f $CONFIG ] ; then
    if grep -qe "^WINDOW_MANAGER=" $CONFIG ; then
       sed -i "s!^WINDOW_MANAGER=.*!WINDOW_MANAGER=$1!" $CONFIG
    else
       echo "WINDOW_MANAGER=$1" >> $CONFIG
    fi
  else
    new_wmconfig
  fi 
}



################################################
wm_description()
{
case "$1" in 
  KDE*|kde*) 
     echo "A desktop with more than anything you want"
     ;;
  GNOME*|gnome*) 
     echo "A desktop with almost everything you want"
     ;;
  dropline|metacity)
     echo "When GNOME is too complicated for you"
     ;;
  XFCE*|xfce*)
     echo "Good looking, medium weight desktop"
     ;;
  IceWM*|icewm*|icesession*)
     echo "Cool, effective windows manager"
     ;;
  fluxbox*|openbox*)
     echo "No nonsense windows manager"
     ;;
  blackbox*)
     echo "The ancestor of *box windows manager"
     ;;
  enlightenment*)
     echo "Fancy yet light windows manager"
     ;;
  windowmaker*|wmmaker*)
     echo "Window manager with its own style"
     ;;
  ROX|rox|rox-filer)
     echo "This one rocks !!!"
     ;;
  *)
     echo "Another X window manager"
     ;;
esac
}

wm_personality()
{
case "$CHOICE" in
KDE*|kde*) 
echo "You are a cosmopolitan who is obsessed to the popular style,
no matter it comes with high price."
;;
GNOME*|gnome*)
echo "You are a loyal fans that always
sings \"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*|icesession*)
echo "You are a modest person that enjoy a simple, beautiful life.
Your intention is to be productive, although sometimes you go for party too."
;;
XwmMenu*|xwmmenu*)
echo "On the bright side, you are a friendly guy. 
However, you are also a compromiser that can not make up your own mind."
;;
tvwm*)
echo "You are an antique collector that know the value of old stuff"
;;
ROX|rox|rox-filer)
echo "You want a revolution, though it is hard."
;;
enlightenment*)
echo "You really know the meaning of beautiful, outer and inner."
;;
*)
echo "You are an adventurer, always trying something different although there is no support." 
;;
esac
}

# Select WM
menuA()
{

TITLE="SELECT WINDOWS MANAGER"
if [ "$UID" = "0" ]; then
TEXT="\n
This configurator setup a default window manager\n
for all users. It will be launched when the user\n
call startx from the console or login using XDM.\n\n
Please select one from the list:"
DIMENSION="17 60 5"
else
TEXT="\n
This configurator setup a default window manager\n
that will be launched when you call 'startx' from\n
the console. It also became your default session\n
when you login via GUI.\n 
Please select your favourite:"
DIMENSION="18 60 5"
fi

echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \' > $fmenu
ii=0
for XINIT in $XINIT_DIR/xinitrc.*; do
   if [ -x $XINIT ]; then
      NAME=`basename $XINIT | cut -d . -f 2`
      DESCRIPTION=`wm_description $NAME`
      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
Create some or reinstall this system ;-).\n" 8 50 
  clean_exit 1
fi

. $fmenu

status=$?
[ $status != 0 ] && return $status

CHOICE=`cat $freply`
dbug "Choice=$CHOICE"

return 0
}


##################################################################
# DONE
menuB() {

if [ "$UID" = 0 ]; then
  infobox "Set $CHOICE as the default window manager for all user."
  rm -rf $XINIT_GLOBAL
  ln -sf $XINIT_DIR/xinitrc.$CHOICE $XINIT_GLOBAL
  set_wmconfig $CHOICE
  sleep 2
else
  rm -rf $XINIT_HOME
  cp $XINIT_DIR/xinitrc.$CHOICE $XINIT_HOME
  (cd $HOME; rm -f .xsession; ln -sf .xinitrc .xsession)

  TITLE="Window Manager & You"
  TEXT="\nYou chose $CHOICE.\n"`wm_personality $CHOICE`
  $WCMD --backtitle "$BACKTITLE" --title "$TITLE" --msgbox "$TEXT"  0 0

fi

clean_exit
}

########################
#
wizard menuA menuB

