#!/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 220303: adjust compatibility with JWMdesk
# radky 220505: adjust horizontal and vertical gradients of GUI svg graphics

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

[ ! "`pidof jwm`" ] && gtkdialog-splash -close never -timeout 5 -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'The JWM window manager is not active !') " && exit 0

if [ "`which busybox`" ]; then
	TS=$(busybox ps | grep -a -- gtkdialog | grep -a -- THEMESWITCHGUI | awk '{print $1}')
else
	TS=$(ps ax | grep -a -- gtkdialog | grep -a -- THEMESWITCHGUI | awk '{print $1}')
fi
if [ "$TS" ]; then
	sleep 1
	[ "$TS" ] && gtkdialog-splash -close never -timeout 5 -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'JWM Theme Switcher is already active !') " && exit 0
fi

APPDIR=/usr/local/jwmdesk
ICONDIR=$APPDIR/icons

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

# define title-bar icon
[ ! -f /usr/share/icons/hicolor/48x48/apps/theme_switcher.svg ] && \
ln -sf /usr/local/jwmdesk/icons/theme_switcher.svg /usr/share/icons/hicolor/48x48/apps && gtk-update-icon-cache -f -i /usr/share/icons/hicolor 2>/dev/null

# set 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/theme_switcher.svg
ICON_HEIGHT=70
MSG_1="<b><span size='"'x-large'"'>$(gettext "JWM Theme Switcher")</span></b>"
MSG_2="<b>$(gettext "Change JWM theme colors")</b>"
ALIGN=center # center or left
HEADER="
	<hbox height-request="'"${BOX_HEIGHT}"'">
	$(. $APPDIR/xml_info_jwmdesk "$ICON" "$ICON_HEIGHT" "$MSG_1" "$MSG_2" "$ALIGN")
	</hbox>"

