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

export TEXTDOMAIN=jwm
export OUTPUT_CHARSET=UTF-8

if [ ! "`ps -A | grep jwm`" ] && [ "`grep -F 'xwin' <<< $(ps -eo pid,command)`" ]; then
	/usr/lib/gtkdialog/box_ok JWM info "$(gettext 'You are currently NOT running the JWM window manager, and settings here will not work until JWM is active.')"
fi

# 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>$HOME/.jwm/window_buttons/close.png</ButtonClose>
<ButtonMax>$HOME/.jwm/window_buttons/max.png</ButtonMax>
<ButtonMaxActive>$HOME/.jwm/window_buttons/maxact.png</ButtonMaxActive>
<ButtonMin>$HOME/.jwm/window_buttons/min.png</ButtonMin>
</JWM>" >> $HOME/.jwm/jwmrc-personal
	fi
	[ ! -d $HOME/.jwm/window_buttons ] && mkdir $HOME/.jwm/window_buttons
	Dir=/usr/share/jwm/themes_window_buttons/$1
	rm $HOME/.jwm/window_buttons/*
	for icon in $Dir/*; do
		ifile=$(basename $icon)
		ext=${ifile##*.}
		newicon=`echo $ifile|sed "s%${ext}$%png%"`
		#JWM does a crappy svg convert, so we help out if rsvg-convert is installed
		if [ "`which rsvg-convert`" ]; then
			rsvg-convert -w 48 -h 48 -o $HOME/.jwm/window_buttons/${newicon} ${icon}
		else
			ln -sf ${icon} $HOME/.jwm/window_buttons/${newicon}
		fi
	done
	[ "$UPDATE" = "true" ] && /usr/local/jwm_config/jwm_restart
}

clear_func (){
	grep -vE '<Button|</JWM' $HOME/.jwm/jwmrc-personal > $HOME/.jwm/tmp/jwmrc-personal-btns
	while read l;do echo $l >> $HOME/.jwm/tmp/jwmrc-personal-btns2; done < $HOME/.jwm/tmp/jwmrc-personal-btns 
	echo '</JWM>' >> $HOME/.jwm/tmp/jwmrc-personal-btns2
	cat $HOME/.jwm/tmp/jwmrc-personal-btns2 > $HOME/.jwm/jwmrc-personal
	/usr/local/jwm_config/jwm_restart
	rm $HOME/.jwm/tmp/jwmrc-personal-btns* 2>/dev/null
}

export -f switchfunc clear_func

#parameters
while [ $# != 0 ]; do
	I=1
	while [ $I -le `echo $# | wc -c` ]; do 
		case $1 in
			-a) NEWTHEME="$2"; shift;;
			-h|--help)
echo 'Usage: window_buttons [OPTION]

Options
  -a THEME    Activate theme
  -h          Show this help message'; exit;;
		esac
		shift
		I=$[$I+1]
	done
done

rm $HOME/.jwm/tmp/window_button_themes.xml 2>/dev/null #precaution
rm /tmp/jwmrc-personal-btns* 2>/dev/null

if [ "$NEWTHEME" ]; then switchfunc "$NEWTHEME"; exit; fi

# build gui in /tmp
echo '
<window title="Window buttons theme" height-request="400" icon-name="gtk-convert">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info fixed "windows_buttons.svg" 60 "$(gettext 'The window buttons can be themed to your liking.')" "$(gettext 'If you want to build your own buttons, see') /usr/share/jwm/themes_window_buttons/"`"'
  <vbox space-expand="true" space-fill="true">
    <vbox margin="20" spacing="10" scrollable="true">' > $HOME/.jwm/tmp/window_button_themes.xml

for theme in /usr/share/jwm/themes_window_buttons/*; do 
echo '
      <vbox space-expand="false" space-fill="false">
        <hbox space-expand="true" space-fill="true">
          <hbox scrollable="true" shadow-type="2" height="36" height-request="10" margin="5" spacing="8" space-expand="false" space-fill="false">
            <pixmap space-expand="false" space-fill="false"><input file>'${theme}'/min.svg</input><height>24</height></pixmap>
            <pixmap space-expand="false" space-fill="false"><input file>'${theme}'/max.svg</input><height>24</height></pixmap>
            <pixmap space-expand="false" space-fill="false"><input file>'${theme}'/close.svg</input><height>24</height></pixmap>
          </hbox>
          <text width-request="40" space-expand="false" space-fill="false"><label>""</label></text>
          <button space-expand="true" space-fill="true">
            <label>'"$(basename $theme)"'</label>
            <action>UPDATE=true; switchfunc '"$(basename $theme)"'</action>
          </button>
        </hbox>
      </vbox>' >> $HOME/.jwm/tmp/window_button_themes.xml
done
echo '
    </vbox>
  </vbox>   
  <hbox space-expand="false" space-fill="false">' >> $HOME/.jwm/tmp/window_button_themes.xml
    [ "`which ptheme`" ] && echo '
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon puppy_theme.svg`"'
      <label>" '$(gettext 'Global theming')' "</label>
      <action>/usr/sbin/ptheme</action>
    </button>' >> $HOME/.jwm/tmp/window_button_themes.xml
    echo '
    <text space-expand="true" space-fill="true"><label>""</label></text>
    <button space-expand="false" space-fill="false" tooltip-text="Reinstates the stock standard icons">
      <label>'$(gettext "Clear")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon clear`"'
      <action>clear_func</action>
    </button>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Quit")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
      <action>exit:quit</action>
    </button>
  </hbox>
</vbox>
</window>' >> $HOME/.jwm/tmp/window_button_themes.xml  

. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
eval $(gtkdialog -f $HOME/.jwm/tmp/window_button_themes.xml)
