#!/bin/bash

###############################################################################
# JWMWindowConfig v0.5 by SFR'2014                                            #
# GNU GPL v2 applies                                                          #
# Partially based on "title-clock" and "focusModel" scripts                   #
# from "jwmconfig2" package                                                   #
###############################################################################

export TEXTDOMAIN=puppy
export OUTPUT_CHARSET=UTF-8

export CONFIG_FILE="$HOME/.jwm/jwmrc-personal"
export CONFIG_FILE_BAK="$HOME/.jwm/jwmrc-personal2"

[ -e /usr/share/pixmaps/puppy/windows.svg ] && PIC_PATH=/usr/share/pixmaps/puppy/windows.svg || PIC_PATH=/usr/local/lib/X11/mini-icons/mini-window.xpm

#------------------------------------------------------------------------------

defTITLE_HEIGHT="`grep '<Height>' "$CONFIG_FILE" | cut -f2 -d '>' | cut -f1 -d '<' | tr -cd [0-9]`"
[ "$defTITLE_HEIGHT" = "" ] && defTITLE_HEIGHT=22

FOCUS_MODEL="`grep '<FocusModel>' "$CONFIG_FILE" | cut -f2 -d '>' | cut -f1 -d '<'`"
defFOCUS_CLICK=true; defFOCUS_SLOPPY=false
[ "$FOCUS_MODEL" = "sloppy" ] && { defFOCUS_SLOPPY=true; defFOCUS_CLICK=false; }

SNAP_MODE="`grep '<SnapMode.*>' "$CONFIG_FILE" | cut -f2 -d '>' | cut -f1 -d '<'`"
defSNAP_NONE=true; defSNAP_SCREEN=false; defSNAP_BORDER=false
[ "$SNAP_MODE" = "screen" ] && { defSNAP_NONE=false; defSNAP_SCREEN=true; }
[ "$SNAP_MODE" = "border" ] && { defSNAP_NONE=false; defSNAP_BORDER=true; }

MOVE_MODE="`grep '<MoveMode>' "$CONFIG_FILE" | cut -f2 -d '>' | cut -f1 -d '<'`"
defMOVE_OUTLINE=true; defMOVE_OPAQUE=false
[ "$MOVE_MODE" = "opaque" ] && { defMOVE_OUTLINE=false; defMOVE_OPAQUE=true; }

RESIZE_MODE="`grep '<ResizeMode>' "$CONFIG_FILE" | cut -f2 -d '>' | cut -f1 -d '<'`"
defRESIZE_OUTLINE=true; defRESIZE_OPAQUE=false
[ "$RESIZE_MODE" = "opaque" ] && { defRESIZE_OUTLINE=false; defRESIZE_OPAQUE=true; }

export FOCUS_MODEL SNAP_MODE MOVE_MODE RESIZE_MODE
export defTITLE_HEIGHT defFOCUS_CLICK defFOCUS_SLOPPY defSNAP_NONE defSNAP_SCREEN defSNAP_BORDER defMOVE_OUTLINE defMOVE_OPAQUE defRESIZE_OUTLINE defRESIZE_OPAQUE

#------------------------------------------------------------------------------
func_replace () {
  sed -i 's%'${VAL_OLD}'%'${VAL_NEW}'%' "$CONFIG_FILE"
}
export -f func_replace

#------------------------------------------------------------------------------

func_save_settings () {
  cp -af "$CONFIG_FILE" "$CONFIG_FILE_BAK"
  
  VAL_OLD="<Height>${defTITLE_HEIGHT}</Height>"
  VAL_NEW="<Height>${varTITLE_HEIGHT}</Height>"
  func_replace
  
  [ "$varFOCUS_SLOPPY" = "true" ] && VAL="sloppy" || VAL="click"
  VAL_OLD="<FocusModel>${FOCUS_MODEL}</FocusModel>"
  VAL_NEW="<FocusModel>${VAL}</FocusModel>"
  func_replace
  
  VAL=none
  [ "$varSNAP_SCREEN" = "true" ] && VAL="screen"
  [ "$varSNAP_BORDER" = "true" ] && VAL="border"
  VAL_OLD="${SNAP_MODE}</SnapMode>"
  VAL_NEW="${VAL}</SnapMode>"
  func_replace
  
  [ "$varMOVE_OUTLINE" = "true" ] && VAL=outline || VAL=opaque
  VAL_OLD="<MoveMode>${MOVE_MODE}</MoveMode>"
  VAL_NEW="<MoveMode>${VAL}</MoveMode>"
  func_replace

  [ "$varRESIZE_OUTLINE" = "true" ] && VAL=outline || VAL=opaque
  VAL_OLD="<ResizeMode>${RESIZE_MODE}</ResizeMode>"
  VAL_NEW="<ResizeMode>${VAL}</ResizeMode>"
  func_replace
  
  jwm -restart
}
export -f func_save_settings
#------------------------------------------------------------------------------

