#!/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=jwm
export OUTPUT_CHARSET=UTF-8

[ ! "`ps -A | grep jwm`" ] && /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.')"

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

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

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
  
  /usr/local/jwm_config/jwm_restart
}
export -f func_save_settings
#------------------------------------------------------------------------------

export GUI_MAIN='
<window title="'$(gettext "Window Settings")'" image-name="/usr/share/pixmaps/puppy/windows.svg">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info scale windows.svg 60 "$(gettext '<b>Window settings</b>')" "$(gettext 'You are currently running the JWM window manager')"`"'        

  <frame '$(gettext "Window Settings")'>
    <hbox tooltip-text="'$(gettext 'Heigth of title bar in pixels')'" space-expand="true" space-fill="true">
      <text use-markup="true" space-expand="false" space-fill="false"><label>"<b>'$(gettext "Window title height")'</b>"</label></text>
      <text space-expand="true" space-fill="true"><label>""</label></text>
      <spinbutton range-min="16" range-max="64" range-step="1" space-expand="false" space-fill="false">
        <variable>varTITLE_HEIGHT</variable>
        <default>'${defTITLE_HEIGHT}'</default>
      </spinbutton>
    </hbox>

    <text space-expand="true" space-fill="true"><label>""</label></text>
    <hseparator></hseparator>

    <hbox tooltip-text="'$(gettext 'SLOPPY FOCUS
The window that is under the mouse pointer is the window that has the focus. The focus is changed by pointing at another window, there is no need to click in it as well.

CLICK TO FOCUS
The model used by Microsoft Windows. The active window is selected by mouse click.')'">
      <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"<b>'$(gettext "Focus")'</b>"</label></text>
      <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>
    </hbox>
      
    <text space-expand="true" space-fill="true"><label>""</label></text>
      
    <hbox>
      <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"<b>'$(gettext "Snap")'</b>"</label></text>
      <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>

    <text space-expand="true" space-fill="true"><label>""</label></text>

    <vbox tooltip-text="'$(gettext 'How should the window be shown when moving/resizing')'">
      <hbox>
        <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"<b>'$(gettext "Move")'</b>"</label></text>
        <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>
      <hbox>
        <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"<b>'$(gettext "Resize")'</b>"</label></text>
        <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>
    </vbox>
  </frame>
    
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon windows_theme.svg`"'
      <action>./theme_switcher &</action>
      <label>" '$(gettext 'Borders theme')' "</label>
    </button>
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon windows_buttons.svg`"'
      <action>./window_buttons &</action>
      <label>" '$(gettext 'Buttons theme')' "</label>
    </button>
    <text space-expand="true" space-fill="true"><label>"   "</label></text>
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <label>'$(gettext "Cancel")'</label>
      <action>exit:CANCEL</action>
    </button>
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon apply`"'
      <label>'$(gettext "Apply")'</label>
      <action>func_save_settings</action>
      <action>exit:EXIT</action>
    </button>
  </hbox>
</vbox>
</window>'

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

[ -e "$CONFIG_FILE" ] || { xmessage -center -bg red -title "$(gettext "Error")" "$(gettext "Configuration file doesn't exist! Aborting...")"; exit 1; }
. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
gtkdialog -p GUI_MAIN
