#!/bin/sh
#(c) Copyright Jan 2008 Barry Kauler www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#written jan 2008 for Puppy Linux.
#v431 improvements by Patriot.
# 20120126 added i18n, upgrade for at least gtkdialog>=0.8.0
# radky 151020: adjust for JWMDesk; add alphanumeric sorting of themes
# radky 151115: adjust GUI; add theming compatibility with jwm_config
# radky 160101: adjust GUI for legacy JWM
# radky 170415: add status report of current themes; adjust compatibility with jwm_config of Woof-CE
# radky 171010: adjust compatibility with jwm_config of Woof-CE

export TEXTDOMAIN=puppy
export OUTPUT_CHARSET=UTF-8
. gettext.sh

#define gtkdialog
if [ "`which gtkdialog4`" ]; then
 GTKDIALOG=gtkdialog4
elif [ "`which gtkdialog3`" ]; then
 GTKDIALOG=gtkdialog3
else
 GTKDIALOG=gtkdialog
fi
export GTKDIALOG

# apply new theme
newtheme_func()
{
NEWTHEME="$1"
[ "$NEWTHEME" = "" ] && exit
[ ! -f $HOME/.jwm/themes/${NEWTHEME}-jwmrc ] && exit

if [ "$NEWTHEME" != "" ];then
 #record chosen theme...
 echo "$NEWTHEME" > $HOME/.jwm/theme
 #select chosen theme...
 cp -f $HOME/.jwm/themes/${NEWTHEME}-jwmrc $HOME/.jwm/jwmrc-theme
 #set correct backgrounds for applets...
 [ -f $HOME/.jwm/themes/${NEWTHEME}-colors ] && cp -f $HOME/.jwm/themes/${NEWTHEME}-colors $HOME/.jwm/jwm_colors

  # Patriot Sep 2009
  # Update only for known -bg option applets: blinky and xload

  MENU_BG=""
  [ -f $HOME/.jwm/themes/${NEWTHEME}-colors ] && . $HOME/.jwm/jwm_colors #Get MENU_BG, PAGER_BG

  JWMtrayfile="$HOME/.jwmrc-tray"
  ThemeFile="$HOME/.jwm/jwmrc-theme"

  if [[ ! $MENU_BG ]]; then
	# if MENU_BG not found in jwm_colors
	if [ -e $ThemeFile ]; then
	  # Get MENU_BG from newly selected theme
	  MENU_BG=$(awk -F: '/MenuStyle/,/<\/MenuStyle>/' ${ThemeFile} | grep '<Background>' | sed -e 's|</.*>||g' -e 's|.*<.*>||g')
	  # theme file could be borked, so hardcode a fallback
	  [[ ! $MENU_BG ]] && MENU_BG="#292D29"
	else
	  # if theme file missing ...
	  MENU_BG="#292D29"
	fi
  fi

  if [[ ! $PAGER_BG ]]; then
	if [ -e $ThemeFile ]; then
	  PAGER_BG=$(awk -F: '/PagerStyle/,/<\/PagerStyle>/' ${ThemeFile} | grep '<Background>' | sed -e 's|</.*>||g' -e 's|.*<.*>||g')
	  [[ ! $PAGER_BG ]] && PAGER_BG="#292D29"
	else
	  PAGER_BG="#292D29"
	fi
  fi

  if [ -f $JWMtrayfile ]; then
    # Make a backup !
    cp -f $JWMtrayfile $JWMtrayfile.bak
    # Update applet -bg colors, catch-all method
    sed -e 's|\(.*blinky.*\) -bg ".*"|\1 -bg "'"$MENU_BG"'"|g' -e 's|\(.*xload.*\) -bg ".*"|\1 -bg "'"$PAGER_BG"'"|g' < $JWMtrayfile > $JWMtrayfile.new

    # Precaution: update to jwmrc-tray only if not empty
    [ -s $JWMtrayfile.new ] && cp -f $JWMtrayfile.new $JWMtrayfile
  fi

  sync
  [ -f $JWMtrayfile.new ] && rm -f $JWMtrayfile.new
  pidof jwm >/dev/null && jwm -restart 2>/dev/null
fi
}
export -f newtheme_func

