#!/bin/bash

export TEXTDOMAIN=jwmdesk

APPDIR=/usr/local/jwmdesk
ICONDIR=$APPDIR/icons
BGDIR=/usr/share/backgrounds
export APPDIR ICONDIR BGDIR

PIXMAPSDIR=/usr/share/pixmaps
[ ! $PIXMAPSDIR/gnome-dark.svg && ln -sf $APPDIR/jwm_menubutton_icons/gnome-dark.svg $PIXMAPSDIR
[ ! $PIXMAPSDIR/gnome-light.svg && ln -sf $APPDIR/jwm_menubutton_icons/gnome-light.svg $PIXMAPSDIR

[ "`which gtkdialog4 2>/dev/null`" ] && GTKDIALOG=gtkdialog4 || GTKDIALOG=gtkdialog
export GTKDIALOG

[ ! -f /usr/share/icons/hicolor/48x48/apps/global_themes.png ] && \
ln -sf $ICONDIR/global_themes.png /usr/share/icons/hicolor/48x48/apps && gtk-update-icon-cache -f -i /usr/share/icons/hicolor 2>/dev/null

########################################################################
#                                                                      #
# HEADER CONFIGURATION                                                 #
#                                                                      #
########################################################################

# define GUI header
XML_INFO_COLOR='#EDEBD7' # background color
XML_INFO_OPACITY=0.5 # background opacity
. $APPDIR/xml_info_jwmdesk gtk > /dev/null # build bg_pixmap for gtk-theme

BOX_HEIGHT=90
ICON=$ICONDIR/global_themes.png
ICON_HEIGHT=64
MSG_1="<b><span size='"'x-large'"'>$(gettext "Global Desktop Themes")</span></b>"
MSG_2="<b>$(gettext "Select light or dark appearance")</b>"
ALIGN=center # center or left
HEADER="
	<hbox height-request="'"${BOX_HEIGHT}"'" space-expand=\"false\" space-fill=\"true\">
	$(. $APPDIR/xml_info_jwmdesk "$ICON" "$ICON_HEIGHT" "$MSG_1" "$MSG_2" "$ALIGN")
	</hbox>"

########################################################################
#                                                                      #
# GLOBALTHEME FUNCTION                                                 #
#                                                                      #
########################################################################

# apply new globaltheme
func_apply(){
   gtkdialog-splash -close never -timeout 2 -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'Processing...') "
   # change background
   sed -i 's/<backdrop style=\".*\">/<backdrop style=\"Stretched\">/' $HOME/Choices/ROX-Filer/PuppyPin
   echo -n "$BGDIR/$background" > $HOME/.config/wallpaper/bg_img
   echo 'Stretch' > $HOME/.config/wallpaper/backgroundmode
   [ -f /tmp/current_wallpaper_selection.jpg ] && rm -f /tmp/current_wallpaper_selection.jpg
   ln -sf /usr/share/backgrounds/"$background" /tmp/current_wallpaper_selection.jpg
   set_bg $BGDIR/"$background"
   # change gtk theme
   /usr/local/jwmdesk/ptheme_gtk -t "$gtktheme" 2>/dev/null
   gtkdialog-splash -close never -timeout 2 -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'Restarting JWM...') "
   # change jwm theme
   NEWTHEME="$jwmtheme"
   if [ -s /usr/share/jwm/themes/${NEWTHEME}-jwmrc ]; then
     cp -f /usr/share/jwm/themes/${NEWTHEME}-jwmrc $HOME/.jwm/jwmrc-theme
     echo "$NEWTHEME" > $HOME/.jwm/theme
   else
     exit
   fi
   # restart JWM/JWMdesk/Conky
   sleep 1
   jwm -restart 2>/dev/null
   sleep 3
   gtkdialog-splash -close never -timeout 2 -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'Processing...') "
   $APPDIR/globalthemes &
   sleep 1
   [ -x $HOME/Startup/conkystart ] && /usr/local/bin/conky-restart &
}
export -f func_apply