# apply new theme
newtheme_func()
{
	NEWTHEME="$1"
	[ "$NEWTHEME" = "" ] && exit

	#select chosen theme...
	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/tray_active_preset
		echo "$NEWTHEME" > $HOME/.jwm/theme
	elif [ -s $HOME/.jwm/themes/${NEWTHEME}-jwmrc ]; then
		cp -f $HOME/.jwm/themes/${NEWTHEME}-jwmrc $HOME/.jwm/jwmrc-theme
		echo "$NEWTHEME" > $HOME/.jwm/theme
	else
		exit
	fi

	#set correct backgrounds for applets...
	[ -f $HOME/.jwm/themes/${NEWTHEME}-colors ] && cp -f $HOME/.jwm/themes/${NEWTHEME}-colors $HOME/.jwm/jwm_colors
	[ -s $HOME/.jwm/jwm_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
}
export -f newtheme_func

# format THEMESETGUI
THEMESETGUI=""
gtkdialog-splash -close never -margin 8 -placement center -fontsize large -bg goldenrod -text " $(gettext 'Processing...') " &
splashPID=$!
	DEF_LANG=$LANG
	LANG=C
	[ ! -d /tmp/jwmtheme_switcher ] && mkdir -p /tmp/jwmtheme_switcher
	[ ! -d /tmp/jwmtheme_switcher2 ] && mkdir -p /tmp/jwmtheme_switcher2
	[ ! -d /tmp/jwmtheme_switcher3 ] && mkdir -p /tmp/jwmtheme_switcher3
	echo > /tmp/tmp
	if [ -d /usr/share/jwm/themes ]; then
		find /usr/share/jwm/themes/ -mindepth 1 -maxdepth 1 -type f | sort | grep 'jwmrc' | sed -e 's/-jwmrc//' > /tmp/tmp
		[ -d $HOME/.jwm/themes ] && find $HOME/.jwm/themes/ -mindepth 1 -maxdepth 1 -type f | sort | grep 'jwmrc' | sed -e 's/-jwmrc//' >> /tmp/tmp
	else
		[ -d $HOME/.jwm/themes ] && find $HOME/.jwm/themes/ -mindepth 1 -maxdepth 1 -type f | sort | grep 'jwmrc' | sed -e 's/-jwmrc//' > /tmp/tmp
	fi
	sort -f /tmp/tmp > /tmp/jwm-themes

	if  ldd $(which gtkdialog) | grep -Fq 'gtk-3'; then BH=37 ; else BH=40 ; fi # button height

	while read I; do
		[[ $(grep -E '<<|>>' "${I}-jwmrc" 2>/dev/null) ]] && sed -i -e 's/<</</g' -e 's/>>/>/g' "${I}-jwmrc"
		ONETHEME="`basename $I`"
		# define box-1 colors: gradient representation of active theme colors (window, menu, pager)
		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 | cut -d'<' -f1)"
		# define box-2 colors: title bar of active window
		COLOR_5="$(grep -FA 100 '<WindowStyle' "${I}-jwmrc" | grep -Fm1 -A100 '<Active>' | grep -Fm1 '<Background>' | awk -F'Background>' '{print $2}' | cut -d: -f2 | cut -d'<' -f1 | sed -e 's/gray[0-4][0-9]/#404040/g' -e 's/grey[0-4][0-9]/#404040/g' -e 's/gray[5-9][0-9]/#BFBFBF/g' -e 's/grey[5-9][0-9]/#BFBFBF/g')"
		COLOR_6="$(grep -FA 100 '<WindowStyle' "${I}-jwmrc" | grep -Fm1 -A100 '<Active>' | grep -Fm1 '<Background>' | awk -F'Background>' '{print $2}' | cut -d: -f1 | cut -d'<' -f1 | sed -e 's/gray[0-4][0-9]/#404040/g' -e 's/grey[0-4][0-9]/#404040/g' -e 's/gray[5-9][0-9]/#BFBFBF/g' -e 's/grey[5-9][0-9]/#BFBFBF/g')"
		COLOR_7="$(grep -FA 100 '<WindowStyle' "${I}-jwmrc" | grep -Fm1 -A100 '<Active>' | grep -Fm1 '<Foreground>' | awk -F'Foreground>' '{print $2}' | cut -d: -f1 | cut -d'<' -f1 | sed -e 's/gray[0-4][0-9]/#404040/g' -e 's/grey[0-4][0-9]/#404040/g' -e 's/gray[5-9][0-9]/#E5E5E5/g' -e 's/grey[5-9][0-9]/#E5E5E5/g')"
		# define box-3 colors: title bar of inactive window
		COLOR_8="$(grep -FB 100 '</WindowStyle>' "${I}-jwmrc" | grep -Fm1 -B100 '<Active>' | grep -Fm1 '<Background>' | awk -F'Background>' '{print $2}' | cut -d: -f2 | cut -d'<' -f1 | sed -e 's/gray[0-4][0-9]/#404040/g' -e 's/grey[0-4][0-9]/#404040/g' -e 's/gray[5-9][0-9]/#BFBFBF/g' -e 's/grey[5-9][0-9]/#BFBFBF/g')"
		COLOR_9="$(grep -FB 100 '</WindowStyle>' "${I}-jwmrc" | grep -Fm1 -B100 '<Active>' | grep -Fm1 '<Background>' | awk -F'Background>' '{print $2}' | cut -d: -f1 | cut -d'<' -f1 | sed -e 's/gray[0-4][0-9]/#404040/g' -e 's/grey[0-4][0-9]/#404040/g' -e 's/gray[5-9][0-9]/#BFBFBF/g' -e 's/grey[5-9][0-9]/#BFBFBF/g')"
		if [ ! "$COLOR_8" ]; then
			COLOR_8="$(grep -FA 100 '<WindowStyle' "${I}-jwmrc" | grep -Fm1 -A100 '</Active>' | grep -Fm1 '<Background>' | awk -F'Background>' '{print $2}' | cut -d: -f2 | cut -d'<' -f1 | sed -e 's/gray[0-4][0-9]/#404040/g' -e 's/grey[0-4][0-9]/#404040/g' -e 's/gray[5-9][0-9]/#BFBFBF/g' -e 's/grey[5-9][0-9]/#BFBFBF/g')"
			COLOR_9="$(grep -FA 100 '<WindowStyle' "${I}-jwmrc" | grep -Fm1 -A100 '</Active>' | grep -Fm1 '<Background>' | awk -F'Background>' '{print $2}' | cut -d: -f1 | cut -d'<' -f1 | sed -e 's/gray[0-4][0-9]/#404040/g' -e 's/grey[0-4][0-9]/#404040/g' -e 's/gray[5-9][0-9]/#BFBFBF/g' -e 's/grey[5-9][0-9]/#BFBFBF/g')"
		fi
		COLOR_10="$(grep -FB 100 '</WindowStyle>' "${I}-jwmrc" | grep -Fm1 -B100 '<Active>' | grep -Fm1 '<Foreground>' | awk -F'Foreground>' '{print $2}' | cut -d: -f1 | cut -d'<' -f1 | sed -e 's/gray[0-4][0-9]/#404040/g' -e 's/grey[0-4][0-9]/#404040/g' -e 's/gray[5-9][0-9]/#E5E5E5/g' -e 's/grey[5-9][0-9]/#E5E5E5/g')"
		[ ! "$COLOR_10" ] && COLOR_10="$(grep -FA 100 '<WindowStyle' "${I}-jwmrc" | grep -Fm1 -A100 '</Active>' | grep -Fm1 '<Foreground>' | awk -F'Foreground>' '{print $2}' | cut -d: -f1 | cut -d'<' -f1 | sed -e 's/gray[0-4][0-9]/#404040/g' -e 's/grey[0-4][0-9]/#404040/g' -e 's/gray[5-9][0-9]/#E5E5E5/g' -e 's/grey[5-9][0-9]/#E5E5E5/g')"
		[ "$COLOR_10" = "$COLOR_8" ] && COLOR_10="#BEBEBE"

		# create box-1: active theme colors
		echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg version="1.0">
   <defs id="defs">
     <linearGradient id="hgradient" gradientUnits="userSpaceOnUse" x2="150">
      <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="'$BH'" width="150" y="0" x="0" fill="url(#hgradient)"/>
   <path style="fill:none;stroke:#000;stroke-width:1px" d="M 0,0 150,0 150,'$BH' 0,'$BH' z"/>
   <text
      style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:18.66666603px;line-height:1.25;font-family:Sans;letter-spacing:0px;word-spacing:0px;fill:'${COLOR_7}';fill-opacity:1;stroke:none"
      xml:space="preserve"><tspan y="27.055677" x="9.1898441" id="tspan92">'$(gettext 'Menu')'</tspan>
   </text>
