#!/bin/sh

####################################
#
# A JWM panel button configuring tool
# for jwmconfig
# 2006-NOV-05
# by plinej
#
#####################################

if [ "`ls /root/.jwmrc-tray-bak`" = "" ]; then
cp /root/.jwmrc-tray /root/.jwmrc-tray-bak  ### makes .jwmrc-tray-bak backup file unless it already exists
fi
cat /root/.jwmrc-tray | sed '/^$/d' > /tmp/.jwmrc-tray  ### remove all blank lines in .jwmrc-tray
mv /tmp/.jwmrc-tray /root/

total=`wc -l /root/.jwmrc-tray | sed -e 's/ *//' | sed -e 's/ .*//g'` ### get total number of lines
low=`grep '<TrayButton label="Menu"'  -n /root/.jwmrc-tray | sed -e 's/\:.*>//g'`  ### get line number with the Menu button
high=`grep '<!-- Additional Pager attributes; width, height -->' -n /root/.jwmrc-tray | sed -e 's/\:.*>//g'` ### get line number with Additional Pager
last=`echo "(("$total" - "$high" + 2))" | bc -l` 
begin=`echo "(("$low" + 1))" | bc -l` ### first line with panel buttons
#end=`echo "(("$high" - 1))" | bc -l` ### last line with panel buttons

cat /root/.jwmrc-tray | head -n "$low" > /tmp/jwmrc-tray-1.txt
cat /root/.jwmrc-tray | tail -n "$last" > /tmp/jwmrc-tray-3.txt

num=1

### xdialog radiolist gui
Xdialog --backtitle "" \
	--title "ラジオリストボックス" \
        --radiolist "JWMパネルボタン設定ツール" 26 46 5 \
        "Edit" "現在のパネルボタンを編集" ON \
        "Add" "パネルボタンを追加" off \
        "Move" "パネルボタンを移動" off \
	"Remove" "パネルボタンを削除" off\
        "Restore" "元の .jwmrc-trayに戻す" off \
        "Backup" "現在の .jwmrc-trayをバックアップ" off \
	"Exit" "このプログラムを終了" off 2>/tmp/checklist.tmp.$$

retval=$?
choice=`cat /tmp/checklist.tmp.$$`
rm -f /tmp/checklist.tmp.$$

case $retval in
  0)
    echo "'$choice' chosen.";;
  1)
    echo "Cancel pressed.";;
  255)
    echo "Box closed.";;
esac

if [ "$retval" != 0 ]; then
exit 0
fi

if [ "$choice" = Backup ]; then

Xdialog --title "メッセージボックス" \
        --msgbox "これは現在の.jwmrc-trayをバックアップして
	      リストアを選択すれば
	     リストアしたバージョンになります。" 10 41

case $? in
  0)
    echo "OK";;
  255)
    echo "Box closed.";;
esac

cp /root/.jwmrc-tray /root/.jwmrc-tray-bak
exit 0
fi

if [ "$choice" = Exit ]; then
exit 0
fi

if [ "$choice" = Edit ]; then

while [ "$begin" != "$high" ]; do

cat /root/.jwmrc-tray | sed -n ""$begin"p" | sed -e 's/<TrayButton popup="//' | sed -e 's/\".*//' | sed 's/^[ \t]*//' > /tmp/tmp-popup.txt  ### gets popup text
popup=`cat /tmp/tmp-popup.txt`
cat /root/.jwmrc-tray | sed -n ""$begin"p" | sed -e 's/<TrayButton popup="//' | sed -e "s/$popup//" | sed -e 's/" icon="//' | sed -e 's/\".*//' | sed 's/^[ \t]*//' > /tmp/tmp-icon.txt  ### gets icon text
icon=`cat /tmp/tmp-icon.txt`
cat /root/.jwmrc-tray | sed -n ""$begin"p" | sed 's/\<.*\"//g' | sed -e 's/<\/TrayButton>//' | sed -e 's/<>//' | sed 's/^[ \t]*//' > /tmp/tmp-exec.txt  ### gets executable text
#action=`cat /tmp/tmp-exec.txt`

### This is the Edit gui
export JWM_Panel_Button_Editor="
<vbox>
 <frame ボタン $num の情報 - 好きに編集して下さい>
  <hbox>
      <text><label>ポップアップディスプレイ</label></text>
      <entry>
    <variable>Entry1</variable>
    <input>cat /tmp/tmp-popup.txt</input>
      </entry>
  </hbox>
  <hbox>
      <text><label>実行ファイル</label></text>
      <entry>
    <variable>Entry3</variable>
    <input>cat /tmp/tmp-exec.txt</input>
      </entry>
  </hbox>
  <hbox>
      <text><label>アイコン名</label></text>
      <entry>
    <variable>Entry2</variable>
    <input>cat /tmp/tmp-icon.txt</input>
    </entry>
    <button>
      <input file>/usr/local/lib/X11/mini-icons/dir_o.xpm</input>
      <action>./file-selector</action>
      <action>refresh:Entry2</action>
    </button>
  </hbox>
 </frame>
  <hbox>
   <button ok>
	<action>echo \$Entry1 | tee /tmp/tmp-popup-$num.txt</action>
	<action>echo \$Entry2 | tee /tmp/tmp-icon-$num.txt</action>
	<action>echo \$Entry3 | tee /tmp/tmp-exec-$num.txt</action>
	<action type=\"exit\">EXIT</action>
   </button>
  </hbox>