########################################################################
#                                                                      #
# MAIN DIALOG                                                          #
#                                                                      #
########################################################################

export globalthemes='
<window title="'$(gettext 'Global Themes')'" icon-name="global_themes" resizable="false">
<vbox space-expand="true" space-fill="true">
  <vbox margin="2">
  '${HEADER}'
  </vbox>
  <vbox margin="2" spacing="10" space-expand="true" space-fill="true">

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button height-request="35" tooltip-text=" '$(gettext 'Default Global Theme')' ">
        <label>'$(gettext 'Default Theme')'</label>
        <action>exit:default-theme</action>
      </button>
    </hbox>

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2">
        <label>'$(gettext 'Breeze Light')'</label>
        <input file>'$ICONDIR'/global_light.svg</input><height>30</height><width>30</width>
        <action>exit:breeze-light</action>
      </button>
      <button image-position="2">
        <label>'$(gettext 'Breeze Dark')'</label>
        <input file>'$ICONDIR'/global_dark.svg</input><height>30</height><width>30</width>
        <action>exit:breeze-dark</action>
      </button>
    </hbox>

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2">
        <label>'$(gettext 'Gradient Light')'</label>
        <input file>'$ICONDIR'/global_light.svg</input><height>30</height><width>30</width>
        <action>exit:gradient-light</action>
      </button>
      <button image-position="2">
        <label>'$(gettext 'Gradient Dark')'</label>
        <input file>'$ICONDIR'/global_dark.svg</input><height>30</height><width>30</width>
        <action>exit:gradient-dark</action>
      </button>
    </hbox>

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2">
        <label>'$(gettext 'Graphite Light')'</label>
        <input file>'$ICONDIR'/global_light.svg</input><height>30</height><width>30</width>
        <action>exit:graphite-light</action>
      </button>
      <button image-position="2">
        <label>'$(gettext 'Graphite Dark')'</label>
        <input file>'$ICONDIR'/global_dark.svg</input><height>30</height><width>30</width>
        <action>exit:graphite-dark</action>
      </button>
    </hbox>

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2">
        <label>'$(gettext 'Numix Light')'</label>
        <input file>'$ICONDIR'/global_light.svg</input><height>30</height><width>30</width>
        <action>exit:numix-light</action>
      </button>
      <button image-position="2">
        <label>'$(gettext 'Numix Dark')'</label>
        <input file>'$ICONDIR'/global_dark.svg</input><height>30</height><width>30</width>
        <action>exit:numix-dark</action>
      </button>
    </hbox>

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2">
        <label>'$(gettext 'Stardust Light')'</label>
        <input file>'$ICONDIR'/global_light.svg</input><height>30</height><width>30</width>
        <action>exit:stardust-light</action>
      </button>
       <button image-position="2">
        <label>'$(gettext 'Stardust Dark')'</label>
        <input file>'$ICONDIR'/global_dark.svg</input><height>30</height><width>30</width>
        <action>exit:stardust-dark</action>
      </button>
    </hbox>

    <hseparator space-expand="false" space-fill="false"></hseparator>

    <hbox spacing="10" space-expand="true" space-fill="true">
       <button height-request="35" width-request="35" space-expand="false" space-fill="false" tooltip-text=" '$(gettext 'View current desktop themes')' ">
        <input file>'$ICONDIR'/view.svg</input><height>22</height><width>22</width>
        <action>'$APPDIR'/current_themes &</action>
       </button>
       <button space-expand="true" space-fill="true" tooltip-text=" '$(gettext 'Legacy theme tools')' ">
        <label>'$(gettext 'Theme Tools')'</label>
        <action>puppy_desktop &</action>
       </button>
       <button height-request="35" width-request="35" space-expand="false" space-fill="false" tooltip-text=" '$(gettext 'Quit and return to JWMdesk')' ">
        <input file>'$ICONDIR'/quit.svg</input><height>22</height><width>22</width>
        <action>'$APPDIR'/jwmdesk &</action>
        <action>exit:quit_now</action>
       </button>
    </hbox>

  </vbox>
