#!/bin/bash
# global themes in Labwc environment

export TEXTDOMAIN=globalthemes

GTKDIALOG=gtkdialog

BGDIR=/usr/share/backgrounds

PIXMAPSDIR=/usr/share/pixmaps
[ ! -f $PIXMAPSDIR/global-light.svg ] && ln -sf /usr/share/pixmaps/puppy/puppy_theme_daylight_mouse.svg $PIXMAPSDIR/global-light.svg
[ ! -f $PIXMAPSDIR/global-dark.svg ] && ln -sf /usr/share/pixmaps/puppy/puppy_theme_eve_mouse.svg $PIXMAPSDIR/global-dark.svg

export GTKDIALOG BGDIR PIXMAPSDIR

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

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

# apply new globaltheme
func_apply(){
   # set openbox theme
   openboxtheme="$gtktheme"
   sed -i "/<theme>/!b;n;c\\    <name>$openboxtheme<\/name>" $HOME/.config/labwc/rc.xml
   # set gtk theme
   gsettings set org.gnome.desktop.interface gtk-theme "$gtktheme"
   # set system icon theme
   gsettings set org.gnome.desktop.interface icon-theme "$icontheme"
   # set background
   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"
   # set clock font color
    sed -i "s/color:.*;/color: $clockcolor;/" $HOME/.config/sfwbar/clock.widget
   # sync and apply themes
   /usr/bin/ptheme_gtk
   labwc -r
   killall -SIGHUP sfwbar &
}
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">
    <text use-markup="true"><label>"<b><span size='"'x-large'"'>          '$(gettext 'Global Desktop Themes')'          </span></b>"</label></text>
    <text use-markup="true"><label>"<b><span size='"'medium'"'>'$(gettext 'Select light or dark appearance')'</span></b>"</label></text>
    <hseparator space-expand="false" space-fill="false"></hseparator>
  </vbox>
  <vbox margin="2" spacing="10" space-expand="true" space-fill="true">

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2"tooltip-text=" '$(gettext 'Adwaita Blue Theme')' ">
        <label>'$(gettext 'Adwaita')'</label>
        <input file>'$PIXMAPSDIR'/global-light.svg</input><height>30</height><width>30</width>
        <action>exit:adwaita</action>
      </button>
      <button image-position="2"tooltip-text=" '$(gettext 'Dark Adwaita Blue Theme')' ">
        <label>'$(gettext 'Adwaita Dark')'</label>
        <input file>'$PIXMAPSDIR'/global-dark.svg</input><height>30</height><width>30</width>
        <action>exit:adwaita-dark</action>
      </button>
    </hbox>

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2"tooltip-text=" '$(gettext 'Graphite Teal Theme')' ">
        <label>'$(gettext 'Graphite')'</label>
        <input file>'$PIXMAPSDIR'/global-light.svg</input><height>30</height><width>30</width>
        <action>exit:graphite</action>
      </button>
      <button image-position="2"tooltip-text=" '$(gettext 'Dark Graphite Teal Theme')' ">
        <label>'$(gettext 'Graphite Dark')'</label>
        <input file>'$PIXMAPSDIR'/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"tooltip-text=" '$(gettext 'Greybird Gray Theme')' ">
        <label>'$(gettext 'Greybird')'</label>
        <input file>'$PIXMAPSDIR'/global-light.svg</input><height>30</height><width>30</width>
        <action>exit:greybird</action>
      </button>
      <button image-position="2"tooltip-text=" '$(gettext 'Dark Greybird Gray Theme')' ">
        <label>'$(gettext 'Greybird Dark')'</label>
        <input file>'$PIXMAPSDIR'/global-dark.svg</input><height>30</height><width>30</width>
        <action>exit:greybird-dark</action>
      </button>
    </hbox>

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2" tooltip-text=" '$(gettext 'Nordic Blue Theme')' ">
        <label>'$(gettext 'Nordic')'</label>
        <input file>'$PIXMAPSDIR'/global-light.svg</input><height>30</height><width>30</width>
        <action>exit:nordic</action>
      </button>
       <button image-position="2" tooltip-text=" '$(gettext 'Dark Nordic Blue Theme')' ">
        <label>'$(gettext 'Nordic Dark')'</label>
        <input file>'$PIXMAPSDIR'/global-dark.svg</input><height>30</height><width>30</width>
        <action>exit:nordic-dark</action>
      </button>
    </hbox>

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2" tooltip-text=" '$(gettext 'Numix Red Theme')' ">
        <label>'$(gettext 'Numix')'</label>
        <input file>'$PIXMAPSDIR'/global-light.svg</input><height>30</height><width>30</width>
        <action>exit:numix</action>
      </button>
      <button image-position="2" tooltip-text=" '$(gettext 'Dark Numix Red Theme')' ">
        <label>'$(gettext 'Numix Dark')'</label>
        <input file>'$PIXMAPSDIR'/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" tooltip-text=" '$(gettext 'Qogir Blue Theme')' ">
        <label>'$(gettext 'Qogir')'</label>
        <input file>'$PIXMAPSDIR'/global-light.svg</input><height>30</height><width>30</width>
        <action>exit:qogir</action>
      </button>
      <button image-position="2" tooltip-text=" '$(gettext 'Dark Qogir Blue Theme')' ">
        <label>'$(gettext 'Qogir Dark')'</label>
        <input file>'$PIXMAPSDIR'/global-dark.svg</input><height>30</height><width>30</width>
        <action>exit:qogir-dark</action>
      </button>
    </hbox>

    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2" tooltip-text=" '$(gettext 'Stardust Blue Theme')' ">
        <label>'$(gettext 'Stardust')'</label>
        <input file>'$PIXMAPSDIR'/global-light.svg</input><height>30</height><width>30</width>
        <action>exit:stardust</action>
      </button>
       <button image-position="2" tooltip-text=" '$(gettext 'Dark Stardust Gold Theme')' ">
        <label>'$(gettext 'Stardust Dark')'</label>
        <input file>'$PIXMAPSDIR'/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="true" space-fill="true" tooltip-text=" '$(gettext 'View current desktop themes')' ">
        <label>'$(gettext 'Themes')'</label>
        <input file>/usr/share/pixmaps/puppy/find.svg</input><height>22</height><width>22</width>
        <action>current_themes &</action>
       </button>
       <button height-request="35" width-request="35" space-expand="true" space-fill="true">
        <label>'$(gettext 'Quit')'</label>
         <input file>/usr/share/pixmaps/puppy/quit.svg</input><height>22</height><width>22</width>
       <action type="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`"
