#!/bin/sh

##----JWM Theme Switcher--->>
#
# author: thoughtjourney
# date:   06/07/2005
#
##------------------------->>

##---TO DO--->>
#
# restart jwm 
#
# restart moved to main config menu
# by Kwiller on 06-May-2006
#
##----------->>

##---------------------variables-------------------->>
THEMES=`cat /root/.jwm/themes/themeslist`
FIN="/root/.jwmrc"
FOUT="/root/.jwmrcNew"
INCTEST=0
RETVAL=$?
NOTHEMES=`grep -c jwm /root/.jwm/themes/themeslist`
TEST=""
FILETEST="empty"

#distinctive part of the personal jwm config filename (ie unique in .jwmrc). You may need to change the sed statement below if you change this variable
PERSONALCFG="-personal"    


##------------------------gui----------------------->>
Xdialog --backtitle "JWMテーマ" --title "JWMテーマの選択" --menu "次のテーマの中から、１つ選んで下さい。" 24 51 $NOTHEMES\
        $THEMES 2> /tmp/menu.tmp.$$

retval=$?
case $retval in
  1)
    exit 0;;
  255)
    exit 0;;
esac


##-----------parse results and prep to write ------->>
CHOICE=`cat /tmp/menu.tmp.$$`
BGSCRIPT="/root/.jwm/themes/bg-$CHOICE"

INCTEST=`grep -c "<Include>/root/.jwm/themes*" $FIN`
FULLINC="\<Include\>\/root\/\.jwm\/themes\/$CHOICE\<\/Include\>"
SUB="/$PERSONALCFG/i\ "
SUBINCLUDE=$SUB$FULLINC
SEDDEFAULT="\/jwm\/themes\/jwm.*/d"
SEDCHANGE=s!themes/jwm.*\</Inc!themes/${CHOICE}\<\/Inc!g

rm -f /tmp/menu.tmp.$$
cp $FIN $FIN~


#--------write include information to jwm config file-------->>

if [ "$CHOICE" = "" ];then
   Xdialog --title "Error" --infobox "There was an error with the selection.\nPlease try again." 0 0 20000 
   exit 1
fi

if [ "$CHOICE" = "jwm-default" ]; then
    #sed $SEDDEFAULT $FIN > $FOUT
    echo  > /root/.jwm/themes/jwm-default
fi
#else
   if [ $INCTEST -eq 0 ]; then
      #find a blank line and insert the <Include>
      sed -e "$SUBINCLUDE" $FIN > $FOUT
   else
      sed -e "$SEDCHANGE" $FIN > $FOUT
   fi

   ##---test config file ok--->>
   TEST=`grep "<Include>/root/.jwm/themes*" $FOUT`

   if [ "$TEST" = "" ]; then
     Xdialog --title "Error" --infobox "There was an error writing your choice\nto the config file.\nPlease try again." 0 0 20000 
     exit 1
   fi
#fi

FILETEST=`head $FOUT`

if [ "$FILETEST" = "" ]; then
   Xdialog --title "Error" --infobox "There was an error writing to the config file.\nPlease try again." 0 0 20000 
   exit 1
fi

mv $FOUT $FIN

#------------------change the background-------------------->>

exec $BGSCRIPT&










