#!/bin/bash
#JWM Corner Activation
#Copyright 2010,2011,2014,2015
#Sigmund Berglund (zigbert)

export TEXTDOMAIN=jwm
export OUTPUT_CHARSET=UTF-8

apply () {
	[ "$LT" ] && echo "$LT" > $HOME/.jwm/corners/lefttop || echo -n > $HOME/.jwm/corners/lefttop
	[ "$RT" ] && echo "$RT" > $HOME/.jwm/corners/righttop || echo -n > $HOME/.jwm/corners/righttop
	[ "$LB" ] && echo "$LB" > $HOME/.jwm/corners/leftbottom || echo -n > $HOME/.jwm/corners/leftbottom
	[ "$RB" ] && echo "$RB" > $HOME/.jwm/corners/rightbottom || echo -n > $HOME/.jwm/corners/rightbottom
	#build corner-rc files
	echo '<JWM>' > $HOME/.jwm/jwmrc-corners
	[ "$RT" ] && echo '<Tray autohide="top" layer="above" halign="right" valign="top" height="3" width="3" layout="horizontal" ><Swallow name="cornerrighttop">/usr/share/jwm/corners/righttop</Swallow></Tray>' >> $HOME/.jwm/jwmrc-corners
	[ "$LT" ] && echo '<Tray autohide="top" layer="above" halign="left" valign="top" height="3" width="3" layout="horizontal" ><Swallow name="cornerlefttop">/usr/share/jwm/corners/lefttop</Swallow></Tray>' >> $HOME/.jwm/jwmrc-corners
	[ "$RB" ] && echo '<Tray autohide="bottom" layer="above" halign="right" height="3" width="3" valign="bottom" layout="horizontal" ><Swallow name="cornerrightbottom">/usr/share/jwm/corners/rightbottom</Swallow></Tray>' >> $HOME/.jwm/jwmrc-corners
	[ "$LB" ] && echo '<Tray autohide="bottom" layer="above" halign="left" valign="bottom" height="3" width="3" layout="horizontal" ><Swallow name="cornerleftbottom">/usr/share/jwm/corners/leftbottom</Swallow></Tray>' >> $HOME/.jwm/jwmrc-corners
	echo '</JWM>' >> $HOME/.jwm/jwmrc-corners
	#---
	/usr/local/jwm_config/jwm_restart
}
export -f apply

#first run - install
if [ ! -d $HOME/.jwm/corners ]; then
	mkdir -p $HOME/.jwm/corners
	#rebuild jwm-template to include corner-rc files
	TMP="`grep -B 10000 -F '</JWM>' /etc/xdg/templates/_root_.jwmrc | head -n -1 | grep -v -F 'jwmrc-corners'`"
	echo "$TMP" > /etc/xdg/templates/_root_.jwmrc
	echo -e " <Include>/root/.jwm/jwmrc-corners</Include>\n\n</JWM>" >> /etc/xdg/templates/_root_.jwmrc
	#define commands
	echo -n > $HOME/.jwm/corners/righttop
	echo -n > $HOME/.jwm/corners/leftbottom
	echo -n > $HOME/.jwm/corners/rightbottom
	[ -f $(which pnote) ] && echo 'pnote #FFF4AF #222222' > $HOME/.jwm/corners/lefttop || echo -n > $HOME/.jwm/corners/lefttop
	chmod 722 $HOME/.jwm/corners/*
fi

export JWM_corner='
<window title="'$(gettext 'JWM Corner Activation')'" icon-name="gtk-preferences">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info scale placement_corners.svg 60 "$(gettext 'Configure what happens when you move the mouse-pointer into one of the desktop corners. Edit the command for the corners you want to activate.')"`"'
  <frame '$(gettext 'Corner-commands')'>
    <vbox space-fill="false" space-expand="false">
      <hbox space-fill="true" space-expand="true">
        '"`/usr/lib/gtkdialog/xml_pixmap placement_topleft.svg icon`"'
        <vbox space-fill="true" space-expand="true">
          <entry space-fill="true" space-expand="true"><variable>LT</variable><input file>'$HOME'/.jwm/corners/lefttop</input></entry>
          <text space-fill="true" space-expand="true"><label>""</label></text>
        </vbox>
        <text width-request="80" space-fill="false" space-expand="false"><label>""</label></text>
        <vbox space-fill="true" space-expand="true">
          <entry space-fill="true" space-expand="true"><variable>RT</variable><input file>'$HOME'/.jwm/corners/righttop</input></entry>
          <text space-fill="true" space-expand="true"><label>""</label></text>
        </vbox>
        '"`/usr/lib/gtkdialog/xml_pixmap placement_topright.svg icon`"'
      </hbox>
    </vbox>
    <text height-request="30" space-fill="true" space-expand="true"><label>""</label></text>
    <vbox space-fill="false" space-expand="false">
      <hbox space-fill="true" space-expand="true">
        '"`/usr/lib/gtkdialog/xml_pixmap placement_bottomleft.svg icon`"'
        <vbox space-fill="true" space-expand="true">
          <text space-fill="true" space-expand="true"><label>""</label></text>
          <entry space-fill="true" space-expand="true"><variable>LB</variable><input file>'$HOME'/.jwm/corners/leftbottom</input></entry>
        </vbox>
        <text width-request="80" space-fill="false" space-expand="false"><label>""</label></text>
        <vbox space-fill="true" space-expand="true">
          <text space-fill="true" space-expand="true"><label>""</label></text>
          <entry space-fill="true" space-expand="true"><variable>RB</variable><input file>'$HOME'/.jwm/corners/rightbottom</input></entry>
        </vbox>
        '"`/usr/lib/gtkdialog/xml_pixmap placement_bottomright.svg icon`"'
      </hbox>
    </vbox>
  </frame>
  <hbox space-expand="false" space-fill="false">
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <label>'$(gettext 'Cancel')'</label>
      <action>exit:EXIT</action>
    </button>
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon apply`"'
      <label>'$(gettext 'Apply')'</label>
      <action>apply</action>
    </button>
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <label>'$(gettext 'Ok')'</label>
      <action>apply</action>
      <action>exit:EXIT</action>
    </button>
    '"`/usr/lib/gtkdialog/xml_scalegrip`"'
  </hbox>
</vbox>
</window>
'
. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
gtkdialog -p JWM_corner