# format ICONSETSGUI for jwm_config environment, else legacy jwm environment
if [ -d /usr/share/jwm/themes ]; then
	mkdir -p $HOME/.jwm/themes
	ln -snf /usr/share/jwm/themes/* $HOME/.jwm/themes
	find -L $HOME/.jwm/themes/ -type l -delete 2>/dev/null #remove broken symlinks
	ICONSETSGUI=""
	for ONETHEME in `find -L $HOME/.jwm/themes/ -mindepth 1 -maxdepth 1 -type f | sort | grep 'jwmrc' | sed -e 's/-jwmrc//' | tr '\n' ' '`
	do
	 ONETHEME="`basename $ONETHEME`"
	 ICONSETSGUI="$ICONSETSGUI
	 <hbox homogeneous=\"true\" margin=\"5\">
	  <vbox>
	   <button height-request=\"41\" width-request=\"245\"><label>${ONETHEME}</label>
	    <action>newtheme_func "${ONETHEME}" 2>/dev/null</action>
	   </button>
	  </vbox>
	 </hbox>"
	done
else
	ICONSETSGUI=""
	for ONETHEME in `find /root/.jwm/themes/ -mindepth 1 -maxdepth 1 -type f | sort | grep 'jwmrc' | sed -e 's/-jwmrc//' | tr '\n' ' '`
	do
	 ONETHEME="`basename $ONETHEME`"
	 ICONSETSGUI="$ICONSETSGUI
	 <pixmap><input file>/usr/local/jwmdesk/icons/invisible96x8.png</input></pixmap>
	 <hbox space-expand=\"false\" space-fill=\"false\">
	  <vbox space-expand=\"false\" space-fill=\"false\">
	   <hbox>
	    <pixmap><width>150</width><input file>/root/.jwm/themes/${ONETHEME}-tray.png</input></pixmap>
	    <pixmap><width>70</width><input file>/root/.jwm/themes/${ONETHEME}-window.png</input></pixmap>
	   </hbox>
	  </vbox>
	  <vbox space-expand=\"true\" space-fill=\"true\">
	   <button space-expand=\"true\" space-fill=\"false\" height-request=\"36\"><label>${ONETHEME}</label>
	    <action>newtheme_func "${ONETHEME}"</action>
	   </button>
	  </vbox>
	 </hbox>"
	done
fi

CURRENT_THEMES=""
if [ ! -d /usr/local/jwm_config* ]; then
 CURRENT_THEMES="  <hbox space-expand=\"false\" space-fill=\"false\">
   <button tooltip-text=\" $(gettext 'View current desktop themes') \">
    <label>$(gettext 'Themes')</label>
    <input file>$ICONDIR/view.svg</input><height>20</height><width>20</width>
    <action>currentthemes &</action>
   </button>
  </hbox>
  <text space-expand=\"true\" space-fill=\"true\"><label>\"\"</label></text>"
fi

export ICONSWITCHGUI="
<window title=\"$(gettext 'JWM Theme Switcher')\" icon-name=\"gtk-convert\" window-position=\"1\">
 <vbox>
  <hbox homogeneous=\"true\" width-request=\"455\" margin=\"5\">
  <text><label>$(gettext 'The JWM theme defines all colors for the desktop menu, tray and window borders.')</label></text>
  </hbox>
  <frame Quick Colour Changer>
  <vbox>
    <button>
    <label>$(gettext 'Colour Chooser')</label>
    <input file>$ICONDIR/windows.svg</input><height>20</height><width>20</width>
    <action>jcc &</action>
   </button>
  </vbox>
  </frame>
  <frame Installed Themes>
  <hbox height-request=\"400\" space-expand=\"true\" space-fill=\"true\">
   <vbox margin=\"5\" scrollable=\"true\">
   ${ICONSETSGUI}
   </vbox> 
  </hbox>
  </frame>
  <hbox space-expand=\"false\" space-fill=\"false\">
   $CURRENT_THEMES
   <button>
    <label>$(gettext 'Quit')</label>
    <input file>$ICONDIR/quit.svg</input><height>20</height><width>20</width>
    <action>exit:quit_now</action>
   </button>
  </hbox>
 </vbox>
</window>"

$GTKDIALOG --program=ICONSWITCHGUI

###END###