</vbox>"

##------------run------------->>
gtkdialog3 --program JWM_Panel_Button_Editor

##----------cleanup------------>>
unset JWM_Panel_Button_Editor

#clear

new_line=`echo \<TrayButton popup\=\"``cat /tmp/tmp-popup-$num.txt``echo \" icon\=\"``cat /tmp/tmp-icon-$num.txt``echo \"\>``cat /tmp/tmp-exec-$num.txt``echo \</TrayButton\>`  ### builds replacement line for .jwmrc-tray
echo "$new_line" >> /tmp/jwmrc-tray-2.txt

begin=`echo "(("$begin" + 1))" | bc -l`
num=`echo "(("$num" + 1))" | bc -l`


shift
done

cat /tmp/jwmrc-tray-2.txt >> /tmp/jwmrc-tray-1.txt
cat /tmp/jwmrc-tray-3.txt >> /tmp/jwmrc-tray-1.txt
mv /tmp/jwmrc-tray-1.txt /root/.jwmrc-tray

rm -f /tmp/jwmrc-tray-*
rm -f /tmp/tmp-popup*.txt
rm -f /tmp/tmp-icon*.txt
rm -r /tmp/tmp-exec*.txt

Xdialog --title "メッセージボックス" \
        --msgbox "編集が完了しました。
	     変更を反映するにはJWMをリスタート
	     しなければなりません。" 10 41

case $? in
  0)
    echo "OK";;
  255)
    echo "Box closed.";;
esac

fi

if [ "$choice" = Restore ]; then

mv /root/.jwmrc-tray-bak /root/.jwmrc-tray

Xdialog --title "メッセージボックス" \
        --msgbox "元の .jwmrc-trayファイルは
        リストアされました。
	     変更を反映するにはJWMをリスタート
	     しなければなりません。" 10 41

case $? in
  0)
    echo "OK";;
  255)
    echo "Box closed.";;
esac
fi

if [ "$choice" = Add ]; then
while [ "$begin" != "$high" ]; do
cat /root/.jwmrc-tray | sed -n ""$begin"p" >> /tmp/jwmrc-tray-panel.txt
begin=`echo "(("$begin" + 1))" | bc -l`
shift
done

### This is the Add gui
export JWM_Panel_Button_Add="
<vbox>
 <frame ボタンを追加>
  <hbox>
      <text><label>ポップアップディスプレイ</label></text>
      <entry>
    <variable>Entry1</variable>
      </entry>
  </hbox>
  <hbox>
      <text><label>実行ファイル</label></text>
      <entry>
    <variable>Entry3</variable>
    <input>echo exec:</input>
      </entry>
  </hbox>
  <hbox>
      <text><label>アイコンの選択</label></text>
      <entry>
    <variable>Entry2</variable>
    <input>cat /tmp/tmp-icon.txt</input>
    </entry>
    <button>
      <input file>/usr/local/lib/X11/mini-icons/dir_o.xpm</input>
      <action>./file-selector</action>
      <action>refresh:Entry2</action>
    </button>
  </hbox>
  </frame>
  <hbox>
   <button ok>
	<action>echo \$Entry1 | tee /tmp/tmp-popup.txt</action>
	<action>echo \$Entry3 | tee /tmp/tmp-exec.txt</action>
	<action type=\"exit\">EXIT</action>
   </button>
  </hbox>
</vbox>"

##------------run------------->>
gtkdialog3 --program JWM_Panel_Button_Add

##----------cleanup------------>>
unset JWM_Panel_Button_Add

new_line=`echo \<TrayButton popup\=\"``cat /tmp/tmp-popup.txt``echo \" icon\=\"``cat /tmp/tmp-icon.txt``echo \"\>``cat /tmp/tmp-exec.txt``echo \</TrayButton\>`  ### builds new line for .jwmrc-tray
echo "$new_line" >> /tmp/jwmrc-tray-panel.txt

cat /tmp/jwmrc-tray-panel.txt >> /tmp/jwmrc-tray-1.txt
cat /tmp/jwmrc-tray-3.txt >> /tmp/jwmrc-tray-1.txt
mv /tmp/jwmrc-tray-1.txt /root/.jwmrc-tray

Xdialog --title "メッセージボックス" \
        --msgbox "追加が完了しました。
	     変更を反映するにはJWMをリスタート
	     しなければなりません。" 10 41

case $? in
  0)
    echo "OK";;
  255)
    echo "Box closed.";;
esac

rm -f /tmp/jwmrc-tray-*
rm -f /tmp/tmp-popup.txt
rm -f /tmp/tmp-icon.txt
rm -r /tmp/tmp-exec.txt