</svg>' > /tmp/jwmtheme_switcher/"${ONETHEME}".svg

		# create box-2: active window
		echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg version="1.0">
  <defs id="defs">
     <linearGradient id="vgradient" x1="50%" y1="100%" x2="50%" y2="0%">
      <stop stop-color="'${COLOR_5}'" offset="0%"/>
      <stop stop-color="'${COLOR_6}'" offset="95%"/>
     </linearGradient>
  </defs>
  <rect
     height="30"
     width="101"
     y="0"
     x="0"
     id="rect60"
     style="fill:url(#vgradient);fill-rule:evenodd;stroke-width:0.71063375" />
  <path
     style="fill:none;stroke:#000000;stroke-width:0.71500754px"
     d="M -0.14249623,-0.14249665 H 101.1425 V 30.142495 H -0.14249623 Z"
     id="path62" />
  <path
     id="path144"
     d="M 10,22.921092 H 31.021969"
     style="stroke:'${COLOR_7}';stroke-width:2.30781531" />
  <rect
     id="rect234"
     y="7.6538973"
     x="41.699795"
     height="14.779022"
     width="16.684103"
     style="fill:none;stroke:'${COLOR_7}';stroke-width:3.14053917" />
  <path
     id="path236"
     d="M 43.368203,10.255007 H 56.715486"
     style="stroke:'${COLOR_7}';stroke-width:3.14053917" />
  <path
     id="path307"
     d="M 71.542917,8.4885447 89.202754,22.440762"
     style="stroke:'${COLOR_7}';stroke-width:4.1858511" />
  <path
     id="path309"
     d="M 71.542917,22.440762 89.202754,8.4885447"
     style="stroke:'${COLOR_7}';stroke-width:4.1858511" />
</svg>' > /tmp/jwmtheme_switcher2/"${ONETHEME}".svg

		# create box-3: inactive window
		echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg version="1.0">
  <defs id="defs">
     <linearGradient id="vgradient" x1="50%" y1="100%" x2="50%" y2="0%">
      <stop stop-color="'${COLOR_8}'" offset="0%"/>
      <stop stop-color="'${COLOR_9}'" offset="95%"/>
     </linearGradient>
  </defs>
  <rect
     height="30"
     width="101"
     y="0"
     x="0"
     id="rect60"
     style="fill:url(#vgradient);fill-rule:evenodd;stroke-width:0.71063375" />
  <path
     style="fill:none;stroke:#000000;stroke-width:0.71500754px"
     d="M -0.14249623,-0.14249665 H 101.1425 V 30.142495 H -0.14249623 Z"
     id="path62" />
  <path
     id="path144"
     d="M 10,22.921092 H 31.021969"
     style="stroke:'${COLOR_10}';stroke-width:2.30781531" />
  <rect
     id="rect234"
     y="7.6538973"
     x="41.699795"
     height="14.779022"
     width="16.684103"
     style="fill:none;stroke:'${COLOR_10}';stroke-width:3.14053917" />
  <path
     id="path236"
     d="M 43.368203,10.255007 H 56.715486"
     style="stroke:'${COLOR_10}';stroke-width:3.14053917" />
  <path
     id="path307"
     d="M 71.542917,8.4885447 89.202754,22.440762"
     style="stroke:'${COLOR_10}';stroke-width:4.1858511" />
  <path
     id="path309"
     d="M 71.542917,22.440762 89.202754,8.4885447"
     style="stroke:'${COLOR_10}';stroke-width:4.1858511" />
