#!/bin/bash

# gui to customise JWM window buttons, again from the mind of technosaurus
# new version for pixmap icons 140118, adjusted 140121

# switch function
switchfunc()
{
	grep -q '<ButtonClose>' $HOME/.jwm/jwmrc-personal
	if [ $? -eq 1 ];then
	sed -i '/^<\/JWM>/d' $HOME/.jwm/jwmrc-personal #delete
	echo '<ButtonClose>/usr/share/pixmaps/close.png</ButtonClose>
<ButtonMax>/usr/share/pixmaps/max.png</ButtonMax>
<ButtonMaxActive>/usr/share/pixmaps/maxact.png</ButtonMaxActive>
<ButtonMin>/usr/share/pixmaps/min.png</ButtonMin>
</JWM>' >> $HOME/.jwm/jwmrc-personal
	cp -af $HOME/.jwm/jwmrc-personal $HOME/.jwm/jwmrc-personal2
	fi
	Dir=/usr/share/pixmaps/jwm_button_themes/$1
	for icon in $Dir/*
	  do ifile=$(basename $icon)
	  ext=${ifile##*.}
	  oldext=$(grep '<Button' /root/.jwm/jwmrc-personal | head -n 1 | cut -f 2 -d '.' | cut -f 1 -d '<' )
	  [ "${ext}" != "${oldext}" ] &&  sed -i "s%${oldext}%${ext}%g" /root/.jwm/jwmrc-personal 
	  ln -sf ${icon} /usr/share/pixmaps/$(basename ${icon})
	  done
	jwm -restart
	rm /tmp/jwm_button_themes.xml 2>/dev/null
}
clear_func()
{
	grep -vE '<Button|</JWM' $HOME/.jwm/jwmrc-personal > /tmp/jwmrc-personal-btns
	while read l;do echo $l >> /tmp/jwmrc-personal-btns2; done < /tmp/jwmrc-personal-btns 
	echo '</JWM>' >> /tmp/jwmrc-personal-btns2
	cat /tmp/jwmrc-personal-btns2 > $HOME/.jwm/jwmrc-personal
	cp -af $HOME/.jwm/jwmrc-personal $HOME/.jwm/jwmrc-personal2
	#fixmenus
	jwm -restart
	rm /tmp/jwmrc-personal-btns* 2>/dev/null
}

rm /tmp/jwm_button_themes.xml 2>/dev/null #precaution
rm /tmp/jwmrc-personal-btns* 2>/dev/null

# build gui in /tmp
echo "<window title=\"jwm button themes\" resizable=\"false\">
   <vbox height-request=\"330\">
    <text><label>You can theme the window title bar to nearly any thing you like.</label></text>
    <text><label>Choose a button theme</label></text>
    <frame Themes>
     <vbox scrollable=\"true\">
" > /tmp/jwm_button_themes.xml

for theme in /usr/share/pixmaps/jwm_button_themes/*
  do 
     #PIC=$(ls ${theme}|grep png$)
     PIC="<frame>
            <hbox>
             <pixmap><input file>${theme}/min.svg</input><height>24</height></pixmap>
             <pixmap><input file>${theme}/max.svg</input><height>24</height></pixmap>
             <pixmap><input file>${theme}/close.svg</input><height>24</height></pixmap>
            </hbox>
          </frame>"
     label=$(basename $theme)
     echo "      <hbox homogeneous=\"true\">
        $PIC
        <text><label>$label</label></text>
        <button use-stock=\"true\" label=\"gtk-apply\">
          <action>exit:$label</action>
        </button>
      </hbox>" >> /tmp/jwm_button_themes.xml
  done
echo "</vbox>   
  </frame>
    <hbox> 
      <button use-stock=\"true\" label=\"gtk-clear\" tooltip-text=\"Reinstates the stock standard icons\">
        <action>exit:clear</action>
        </button>
      <button cancel></button>
    </hbox>
  </vbox>
</window>
" >> /tmp/jwm_button_themes.xml  

eval $(gtkdialog -f /tmp/jwm_button_themes.xml)
case $EXIT in
Cancel|abort)exit ;;
clear)clear_func ;;
*)switchfunc $EXIT ;;
esac
