#!/bin/sh
# @vasm: XMENUSET
# @level: root
# @description: setup the selection items for XwmMenu
# 
# (c) Kocil, 2003
# (c) Vector Linux, 2003

dbug() {
  echo $*
}

# must be root !
if [ ! "$UID" = "0" ]; then
$DCMD --backtitle "Vector Linux Software Ltd." \
--title " SORRY " --msgbox \
"\nThis is a root only configurator. Please login as root, use su, or sudo" 0 0
  exit
fi


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

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

WMMENU_DIR=/etc/XwmMenu
WMMENU_INI=/etc/XwmMenu.ini

echo '$DCMD --backtitle "$BACKTITLE" --title "XwmMenu ITEM SELECTION" --checklist \
"\nThis configurator setup your XwmMenu menu items.
Please select the items that you want to be included." 24 70 10 \' > $fmenu

ls $WMMENU_DIR/*.wm | while read LINE; do
   FNAME=`basename $LINE`
   ENTRY=`grep Description $LINE | cut -d = -f 2` 
   echo "\"$FNAME\" \"$ENTRY\" on \\" >> $fmenu
done

ls $WMMENU_DIR/*.def | while read LINE; do
   FNAME=`basename $LINE`
   ENTRY=`grep Description $LINE | cut -d = -f 2` 
   echo "\"$FNAME\" \"$ENTRY\" on \\" >> $fmenu
done

echo " 2> $freply " >> $fmenu

. $fmenu

if [ $? -gt 0 ]; then
   exit 1
fi

if [ "$CMD" = "" ]; then
  choice=`cat $freply | sed -e 's/"/ /g'`
else
  choice=`cat $freply | sed -e 's!/! !g'`
fi

rm $fmenu
rm $freply


cat $WMMENU_DIR/XwmMenu.skel > $WMMENU_INI

entry=0
for script in $choice; do
    lab=`grep Label $WMMENU_DIR/$script`
    exe=`grep Execute $WMMENU_DIR/$script`
    desc=`grep Description $WMMENU_DIR/$script`
    # echo $script $lab $exe $desc 
    echo "Entry $entry $lab" >> $WMMENU_INI
    echo "Entry $entry $exe" >> $WMMENU_INI
    echo "Entry $entry $desc" >> $WMMENU_INI
    echo >> $WMMENU_INI
    let entry=entry+1
done

# Fixed options
$DCMD --backtitle "$BACKTITLE" --title "SET EXIT" --yesno \
"\nDo you want XwmMenu to exit after launching the Window Manager ?
Yes is the safest choice. If you select No, make sure only 
ONE X-server runs in this machine. You have been warned" 0 0

if [ $? = 0 ]; then
  cat $WMMENU_INI | sed -e 's/^Do Not Exit =.*/Do Not Exit = NO/' > $WMMENU_INI
else
  cat $WMMENU_INI | sed -e 's/^Do Not Exit =.*/Do Not Exit = YES/' > $WMMENU_INI
fi

$DCMD --backtitle "$BACKTITLE" --title " DONE " --msgbox \
"\nYour XwmMenu item has been configured.
It will be activated on the next X session" 0 0