func_model_info () {
Xdialog -title "$(gettext "Info")" -msgbox "$(gettext 'SLOPPY FOCUS')
$(gettext 'The window that is under the mouse pointer is the')
$(gettext 'window that has the focus. The focus is changed')
$(gettext 'by pointing at another window, there is no need')
$(gettext 'to click in it as well.')

$(gettext 'CLICK TO FOCUS')
$(gettext 'The model used by Microsoft Windows.')
$(gettext 'The active window is selected by mouse click.')" 0 0
}
export -f func_model_info

#------------------------------------------------------------------------------

export GUI_MAIN='
<window title="'$(gettext "Window Settings")'" resizable="false" image-name="'${PIC_PATH}'">
  <vbox>
    
    <frame '$(gettext "Height of Title Bar")'>
      <hbox>
        <frame>
           <hbox width-request="180">
             <text space-fill="true" space-expand="true"><label>'$(gettext "Title Bar")'</label></text>
             <pixmap><input file>/usr/share/pixmaps/jwm_button_themes/Basix/min.svg</input><height>20</height></pixmap>
             <pixmap><input file>/usr/share/pixmaps/jwm_button_themes/Basix/max.svg</input><height>20</height></pixmap>
             <pixmap><input file>/usr/share/pixmaps/jwm_button_themes/Basix/close.svg</input><height>20</height></pixmap>
           </hbox>
        </frame>
        <spinbutton range-min="16" range-max="64" range-step="1">
          <variable>varTITLE_HEIGHT</variable>
          <default>'${defTITLE_HEIGHT}'</default>
        </spinbutton>
      </hbox>
    </frame>
    
    <frame '$(gettext "Window Focus Model")'>
      <hbox>
        <radiobutton>
          <variable>varFOCUS_CLICK</variable>
          <default>'${defFOCUS_CLICK}'</default>
          <label>'$(gettext "Click")'</label>
        </radiobutton>
        <radiobutton space-fill="false" space-expand="false">
          <variable>varFOCUS_SLOPPY</variable>
          <default>'${defFOCUS_SLOPPY}'</default>
          <label>'$(gettext "Sloppy")'</label>
        </radiobutton>
        <text space-fill="true" space-expand="true"><label>""</label></text>
        <button space-fill="false" space-expand="false">
          <input file stock="gtk-info"></input>
          <label>""</label>
          <action>func_model_info</action>
        </button>
      </hbox>
    </frame>
    
    <frame '$(gettext "Snap Mode")'>
      <hbox homogeneous="true">
        <radiobutton>
          <variable>varSNAP_NONE</variable>
          <default>'${defSNAP_NONE}'</default>
          <label>'$(gettext "None")'</label>
        </radiobutton>
        <radiobutton>
          <variable>varSNAP_SCREEN</variable>
          <default>'${defSNAP_SCREEN}'</default>
          <label>'$(gettext "Screen")'</label>
        </radiobutton>
        <radiobutton>
          <variable>varSNAP_BORDER</variable>
          <default>'${defSNAP_BORDER}'</default>
          <label>'$(gettext "Border")'</label>
        </radiobutton>
      </hbox>
    </frame>
    
    <frame '$(gettext "Move Mode")'>
      <hbox homogeneous="true">
        <radiobutton>
          <variable>varMOVE_OUTLINE</variable>
          <default>'${defMOVE_OUTLINE}'</default>
          <label>'$(gettext "Outline")'</label>
        </radiobutton>
        <radiobutton>
          <variable>varMOVE_OPAQUE</variable>
          <default>'${defMOVE_OPAQUE}'</default>
          <label>'$(gettext "Opaque")'</label>
        </radiobutton>
      </hbox>
    </frame>

    <frame '$(gettext "Resize Mode")'>
      <hbox homogeneous="true">
        <radiobutton>
          <variable>varRESIZE_OUTLINE</variable>
          <default>'${defRESIZE_OUTLINE}'</default>
          <label>'$(gettext "Outline")'</label>
        </radiobutton>
        <radiobutton>
          <variable>varRESIZE_OPAQUE</variable>
          <default>'${defRESIZE_OPAQUE}'</default>
          <label>'$(gettext "Opaque")'</label>
        </radiobutton>
      </hbox>
    </frame>
    
    <hbox>
      <button>
        <input file stock="gtk-apply"></input>
        <label>'$(gettext "Apply")'</label>
        <action>func_save_settings</action>
        <action>exit:EXIT</action>
      </button>
      <button>
        <input file stock="gtk-cancel"></input>
        <label>'$(gettext "Cancel")'</label>
        <action>exit:CANCEL</action>
      </button>
    </hbox>
  </vbox>
</window>'

#------------------------------------------------------------------------------

[ -e "$CONFIG_FILE" ] || { xmessage -center -bg red -title "$(gettext "Error")" "$(gettext "Configuration file doesn't exist! Aborting...")"; exit 1; }
gtkdialog -p GUI_MAIN