eval "$retvals"
case $EXIT in
  adwaita) /
    background=trixie_wave_blue.jpg
    gtktheme="Adwaita"
    icontheme="PMaterial-blue"
    clockcolor="black"
    func_apply & 
    exit ;;
  adwaita-dark) /
    background=trixie_wave_blue.jpg
    gtktheme="Adwaita_dark"
    icontheme="PMaterial-blue"
    clockcolor="white"
    func_apply &
    exit ;;
  graphite) /
    background=trixie_marble_teal.jpg
    gtktheme="Graphite"
    icontheme="PMaterial-teal"
    clockcolor="black"
    func_apply & 
    exit ;;
  graphite-dark) /
    background=trixie_marble_teal.jpg
    gtktheme="Graphite_dark"
    icontheme="PMaterial-teal"
    clockcolor="white"
    func_apply &
    exit ;;
  greybird) /
    background=trixie_gray.jpg
    gtktheme="Greybird"
    icontheme="Puppy Standard"
    clockcolor="black"
    func_apply & 
    exit ;;
  greybird-dark) /
    background=trixie_gray.jpg
    gtktheme="Greybird_dark"
    icontheme="Puppy Standard"
    clockcolor="white"
    func_apply &
    exit ;;
  nordic) /
    background=trixie_marble_blue.jpg
    gtktheme="Nordic"
    icontheme="PMaterial-blue"
    clockcolor="black"
    func_apply &
    exit ;;
  nordic-dark) /
    background=trixie_marble_blue.jpg
    gtktheme="Nordic_dark"
    icontheme="PMaterial-blue"
    clockcolor="white"
    func_apply &
    exit ;;
  numix) /
    background=trixie_marble_red.jpg
    gtktheme="Numix"
    icontheme="PMaterial-red"
    clockcolor="black"
    func_apply & 
    exit ;;
  numix-dark) /
    background=trixie_marble_red.jpg
    gtktheme="Numix_dark"
    icontheme="PMaterial-red"
    clockcolor="white"
    func_apply & 
    exit ;;
  qogir) /
    background=trixie_marble_indigo.jpg
    gtktheme="Qogir"
    icontheme="PMaterial-azure"
    clockcolor="black"
    func_apply &
    exit ;;
  qogir-dark) /
    background=trixie_marble_indigo.jpg
    gtktheme="Qogir_dark"
    icontheme="PMaterial-azure"
    clockcolor="white"
    func_apply &
    exit ;;
  stardust) /
    background=trixie_marble_blue.jpg
    gtktheme="Stardust_bright"
    icontheme="PMaterial-blue"
    clockcolor="black"
    func_apply &
    exit ;;
  stardust-dark) /
    background=trixie_marble_gold.jpg
    gtktheme="Stardust_dark"
    icontheme="PMaterial-sand"
    clockcolor="black"
    func_apply &
    exit ;;
  *)exit ;;
esac
