#!/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

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

if [ ! "`ps -A | grep jwm`" ] && [ "`grep -F 'xwin' <<< $(ps -eo pid,command)`" ]; then
	/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.')"
fi

SCRIPT_DIR="/usr/local/jwm_config"
[ ! -d /tmp/theme_switcher ] && mkdir /tmp/theme_switcher

apply_theme () {
	NEWTHEME="$1"
	[ "$NEWTHEME" = "" ] && exit
	if [ -s /usr/share/jwm/themes/${NEWTHEME}-jwmrc ]; then
		#select chosen theme...
		cp -f /usr/share/jwm/themes/${NEWTHEME}-jwmrc $HOME/.jwm/jwmrc-theme
	elif [ -s $HOME/.jwm/themes/${NEWTHEME}-jwmrc ]; then
		cp -f $HOME/.jwm/themes/${NEWTHEME}-jwmrc $HOME/.jwm/jwmrc-theme
	else
		exit
	fi

	. $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

	# 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

	sync
	rm -f $JWMtrayfile.new
	if [ "$UPDATE" = "true" ]; then
		pidof jwm >/dev/null && /usr/local/jwm_config/jwm_restart
		echo ${NEWTHEME} > $HOME/.jwm/theme
	fi
}
export -f apply_theme


#parameters
while [ $# != 0 ]; do
	I=1
	while [ $I -le `echo $# | wc -c` ]; do 
		case $1 in
			-a) apply_theme "$2"; exit;;
			-u) UPDATE=true;;
			-h|--help)
echo 'Usage: icon_switcher [OPTION]

Options
  -a THEME    Activate theme
  -h          Show this help message
  -u          Update desktop'; exit;;
		esac
		shift
		I=$[$I+1]
	done
done
#---


export UPDATE=true #to restart jwm when choosing theme

#run gui if no theme is set
if [ ! "$NEWTHEME" ]; then
	UPDATE=true
	ICONSETSGUI=""
	find /usr/share/jwm/themes/ -mindepth 1 -maxdepth 1 -type f | sort | grep 'jwmrc' | sed -e 's/-jwmrc//' > $HOME/.jwm/tmp/themes
	find $HOME/.jwm/themes/ -mindepth 1 -maxdepth 1 -type f | sort | grep 'jwmrc' | sed -e 's/-jwmrc//' >> $HOME/.jwm/tmp/themes
	while read I; do
		ONETHEME="`basename $I`"
		if [ ! "`grep -F "/${ONETHEME}.svg" <<< "$ICONSETSGUI"`" ]; then #skip if already there
			COLOR_1="$(grep -FA 100 '<WindowStyle>' "${I}-jwmrc" | grep -Fm1 -A100 '<Active>' | grep -Fm1 '<Background>' | awk -F'Background>' '{print $2}' | cut -d: -f1 | cut -d'<' -f1)"
			COLOR_2="$(grep -FA 100 '<MenuStyle>' ${I}-jwmrc |  grep -Fm1 -A100 '<Active>' | grep -Fm1 '<Background>' | awk -F'Background>' '{print $2}' | cut -d: -f1 | cut -d'<' -f1)"
			COLOR_3="$(grep -FA 100 '<WindowStyle>' ${I}-jwmrc | grep -Fm1 -A100 '<Active>' | grep -Fm1 '<Background>' |awk -F'Background>' '{print $2}' | cut -d: -f1| cut -d'<' -f1)"
			COLOR_4="$(grep -FA 100 '<PagerStyle>' ${I}-jwmrc | grep -Fm1 -A100 '<Active>' | grep -Fm1 '<Background>' | awk -F'Background>' '{print $2}' | cut -d'<' -f1)"
			COLOR_5="$(grep -FA 100 '<MenuStyle>' ${I}-jwmrc | grep -Fm1 '<Foreground>' | awk -F'Foreground>' '{print $2}' | cut -d: -f1 | cut -d'<' -f1)"
			echo '<svg version="1.0">
			<defs id="defs">
			  <linearGradient id="Gradient0" gradientUnits="userSpaceOnUse" x2="250">
				<stop stop-color="'${COLOR_1}'" offset="0"/>
				<stop stop-color="'${COLOR_2}'" offset=".33"/>
				<stop stop-color="'${COLOR_3}'" offset=".66"/>
				<stop stop-color="'${COLOR_4}'" offset="1"/>
			  </linearGradient>
			</defs>
			<rect fill-rule="evenodd" height="32" width="300" y="0" x="0" fill="url(#Gradient0)"/>
			<path style="fill:none;stroke:#000;stroke-width:1px" d="M 0,0 300,0 300,32 0,32 z"/>
			<text style="fill:'${COLOR_5}';font-family:DejuVu;font-size:16;font-weight:bold;text-anchor:left" x="20" y="24" >'${ONETHEME}'</text>
			</svg>' > /tmp/theme_switcher/${ONETHEME}.svg
			ICONSETSGUI=$ICONSETSGUI'
			<hbox homogeneous="true">
			  <hbox space-expand="false" space-fill="false">
			  <vbox spacing="10" space-expand="true" space-fill="true">
				<button relief="2" space-expand="true" space-fill="true">
				  <input file>/tmp/theme_switcher/'${ONETHEME}'.svg</input>
				  <action>apply_theme "'${ONETHEME}'"</action>
				</button>
			  </vbox>
			  </hbox>
			</hbox>'
		fi
	done < $HOME/.jwm/tmp/themes

	S='
	<window title="'$(gettext 'JWM Theme Switcher')'" icon-name="gtk-convert" height-request="500">
	  <vbox space-expand="true" space-fill="true">
	    '"`/usr/lib/gtkdialog/xml_info fixed "windows_theme.svg" 60 "$(gettext 'JWM is the window manager, handling window borders, desktop menu and tray (taskbar) appearance. This utility colorize these.')"`"' 
	    <vbox space-expand="true" space-fill="true">
	      <vbox scrollable="true" spacing="10" margin="10">
	        '${ICONSETSGUI}'
	      </vbox> 
	    </vbox> 
	    <hbox space-expand="false" space-fill="false">
	      <button space-expand="false" space-fill="false">
	        '"`/usr/lib/gtkdialog/xml_button-icon internet`"'
	        <label>'$(gettext 'More themes')'</label>
	        <action>defaulthtmlviewer http://www.murga-linux.com/puppy/viewtopic.php?t=23260 &</action>
	      </button>'
	      [ "`which ptheme`" ] && S=$S'<button space-expand="false" space-fill="false">
	        '"`/usr/lib/gtkdialog/xml_button-icon puppy_theme.svg`"'
	        <label>" '$(gettext 'Global theming')' "</label>
	        <action>/usr/sbin/ptheme &</action>
	      </button>'
	      S=$S'<text space-expand="true" space-fill="true"><label>""</label></text>
	      <button>
	        '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
	        <label>'$(gettext 'Cancel')'</label>
	        <action condition="command_is_true([[ -s $HOME/.jwm/theme ]] && echo true)">apply_theme '"$(<$HOME/.jwm/theme)"'</action>
	        <action>EXIT:exit</action>
	      </button>
	      <button>
	        '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
	        <label>'$(gettext 'Ok')'</label>
	      </button>
	      '"`/usr/lib/gtkdialog/xml_scalegrip`"'
	    </hbox>
	  </vbox>
	</window>'
	
	export ICONSWITCHGUI="$S"
	. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
	gtkdialog -p ICONSWITCHGUI
fi