</vbox>
<action signal="key-press-event" condition="command_is_true([ $KEY_SYM = Escape ] && echo true )">exit:EXIT</action>
</window>'

retvals="`$GTKDIALOG -p globalthemes --center --styles=/tmp/jwmdesk/gtkrc_xml_info.css`"
eval "$retvals"
case $EXIT in
  default-theme) /
    background=bw_emerald.jpg
    gtktheme="Flat-grey-rounded"
    jwmtheme="Flat-grey"
    sed -i "/root:3/ s%icon=".*" border%icon="\"gnome-dark.svg\"" border%" $HOME/.jwmrc-tray
    func_apply & 
    exit ;;
  breeze-light) /
    background=bw_green_light.svg
    gtktheme="Flat-grey-rounded"
    jwmtheme="Breeze_light"
    sed -i "/root:3/ s%icon=".*" border%icon="\"gnome-dark.svg\"" border%" $HOME/.jwmrc-tray
    func_apply & 
    exit ;;
  breeze-dark) /
    background=bw_green_dark.jpg
    gtktheme="Flat-grey-rounded-dark"
    jwmtheme="Breeze_dark"
    sed -i "/root:3/ s%icon=".*" border%icon="\"gnome-light.svg\"" border%" $HOME/.jwmrc-tray
    func_apply &
    exit ;;
  gradient-light) /
    background=bw_gray_light.svg
    gtktheme="Gradient-grey"
    jwmtheme="Gradient_grey"
    sed -i "/root:3/ s%icon=".*" border%icon="\"gnome-dark.svg\"" border%" $HOME/.jwmrc-tray
    func_apply &
    exit ;;
  gradient-dark) /
    background=bw_gray_dark.jpg
    gtktheme="Gradient-grey-dark"
    jwmtheme="Gradient_grey_dark"
    sed -i "/root:3/ s%icon=".*" border%icon="\"gnome-dark.svg\"" border%" $HOME/.jwmrc-tray
    func_apply &
    exit ;;
  graphite-light) /
    background=bw_graphite_light.jpg
    gtktheme="Graphite_light"
    jwmtheme="Graphite_light"
    sed -i "/root:3/ s%icon=".*" border%icon="\"gnome-dark.svg\"" border%" $HOME/.jwmrc-tray
    func_apply &
    exit ;;
  graphite-dark) /
    background=bw_graphite_dark.jpg
    gtktheme="Graphite_dark"
    jwmtheme="Graphite_dark"
    sed -i "/root:3/ s%icon=".*" border%icon="\"gnome-light.svg\"" border%" $HOME/.jwmrc-tray
    func_apply &
    exit ;;
  numix-light) /
    background=bw_red_light.svg
    gtktheme="Numix"
    jwmtheme="Numix"
    sed -i "/root:3/ s%icon=".*" border%icon="\"gnome-light.svg\"" border%" $HOME/.jwmrc-tray
    func_apply &
    exit ;;
  numix-dark) /
    background=bw_red_dark.jpg
    gtktheme="Numix_dark"
    jwmtheme="Numix"
    sed -i "/root:3/ s%icon=".*" border%icon="\"gnome-light.svg\"" border%" $HOME/.jwmrc-tray
    func_apply &
    exit ;;
  stardust-light) /
    background=bw_blue_light.svg
    gtktheme="Stardust_bright"
    jwmtheme="Stardust_bright"
    sed -i "/root:3/ s%icon=".*" border%icon="\"gnome-dark.svg\"" border%" $HOME/.jwmrc-tray
    func_apply &
    exit ;;
  stardust-dark) /
    background=bw_gold_dark.jpg
    gtktheme="Stardust_dark"
    jwmtheme="Stardust_dark"
    sed -i "/root:3/ s%icon=".*" border%icon="\"gnome-light.svg\"" border%" $HOME/.jwmrc-tray
    func_apply &
    exit ;;
  *)exit ;;
esac