</svg>' > /tmp/jwmtheme_switcher3/"${ONETHEME}".svg

		# set GUI widgets
		THEMESETGUI=$THEMESETGUI"
					<hbox homogeneous=\"true\" margin=\"5\">
						<hbox>
							<vbox space-expand=\"false\" space-fill=\"false\">
								<pixmap space-expand=\"true\" space-fill=\"false\" height-request=\"$BH\" width-request=\"150\">
									<input file>/tmp/jwmtheme_switcher/"${ONETHEME}".svg</input>
								</pixmap>
							</vbox>
							<vbox space-expand=\"false\" space-fill=\"false\" homogeneous=\"true\">
								<pixmap space-expand=\"true\" space-fill=\"false\">
									<input file>/tmp/jwmtheme_switcher2/"${ONETHEME}".svg</input>
								</pixmap>
							</vbox>
							<vbox space-expand=\"false\" space-fill=\"false\" homogeneous=\"true\">
								<pixmap space-expand=\"true\" space-fill=\"false\">
									<input file>/tmp/jwmtheme_switcher3/"${ONETHEME}".svg</input>
								</pixmap>
							</vbox>
							<vbox space-expand=\"false\" space-fill=\"false\">
								<button height-request=\"${BH}\" width-request=\"245\"><label>${ONETHEME}</label>
									<action>newtheme_func "${ONETHEME}" 2>/dev/null</action>
								</button>
							</vbox>
						</hbox>
					</hbox>"
	done < /tmp/jwm-themes
kill $splashPID 2>/dev/null

LANG=$DEF_LANG

# current background, gtk, jwm, icon and cursor themes 
CURRENT_THEMES="
	<button space-expand=\"true\" space-fill=\"true\" tooltip-text=\" $(gettext 'View current desktop themes') \">
		<label>$(gettext 'Themes')</label>
		<input file>/usr/local/jwmdesk/icons/view.svg</input><height>20</height><width>20</width>
		<action>/usr/local/jwmdesk/current_themes &</action>
	</button>"

# main GUI
S="
<window title=\"$(gettext 'JWM Theme Switcher')\" icon-name=\"theme_switcher\" window-position=\"3\" resizable=\"true\">
	<vbox width-request=\"675\">
		<vbox margin=\"1\" space-expand=\"true\" space-fill=\"true\">
		${HEADER}
		</vbox>
		<hbox height-request=\"387\" space-expand=\"true\" space-fill=\"true\">"
		S=$S"
		<vbox margin=\"1\">
			<vbox scrollable=\"true\">
				<vbox margin=\"2\" homogeneous=\"true\" space-expand=\"false\" space-fill=\"false\">
					${THEMESETGUI}
				</vbox>
			</vbox> 
		</vbox>"
		S=$S"
		</hbox>
		<hbox margin=\"1\" homogeneous=\"true\" space-expand=\"false\" space-fill=\"true\">
			${CURRENT_THEMES}
			<button space-expand=\"true\" space-fill=\"true\" use-underline=\"true\" tooltip-text=\" $(gettext 'Launch JWM Theme Builder') \">
				<label>$(gettext '_Builder')</label>
				<input file>$ICONDIR/edit.svg</input><height>20</height><width>20</width>
				<action>$APPDIR/theme_builder &</action>
			</button>
			<button space-expand=\"true\" space-fill=\"true\" use-underline=\"true\" tooltip-text=\" $(gettext 'Restart JWM Theme Switcher') \">
				<label>$(gettext '_Restart')</label>
				<input file>$ICONDIR/restore.svg</input><height>20</height><width>20</width>
				<action>$APPDIR/theme_switcher &</action>
				<action>exit:quit_now</action>
			</button>
			<button space-expand=\"true\" space-fill=\"true\" use-underline=\"true\">
				<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>"
export THEMESWITCHGUI="$S"

$GTKDIALOG -p THEMESWITCHGUI --styles=/tmp/jwmdesk/gtkrc_xml_info.css

unset THEMESWITCHGUI

rm -f /tmp/jwmtheme_switcher*/* 2>/dev/null

exit 0