fi




if [ "$choice" = Remove ]; then

y=1
z=`echo "(("$high" - "$begin"))" | bc -l`
while [ "$begin" != "$high" ]; do

Xdialog --backtitle "パネルボタンを削除" \
	--title "ラジオリストボックス" \
        --radiolist "パネルから `cat /root/.jwmrc-tray | sed -n ""$begin"p" | sed -e 's/<TrayButton popup="//g' | sed -e 's/\".*//g' | sed 's/^[ \t]*//'`\n\
を削除しますか？削除するにはyesを\n\
そうでなければnoを選択して下さい。" 26 46 5 \
        "no"    "noはこのボタンを削除しません" ON \
        "yes" "yesはこのボタンを削除します" off 2>/tmp/checklist.tmp.$$

retval=$?
choice=`cat /tmp/checklist.tmp.$$`
rm -f /tmp/checklist.tmp.$$

case $retval in
  0)
    echo "'$choice' chosen.";;
  1)
    echo "Cancel pressed.";;
  255)
    echo "Box closed.";;
esac

if [ "$choice" = no ]; then
cat /root/.jwmrc-tray | sed -n ""$begin"p" >> /tmp/jwmrc-tray-1.txt
fi
begin=`echo "(("$begin" + 1))" | bc -l`
y=`echo "(("$y" + 1))" | bc -l`

shift
done

cat /tmp/jwmrc-tray-3.txt >> /tmp/jwmrc-tray-1.txt
mv /tmp/jwmrc-tray-1.txt /root/.jwmrc-tray

Xdialog --title "メッセージボックス" \
        --msgbox "削除が完了しました。
	     変更を反映するにはJWMをリスタート
	     しなければなりません。" 10 41

case $? in
  0)
    echo "OK";;
  255)
    echo "Box closed.";;
esac

fi

if [ "$choice" = Move ]; then
y=1
z=`echo "(("$high" - "$begin"))" | bc -l`
while [ "$begin" != "$high" ]; do
cat /root/.jwmrc-tray | sed -n ""$begin"p" >> /tmp/jwmrc-tray-panel-"$y".txt

Xdialog --title "入力ボックス" \
        --inputbox "`cat /root/.jwmrc-tray | sed -n ""$begin"p" | sed -e 's/<TrayButton popup="//g' | sed -e 's/\".*//g' | sed 's/^[ \t]*//'` is button $y of your $z buttons.\n\
\n\
このボタンを他の位置に移動したければ\n\
次の番号を入力して下さい。" 18 45 2> /tmp/inputbox.tmp.$$

retval=$?
input=`cat /tmp/inputbox.tmp.$$`
rm -f /tmp/inputbox.tmp.$$

case $retval in
  0)
    echo "Input string is '$input'";;
  1)
    echo "Cancel pressed.";;
  255)
    echo "Box closed.";;
esac

if [ "$retval" != 0 ]; then
exit 0
fi

if [ "$input" != "" ]; then

if [ "`ls /tmp/jwmrc-tray-tmp-"$input".txt`" = /tmp/jwmrc-tray-tmp-"$input".txt ]; then

Xdialog --title "メッセージボックス" \
        --msgbox "位置 "$input"
	     はすでに選択されています。
	     やり直して下さい。" 10 41

case $? in
  0)
    echo "OK";;
  255)
    echo "Box closed.";;
esac
rm -f /tmp/jwmrc-tray*
exit 0

fi

mv /tmp/jwmrc-tray-panel-"$y".txt /tmp/jwmrc-tray-tmp-"$input".txt

else

if [ "`ls /tmp/jwmrc-tray-tmp-"$y".txt`" = /tmp/jwmrc-tray-tmp-"$y".txt ]; then

Xdialog --title "メッセージボックス" \
        --msgbox "位置 "$y"
	     はすでに選択されています。
	     やり直して下さい。" 10 41

case $? in
  0)
    echo "OK";;
  255)
    echo "Box closed.";;
esac
rm -f /tmp/jwmrc-tray*
exit 0

fi

mv /tmp/jwmrc-tray-panel-"$y".txt /tmp/jwmrc-tray-tmp-"$y".txt

fi

begin=`echo "(("$begin" + 1))" | bc -l`
y=`echo "(("$y" + 1))" | bc -l`
shift
done

cat /tmp/jwmrc-tray-tmp-* >> /tmp/jwmrc-tray-1.txt
cat /tmp/jwmrc-tray-3.txt >> /tmp/jwmrc-tray-1.txt
mv /tmp/jwmrc-tray-1.txt /root/.jwmrc-tray

Xdialog --title "メッセージボックス" \
        --msgbox "移動が完了しました。
	     変更を反映するにはJWMをリスタート
	     しなければなりません。" 10 41

case $? in
  0)
    echo "OK";;
  255)
    echo "Box closed.";;
esac

rm -f /tmp/jwmrc-tray*

fi

rm -f /tmp/jwmrc-tray*

exit 0