#!/bin/sh
#ptheme_gtk - change gtk-theme
#Copyright - GPL 2010,2014,2015,2016,2017,2021
#Sigmund Berglund, Michael Amadio
#radky 2022: adjust code for JWMdesk compatibility

export TEXTDOMAIN=ptheme
export OUTPUT_CHARSET=UTF-8

# check if X is runnning
[ "`grep -F 'xwin' <<< $(ps -eo pid,command)`" ] && export X_IS_RUNNING=true || export X_IS_RUNNING=false

export APPDIR=/usr/local/jwmdesk
export WORKDIR=$HOME/.ptheme/tmp/ptheme
[ ! -d $WORKDIR ] && mkdir -p $WORKDIR

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

# current theme settings
TMP="`grep 'gtkrc"' $HOME/.gtkrc-2.0 | awk -F '/gtk-2.0' '{print $1}' | cut -d'"' -f2-`" # Standard Puppy gtkrc format
ACTIVE_THEME="`basename "$TMP"`"
[ -z "$ACTIVE_THEME" ] && IFS='"' read C1 ACTIVE_THEME C <<< "$(grep -F 'gtk-theme-name' $HOME/.gtkrc-2.0)" # LXAppearance gtkrc format
echo "$ACTIVE_THEME" > $WORKDIR/ptheme_gtksel

if [ -z "$1" ];then
	THEME="$ACTIVE_THEME"
else
	touch /tmp/ptheme_gtk_cli
fi

IFS='"' read C1 ACTIVE_ICON_THEME C <<< "$(grep -F 'gtk-icon-theme-name' $HOME/.gtkrc-2.0)"
[ -z "$ACTIVE_ICON_THEME" ] && ACTIVE_ICON_THEME="hicolor"
echo "$ACTIVE_ICON_THEME" > $WORKDIR/ptheme_gtkicon

ACTIVE_FONT=$(cat $WORKDIR/ptheme_gtkfont 2>/dev/null)
[ -z "$ACTIVE_FONT" ] && ACTIVE_FONT="Sans 10" && echo "$ACTIVE_FONT" > $WORKDIR/ptheme_gtkfont

ldd $(which gtkdialog) | grep -Fq 'gtk-3' && GTK3=true || GTK3=false

DIRICONS=false # set true to enable diricons (requires ptheme_diricons)

export ACTIVE_THEME ACTIVE_ICON_THEME ACTIVE_FONT GTK3 DIRICONS
export REAL_XDG_CONFIG_HOME="$HOME/.config"

# functions
set_gtk4_theme() {
	if ls /usr/share/icons | grep -q "$1" ; then
		if [ -e $REAL_XDG_CONFIG_HOME/gtk-4.0/settings.ini ]; then
			if grep -q "^gtk-icon-theme-name" $REAL_XDG_CONFIG_HOME/gtk-4.0/settings.ini ;then
				! grep -q "$1" $REAL_XDG_CONFIG_HOME/gtk-4.0/settings.ini &&  \
					sed -i "s/^gtk-icon-theme-name.*/gtk-icon-theme-name = $1/" $REAL_XDG_CONFIG_HOME/gtk-4.0/settings.ini
			else
				echo "gtk-icon-theme-name = $ICON_THEME" >> $REAL_XDG_CONFIG_HOME/gtk-4.0/settings.ini
			fi
		else
			cat << _EOF > $REAL_XDG_CONFIG_HOME/gtk-4.0/settings.ini
[Settings]
gtk-icon-theme-name = $ICON_THEME
_EOF
		fi
	else
		PTHEME_GTK4="$1"
		if [[ $(ls -1Ad /usr/share/themes/"${PTHEME_GTK4}"/gtk-4*) ]]; then
			mkdir -p $REAL_XDG_CONFIG_HOME/gtk-4.0
			cat > $REAL_XDG_CONFIG_HOME/gtk-4.0/settings.ini << _EOF
[Settings]
gtk-theme-name = ${PTHEME_GTK4}
_EOF
		else
			mkdir -p $REAL_XDG_CONFIG_HOME/gtk-4.0
			cat > $REAL_XDG_CONFIG_HOME/gtk-4.0/settings.ini << _EOF
[Settings]
_EOF
		fi
	fi
	if ! grep -qE "gtk-enable-animations" $REAL_XDG_CONFIG_HOME/gtk-4.0/settings.ini ; then
		mkdir -p $REAL_XDG_CONFIG_HOME/gtk-4.0
		cat >> $REAL_XDG_CONFIG_HOME/gtk-4.0/settings.ini << _EOF
gtk-enable-animations = 0
gtk-hint-font-metrics = 1
_EOF
	fi
	if [ $(which install 2>/dev/null) ]; then
		install -D -m 644 $REAL_XDG_CONFIG_HOME/gtk-4.0/settings.ini /etc/gtk-4.0/settings.ini
		install -D -m 600 -o spot -g spot $REAL_XDG_CONFIG_HOME/gtk-4.0/settings.ini /home/spot/.config/gtk-4.0/settings.ini
	fi
}

set_gtk3_theme() {
	# gtk-icon-theme-name
	if ls /usr/share/icons | grep -q "$1" ; then
		if [ -e $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini ]; then
			if grep -q "^gtk-icon-theme-name" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini ;then
				! grep -q "$1" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini &&  \
					sed -i "s/^gtk-icon-theme-name.*/gtk-icon-theme-name = $1/" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini
			else
				echo "gtk-icon-theme-name = $ICON_THEME" >> $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini
			fi
		else
			cat << _EOF > $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini
[Settings]
gtk-icon-theme-name = $ICON_THEME
_EOF
		fi
	# gtk-theme-name and optional settings
	else
		PTHEME_GTK3="$1"
		if [[ $(ls -1Ad /usr/share/themes/"${PTHEME_GTK3}"/gtk-3*) ]]; then
			mkdir -p $REAL_XDG_CONFIG_HOME/gtk-3.0
			# gtk-theme-name
			cat > $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini << _EOF
[Settings]
gtk-theme-name = ${PTHEME_GTK3}
_EOF
			# optional gtk3 settings (user may add/delete/edit: Enable=1 Disable=0)
			cat >> $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini << EOF
gtk-application-prefer-dark-theme = 0
gtk-cursor-theme-size = 0
gtk-button-images = 1
gtk-menu-images = 1
gtk-toolbar-style = GTK_TOOLBAR_BOTH
gtk-toolbar-icon-size = GTK_ICON_SIZE_LARGE_TOOLBAR
gtk-overlay-scrolling = 0
gtk-enable-animations = 0
gtk-enable-event-sounds = 1
gtk-enable-input-feedback-sounds = 1
EOF
		else
			mkdir -p $REAL_XDG_CONFIG_HOME/gtk-3.0
			cat > $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini << _EOF
[Settings]
gtk-toolbar-style = GTK_TOOLBAR_BOTH
gtk-toolbar-icon-size = GTK_ICON_SIZE_LARGE_TOOLBAR
_EOF
		fi
	fi
	if ! grep -qE "gtk-menu-images|gtk-button-images|gtk-enable-animations" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini ; then
		mkdir -p $REAL_XDG_CONFIG_HOME/gtk-3.0
		cat >> $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini << _EOF
gtk-menu-images = 1
gtk-button-images = 1
gtk-enable-animations = 0
_EOF
	fi
	# font configuration (antialiasing, hinting, sub-pixel geometry)
	[ "$GTK3" = "true" ] && set_font_configuration
	# gtk-font-name
	if grep -q "^gtk-font-name" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini ;then
		! grep -q "$FONT" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini &&  \
			sed -i "s/^gtk-font-name.*/gtk-font-name = $FONT/" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini
	else
		echo "gtk-font-name = $FONT" >> $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini
	fi
	if [ $(which install 2>/dev/null) ]; then
		install -D -m 644 $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini /etc/gtk-3.0/settings.ini
		install -D -m 600 -o spot -g spot $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini /home/spot/.config/gtk-3.0/settings.ini
	fi
}

# export -f set_gtk3_theme

set_theme_gtk (){
	# GTK theme
	[ -z "$THEME" ] && THEME="$ACTIVE_THEME" && export THEME
	[ -z "$FONT" ] && FONT="$ACTIVE_FONT" && export FONT
	if [ $X_IS_RUNNING = true -o "$WAYLAND_DISPLAY" ]; then
		if [ "$(which gtk-theme-switch2)" ] ; then
			gtk-theme-switch2 -f "$FONT" "/usr/share/themes/$THEME"
			set_gtk3_theme "$THEME"
			set_gtk4_theme "$THEME"
		elif [ "$(which switch2)" ] ; then
			switch2 -f "$FONT" "/usr/share/themes/$THEME"
			set_gtk3_theme "$THEME"
			set_gtk4_theme "$THEME"
		else
			# from upgrade_ptheme_fix
			export PTHEME_GTK="$THEME"
			cat > $HOME/.gtkrc-2.0 << _EOF
# -- THEME AUTO-WRITTEN BY gtk-theme-switch2 DO NOT EDIT
include "/usr/share/themes/${PTHEME_GTK}/gtk-2.0/gtkrc"

style "user-font"
{
  font_name="${FONT}"
}
widget_class "*" style "user-font"

include "/root/.gtkrc-2.0.mine"

# -- THEME AUTO-WRITTEN BY gtk-theme-switch2 DO NOT EDIT
gtk-theme-name = "${PTHEME_GTK}"
gtk-toolbar-style = GTK_TOOLBAR_BOTH
gtk-toolbar-icon-size = GTK_ICON_SIZE_LARGE_TOOLBAR
_EOF

			set_gtk3_theme "$THEME"
			set_gtk4_theme "$THEME"
		fi
	fi
	# GTK icons
	[ -z "$ICON_THEME" ] && ICON_THEME="$ACTIVE_ICON_THEME" && export ICON_THEME
	if [ "$ICON_THEME" ]; then
		# update GTK icon theme
		sed -i '/gtk-icon-theme-name/d' $HOME/.gtkrc-2.0
		echo "gtk-icon-theme-name = \"$ICON_THEME\"" >> $HOME/.gtkrc-2.0
		set_gtk3_theme "$ICON_THEME"
		set_gtk4_theme "$ICON_THEME"
	fi
	# make Qt4 apps theme match gtk2...
	# see also /etc/profile.d/pup_gtk and /root/.config/Trolltech.conf
	THEREBEFORE="$(grep '^gtk-theme-name' /root/.gtkrc-2.0)"
	if [ "$THEREBEFORE" = "" ]; then
		echo "gtk-theme-name=\"${THEME}\"" >> /root/.gtkrc-2.0
	fi
	[ $(which install 2>/dev/null) ] && install -D -m 644 $HOME/.gtkrc-2.0 /etc/gtk-2.0/gtkrc
}

set_font (){
	REPLY=$(Xdialog --title "$(gettext 'Font attributes')" --buttons-style text --ok-label "$(gettext 'Apply')" --fontsel "$ACTIVE_FONT" 0 0 0 2> $WORKDIR/ptheme_gtkfont )
	if [ "$REPLY" ]; then
		read FONT < $WORKDIR/ptheme_gtkfont
		export FONT
	else
		exit 0
	fi
}

update_theme (){
	set_theme_gtk
	[ -x /usr/local/bin/roxfiler ] && ROX=roxfiler || ROX=rox
	unset THEME # if inherited by ROX-Filer and ptheme_gtk is invoked through the pinboard, we always enter this if
	if pidof ROX-Filer >/dev/null 2>&1; then
		[ $(pidof conky) -a -f $HOME/Startup/conkystart ] && CONKY_ACTIVE=yes
		HOMEDIR=`echo $HOME`
		ROX_OPEN="$(xwininfo -root -tree 2>/dev/null | grep -i 'ROX' | awk '{print $2}' | grep -Evi 'ROX|has|bugfix' | sed -e 's/[\(";:\)]//g' -e "s%\~%$HOMEDIR%g")" # store open rox windows 
		killall ROX-Filer
		sleep 1
		$ROX -p $HOME/Choices/ROX-Filer/PuppyPin
		if [ -f /usr/sbin/ptheme_diricons ]; then
			if [ "$DIRICONS" = "true" ]; then
				ptheme_diricons "$ICON_THEME"
			else
				[ -h "$HOME/.DirIcon" ] && rm -f "$HOME/.DirIcon"
				for dir in Documents Downloads my-documents my-applications network Music Video Images ; do
					[ -h "$HOME/$dir/.DirIcon" ] && rm -f "$HOME/$dir/.DirIcon"
				done
			fi
		fi
		sync
		for d in `echo $ROX_OPEN` ; do
			[ -d $d ] && $ROX $d 2>/dev/null
		done # re-open killed windows
		[ "$CONKY_ACTIVE" = "yes" ] && $HOME/Startup/conkystart &
	fi
}

export -f set_theme_gtk set_gtk3_theme set_gtk4_theme set_font update_theme

if [ "$GTK3" = "true" ]; then
 set_font_configuration (){
	ANTIALIASING=$(cat $WORKDIR/antialiasing 2>/dev/null)
	[ -z "$ANTIALIASING" ] && ANTIALIASING="true"
	[ "$ANTIALIASING" = "true" ] && AA="1" || AA="0"
	if grep -q "^gtk-xft-antialias" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini ;then
		sed -i "s/^gtk-xft-antialias.*/gtk-xft-antialias = $AA/" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini
	else
		echo "gtk-xft-antialias = $AA" >> $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini
	fi

	HINTING=$(cat $WORKDIR/hinting 2>/dev/null)
	[ -z "$HINTING" ] && HINTING="true" 
	[ "$HINTING" = "true" ] && H="1" || H="0"
	if grep -q "^gtk-xft-hinting" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini ;then
		sed -i "s/^gtk-xft-hinting.*/gtk-xft-hinting = $H/" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini
	else
		echo "gtk-xft-hinting = $H" >> $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini
	fi

	HINTING_STYLE=$(cat $WORKDIR/hinting_style 2>/dev/null)
	[ -z "$HINTING_STYLE" ] && HINTING_STYLE="Slight"
	[ "$HINTING_STYLE" = "None" ] && HS="hintnone"
	[ "$HINTING_STYLE" = "Slight" ] && HS="hintslight"
	[ "$HINTING_STYLE" = "Medium" ] && HS="hintmedium"
	[ "$HINTING_STYLE" = "Full" ] && HS="hintfull"
	if grep -q "^gtk-xft-hintstyle" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini ;then
		sed -i "s/^gtk-xft-hintstyle.*/gtk-xft-hintstyle = $HS/" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini
	else
		echo "gtk-xft-hintstyle = $HS" >> $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini
	fi

	SUBPIXEL_GEOMETRY=$(cat $WORKDIR/subpixel_geometry 2>/dev/null)
	[ -z "$SUBPIXEL_GEOMETRY" ] && SUBPIXEL_GEOMETRY="RGB"
	[ "$SUBPIXEL_GEOMETRY" = "None" ] && SPG="none"
	[ "$SUBPIXEL_GEOMETRY" = "RGB" ] && SPG="rgb"
	[ "$SUBPIXEL_GEOMETRY" = "BGR" ] && SPG="bgr"
	[ "$SUBPIXEL_GEOMETRY" = "VRGB" ] && SPG="vrgb"
	[ "$SUBPIXEL_GEOMETRY" = "VBGR" ] && SPG="vbgr"
	if grep -q "^gtk-xft-rgba" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini ;then
		sed -i "s/^gtk-xft-rgba.*/gtk-xft-rgba = $SPG/" $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini
	else
		echo "gtk-xft-rgba = $SPG" >> $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini
	fi

	if [ -f /tmp/font_configuration_splash ];then
		rm -f /tmp/font_configuration_splash
		gtkdialog-splash -close never -placement center -margin 8 -timeout 1 -fontsize large -bg goldenrod -text " $(gettext 'Saving...') " &
	fi
}

 font_configuration (){
	ANTIALIASING=$(grep 'gtk-xft-antialias' $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini 2>/dev/null | cut -d '=' -f2 | awk '{$1=$1};1')
	[ "$ANTIALIASING" = "1" ] && ANTIALIASING="true" || ANTIALIASING="false"
	[ -z "$ANTIALIASING" ] && ANTIALIASING="true"
	echo "$ANTIALIASING" > $WORKDIR/antialiasing
	ACTIVE_ANTIALIASING="$ANTIALIASING"

	HINTING=$(grep 'gtk-xft-hinting' $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini 2>/dev/null | cut -d '=' -f2 | awk '{$1=$1};1')
	[ "$HINTING" = "1" ] && HINTING="true" || HINTING="false"
	[ -z "$HINTING" ] && HINTING="true"
	echo "$HINTING" > $WORKDIR/hinting
	ACTIVE_HINTING="$HINTING"
	[ "$HINTING" = "true" ] && HINTING_STATE="true" || HINTING_STATE="false"

	HINTING_STYLE=$(grep 'gtk-xft-hintstyle' $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini 2>/dev/null | cut -d '=' -f2 | awk '{$1=$1};1')
	[ "$HINTING_STYLE" = "hintnone" ] && HINTING_STYLE="None"
	[ "$HINTING_STYLE" = "hintslight" ] && HINTING_STYLE="Slight"
	[ "$HINTING_STYLE" = "hintmedium" ] && HINTING_STYLE="Medium"
	[ "$HINTING_STYLE" = "hintfull" ] && HINTING_STYLE="Full"
	[ -z "$HINTING_STYLE" ] && HINTING_STYLE="Slight"
	echo "$HINTING_STYLE" > $WORKDIR/hinting_style
	ACTIVE_HINTING_STYLE="$HINTING_STYLE"

	SUBPIXEL_GEOMETRY=$(grep 'gtk-xft-rgba' $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini 2>/dev/null | cut -d '=' -f2 | awk '{$1=$1};1')
	[ "$SUBPIXEL_GEOMETRY" = "none" ] && SUBPIXEL_GEOMETRY="None"
	[ "$SUBPIXEL_GEOMETRY" = "rgb" ] && SUBPIXEL_GEOMETRY="RGB"
	[ "$SUBPIXEL_GEOMETRY" = "bgr" ] && SUBPIXEL_GEOMETRY="BGR"
	[ "$SUBPIXEL_GEOMETRY" = "vrgb" ] && SUBPIXEL_GEOMETRY="VRGB"
	[ "$SUBPIXEL_GEOMETRY" = "vbgr" ] && SUBPIXEL_GEOMETRY="VBGR"
	[ -z "$SUBPIXEL_GEOMETRY" ] && SUBPIXEL_GEOMETRY="RGB"
	echo "$SUBPIXEL_GEOMETRY" > $WORKDIR/subpixel_geometry
	ACTIVE_SUBPIXEL_GEOMETRY="$SUBPIXEL_GEOMETRY"

	export -f ACTIVE_ANTIALIASING ACTIVE_HINTING ACTIVE_HINTING_STYLE ACTIVE_SUBPIXEL_GEOMETRY

	export FC='
<window title="'$(gettext 'Font configuration')'" icon-name="gtk-properties" resizable="false" window-position="1">
 <vbox>
 <vbox margin="5">
   <hbox space-expand="true" space-fill="true" homogeneous="true">
     <text xalign="0" use-markup="true"><label>"<b><span size='"'medium'"'>'$(gettext 'Antialiasing')'</span></b>"</label></text>
   </hbox>
   <hbox space-expand="true" space-fill="true" homogeneous="true">
     <checkbox>
      <label>'$(gettext 'Enable antialiasing')'</label>
      <variable>ANTIALIASING</variable>
      <input>cat '$WORKDIR'/antialiasing</input>
      <action>if true echo true > '$WORKDIR'/antialiasing</action>
      <action>if false echo false > '$WORKDIR'/antialiasing</action>
     </checkbox>
   </hbox>

   <text space-expand="false" space-fill="false"><label>" "</label></text>

   <hbox space-expand="true" space-fill="true" homogeneous="true">
     <text xalign="0" use-markup="true"><label>"<b><span size='"'medium'"'>'$(gettext 'Hinting')'</span></b>"</label></text>
   </hbox>
   <hbox space-expand="true" space-fill="true" homogeneous="true">
     <checkbox>
      <label>'$(gettext 'Enable hinting')'</label>
      <variable>HINTING</variable>
      <input>cat '$WORKDIR'/hinting</input>
      <action>if true echo true > '$WORKDIR'/hinting</action>
      <action>if true echo true > '$WORKDIR'/hinting_state</action>
      <action>if true enable:HINTING_STYLE</action>
      <action>if false echo false > '$WORKDIR'/hinting</action>
      <action>if false echo false > '$WORKDIR'/hinting_state</action>
      <action>if false disable:HINTING_STYLE</action>
     </checkbox>
   </hbox>

   <hbox space-expand="true" space-fill="true" homogeneous="true">
    <text xalign="0"><label>'$(gettext 'Hinting style')'</label></text>
     <comboboxtext sensitive="'$HINTING_STATE'" wrap-width="1" width-request="120">
      <variable>HINTING_STYLE</variable>
      <input>cat '$WORKDIR'/hinting_style</input>
      <item>None</item>
      <item>Slight</item>
      <item>Medium</item>
      <item>Full</item>
      <action>echo "$HINTING_STYLE" > '$WORKDIR'/hinting_style</action>
     </comboboxtext>
   </hbox>

   <text space-expand="false" space-fill="false"><label>" "</label></text>

   <hbox space-expand="true" space-fill="true" homogeneous="true">
     <text xalign="0" use-markup="true"><label>"<b><span size='"'medium'"'>'$(gettext 'Geometry')'</span></b>"</label></text>
   </hbox>
   <hbox space-expand="true" space-fill="true" homogeneous="true">
    <text xalign="0"><label>'$(gettext 'Sub-pixel geometry')' </label></text>
     <comboboxtext wrap-width="1" width-request="120">
      <variable>SUBPIXEL_GEOMETRY</variable>
      <input>cat '$WORKDIR'/subpixel_geometry</input>
      <item>None</item>
      <item>RGB</item>
      <item>BGR</item>
      <item>VRGB</item>
      <item>VBGR</item>
      <action>echo "$SUBPIXEL_GEOMETRY" > '$WORKDIR'/subpixel_geometry</action>
     </comboboxtext>
   </hbox>
 </vbox>

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

   <hbox space-expand="false" space-fill="true" homogeneous="true">
    <button space-expand="true" space-fill="true" tooltip-text=" '"$(gettext 'Exit without change')"' ">
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <label>'$(gettext 'Cancel')'</label>
      <action>echo "$ACTIVE_ANTIALIASING" > '$WORKDIR'/antialiasing</action>
      <action>echo "$ACTIVE_HINTING" > '$WORKDIR'/hinting</action>
      <action>echo "$ACTIVE_HINTING_STYLE" > '$WORKDIR'/hinting_style</action>
      <action>echo "$ACTIVE_SUBPIXEL_GEOMETRY" > '$WORKDIR'/subpixel_geometry</action>
      <action>EXIT:exit</action>
    </button>
    <button space-expand="true" space-fill="true" tooltip-text=" '"$(gettext 'Apply the current selections')"' ">
      '"`/usr/lib/gtkdialog/xml_button-icon apply`"'
      <label>'$(gettext 'Apply')'</label>
      <action>touch /tmp/font_configuration_splash</action>
      <action>set_font_configuration</action>
      <action>EXIT:exit</action>
    </button>
   </hbox>
 </vbox>
</window>'

$GTKDIALOG -p FC --styles=/tmp/jwmdesk/gtkrc_ptheme.css 2> /dev/null
unset FC
}

 export -f set_font_configuration font_configuration

fi

# parameters
while [ $# != 0 ]; do
	I=1
	while [ $I -le `echo $# | wc -c` ]; do
		case $1 in
			-t) export THEME="$2"
				[ ! "$THEME" ] && export THEME="$ACTIVE_THEME"
				shift;;
			-f) export FONT="$2"; shift;;
			-i) export ICON_THEME="$2"
				shift;;
			-h|--help)
echo 'Usage: ptheme_gtk [OPTIONS]

Options
  -f FONT       Set font for theme
  -i ICON_THEME Set icon-theme for theme
  -t THEME      Set theme without open gui 
  -h            Show this help message'; exit;;
		esac
		shift
		I=$[$I+1]
	done
done

if [ "$THEME" ]; then
	if [ -f /tmp/ptheme_gtk_cli ]; then
		rm -f /tmp/ptheme_gtk_cli
		update_theme
		exit 0
	elif [ -f /tmp/ptheme_gtk_gui ]; then
		rm -f /tmp/ptheme_gtk_gui
		update_theme
	fi
fi

# GTK themes
if [ -d $HOME/.themes ]; then # alternate theme directory
	find -L /usr/share/themes -type l -delete 2>/dev/null #remove broken symlinks
	XTRA_GTK_THEMES=$(ls -1A $HOME/.themes/ 2>/dev/null)
	if [ "$XTRA_GTK_THEMES" ]; then
		for t in $XTRA_GTK_THEMES; do
			[ ! -d /usr/share/themes/"$t" ] && ln -snf $HOME/.themes/"$t" /usr/share/themes/
		done
	fi
fi

if [ "$GTK3" = "true" ]; then
	echo '@import url("/tmp/jwmdesk/gtkrc_xml_info.css");
menuitem {
	min-width: 9rem;
}' > /tmp/jwmdesk/gtkrc_ptheme.css

current_gtk_settings (){
	Xdialog --title "$(gettext 'GTK3 settings')" --fixed-font --left --no-wrap --buttons-style text --ok-label "$(gettext 'Close')" --msgbox "$(cat $REAL_XDG_CONFIG_HOME/gtk-3.0/settings.ini | grep '^gtk-' | sed 's/^gtk-/• gtk-/g')" 0 0 &
}
export -f current_gtk_settings

	GTK_SETTINGS='
        <button width-request="30" tooltip-text=" '"$(gettext 'View GTK settings')"' ">
          <input file stock="gtk-dialog-info"></input>
          <action>current_gtk_settings &</action>
        </button>'
	GTK_ICONS='
        <button width-request="30" tooltip-text=" '"$(gettext 'View GTK icons')"' ">
          <input file stock="gtk-about"></input>
          <action>'$APPDIR'/gtk.icons &</action>
        </button>'
	FONT_CONFIGURATION='
        <button width-request="30" tooltip-text=" '"$(gettext 'Change font configuration')"' ">
          <input file stock="gtk-properties"></input>
          <action>font_configuration &</action>
        </button>'
	MORE_THEMES=''
	TXT='(gtk3+)'
	ls -1Ad /usr/share/themes/*/gtk-3* 2>/dev/null | sed -e 's%/usr/share/themes/%%g' -e 's%/gtk-3.*%%g' | sort -u > $WORKDIR/ptheme_gtkthemes

else

current_gtk_settings (){
	Xdialog --title "$(gettext 'GTK2 settings')" --fixed-font --left --no-wrap --buttons-style text --ok-label "$(gettext 'Close')" --msgbox "$(cat $HOME/.gtkrc-2.0 | grep -v '^#')" 0 0 &
}
export -f current_gtk_settings

	GTK_SETTINGS='
        <button width-request="30" tooltip-text=" '"$(gettext 'View GTK settings')"' ">
          <input file stock="gtk-dialog-info"></input>
          <action>current_gtk_settings &</action>
        </button>'
	GTK_ICONS='
        <button width-request="30" tooltip-text=" '"$(gettext 'View GTK icons')"' ">
          <input file stock="gtk-about"></input>
          <action>'$APPDIR'/gtk.icons &</action>
        </button>'
	FONT_CONFIGURATION=''
	MORE_THEMES='
    <button space-expand="true" space-fill="true" tooltip-text=" '"$(gettext 'Online GTK themes')"' ">
      '"`/usr/lib/gtkdialog/xml_button-icon internet.svg`"'
      <label>'$(gettext 'More themes')'</label>
      <action>defaulthtmlviewer https://oldforum.puppylinux.com/viewtopic.php?t=30075 &</action>
    </button>'
	TXT='(gtk2)'
	ls -1Ad /usr/share/themes/*/gtk-2* 2>/dev/null | sed -e 's%/usr/share/themes/%%g' -e 's%/gtk-2.*%%g' | sort -u > $WORKDIR/ptheme_gtkthemes
fi
while read I; do ITEMS="$ITEMS<item>$I</item>"; done < $WORKDIR/ptheme_gtkthemes

# GTK icon themes : problem here, where sometimes the name in index.theme doesn't match the directory
while read I; do
	MENUITEMS_ICONS_GTK="$MENUITEMS_ICONS_GTK<menuitem stock=\"gtk-directory\" label=\"$I\"><action>echo $I > '$WORKDIR'/ptheme_gtkicon</action><action>refresh:ICON_THEME</action></menuitem>"
done <<<  "$(find /usr/share/icons -maxdepth 3 -type d -name "32*" | grep -iv 'tango' | cut -d '/' -f 5 | sort -u)"

# set title-bar icon
[ ! -f /usr/share/icons/hicolor/48x48/apps/gtk_themes.svg ] && \
ln -sf $APPDIR/icons/gtk_themes.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 # HEADER
ICON=$APPDIR/icons/gtk_themes.svg
ICON_HEIGHT=70
MSG_1="<b><span size='"'x-large'"'>$(gettext "GTK Theme Switcher")</span></b>"
MSG_2="<b>$(gettext "Change appearance of program widgets")</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>"

# main dialog
export pThemeGTK='
<window title="GTK '$(gettext 'theme switcher')'" icon-name="gtk_themes" window-position="1">
<vbox space-expand="true" space-fill="true">
  <vbox space-expand="false" space-fill="false">
  '$HEADER'
  </vbox>

  <vbox space-expand="false" space-fill="true">
    <hbox space-expand="false" space-fill="true">
     <menubar space-expand="true" space-fill="true">
      <menu>
        <menuitem><label>'$(gettext 'Simple item')'</label></menuitem>
        <menuitem><label>'$(gettext 'Simple item')'</label></menuitem>
        <separator></separator>
        <menuitem stock="gtk-convert" label="'$(gettext 'With image')'"></menuitem>
        <menuitem stock="gtk-convert" label="'$(gettext 'With image')'"></menuitem>
        <label>'$(gettext 'Menu')'</label>
      </menu>
     </menubar>
    </hbox>
    <hbox space-expand="false" space-fill="true">
      <checkbox><default>true</default><label>'$(gettext 'Check')' 1</label></checkbox>
      <checkbox><label>'$(gettext 'Check')' 2</label></checkbox>
      <text space-expand="false" space-fill="false"><label>"  "</label></text>
      <radiobutton space-expand="false" space-fill="false"><label>'$(gettext 'Radio')' 1</label></radiobutton>
      <radiobutton space-expand="false" space-fill="false"><label>'$(gettext 'Radio')' 2</label></radiobutton>
      <radiobutton space-expand="false" space-fill="false"><label>'$(gettext 'Radio')' 3</label></radiobutton>
      <text space-expand="true" space-fill="true"><label>" "</label></text>
    </hbox>
    <hbox space-expand="false" space-fill="true">
      <button space-expand="true" space-fill="true"><label>" '$(gettext 'Button')' "</label><input file stock="gtk-execute"></input><action signal="button-release-event">null</action></button>
      <text space-expand="false" space-fill="false"><label>"  "</label></text>
      <checkbox space-expand="true" space-fill="true" draw_indicator="false"><default>true</default><label>" '$(gettext 'Toggle')' 1 "</label></checkbox>
      <checkbox space-expand="true" space-fill="true" draw_indicator="false"><label>" '$(gettext 'Toggle')' 2 "</label></checkbox>
      <checkbox space-expand="true" space-fill="true" draw_indicator="false"><label>" '$(gettext 'Toggle')' 3 "</label></checkbox>
      <text space-expand="false" space-fill="false"><label>"  "</label></text>
      <button space-expand="true" space-fill="true"><label>" '$(gettext 'Disabled')' "</label><input file stock="gtk-close"></input><visible>disabled</visible></button>
    </hbox>
    <hbox space-expand="false" space-fill="true">
      <hscale space-expand="true" space-fill="true" width-request="200" value-pos="1"><action signal="button-release-event">null</action><input>echo 50; echo "(50%)"</input></hscale>
      <comboboxtext space-expand="false" space-fill="false">
       <item>'$(gettext 'item')' A </item>
       <item>'$(gettext 'item')' B </item>
       <item>'$(gettext 'item')' C </item>
      </comboboxtext>
    </hbox>
  </vbox>

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

  <vbox space-expand="true" space-fill="true">
      <tree rules_hint="true" headers-clickable="false">
        <label>'$(gettext 'Themes')'  '$TXT'</label>
        <variable>THEME</variable>
        <height>152</height><width>50</width>
        '$ITEMS'
        <action signal="button-release-event">echo "$THEME" > '$WORKDIR'/ptheme_gtksel</action>
        <action signal="button-release-event">refresh:SELECTED_THEME</action>
        <action signal="row-activated">echo "$THEME" > '$WORKDIR'/ptheme_gtksel</action>
        <action signal="row-activated">refresh:SELECTED_THEME</action>
      </tree>
  </vbox>

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

  <vbox space-expand="false" space-fill="false">
      <hbox>
      <text xalign="0" width-request="200" space-expand="false" space-fill="false"><label>"  '$(gettext 'GTK theme')'"</label></text>
      <entry editable="false" width-request="180" space-expand="true" space-fill="true">
        <variable>SELECTED_THEME</variable>
        <input>cat '$WORKDIR'/ptheme_gtksel</input>
      </entry>
      '$GTK_SETTINGS'
      </hbox>
      <hbox>
        <menubar width-request="200" space-expand="false" space-fill="false">
          <menu label="▼ '$(gettext 'GTK icon')'" width-request="180">
            '$MENUITEMS_ICONS_GTK'
            <menuitem label="">
            </menuitem>
          </menu>
        </menubar>
        <entry editable="false" width-request="180" space-expand="true" space-fill="true">
          <variable>ICON_THEME</variable>
          <input>cat '$WORKDIR'/ptheme_gtkicon</input>
        </entry>
        '$GTK_ICONS'
      </hbox>
      <hbox>
      <text xalign="0" width-request="200" space-expand="false" space-fill="false"><label>"  '$(gettext 'Font')'"</label></text>
        <entry editable="false" secondary-icon-stock="gtk-clear">
          <variable>FONT</variable>
          <input>cat '$WORKDIR'/ptheme_gtkfont</input>
          <action>echo "$FONT" > '$WORKDIR'/ptheme_gtkfont</action>
          <action signal="secondary-icon-release">echo > '$WORKDIR'/ptheme_gtkfont; export FONT=""; set_theme_gtk</action>
          <action signal="secondary-icon-release">refresh:FONT</action>
        </entry>
        <button width-request="30" tooltip-text=" '"$(gettext 'Change font attributes')"' ">
          <input file stock="gtk-bold"></input>
          <action>set_font</action>
          <action>refresh:FONT</action>
        </button>
        '$FONT_CONFIGURATION'
      </hbox>
  </vbox>

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

  <hbox space-expand="false" space-fill="false">
    '$MORE_THEMES'
    <button space-expand="true" space-fill="true" tooltip-text=" '"$(gettext 'Current themes')"' ">
      <input file>'$APPDIR'/icons/view.svg</input><height>20</height><width>20</width>
      <label>" '$(gettext 'Themes')' "</label>
      <action>'$APPDIR'/current_themes &</action>
    </button>
    <button space-expand="true" space-fill="true" tooltip-text=" '"$(gettext 'Exit without change')"' ">
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <label>'$(gettext 'Cancel')'</label>
      <action>echo "$ACTIVE_THEME" > '$WORKDIR'/ptheme_gtksel</action>
      <action>echo "$ACTIVE_ICON_THEME" > '$WORKDIR'/ptheme_gtkicon</action>
      <action>echo "$ACTIVE_FONT" > '$WORKDIR'/ptheme_gtkfont</action>
      <action>EXIT:exit</action>
    </button>
    <button space-expand="true" space-fill="true" tooltip-text=" '"$(gettext 'Apply the current selections')"' ">
      '"`/usr/lib/gtkdialog/xml_button-icon apply`"'
      <label>'$(gettext 'Apply')'</label>
      <action>set_theme_gtk</action>
      <action>touch /tmp/ptheme_gtk_gui</action>
      <action>'$APPDIR'/ptheme_gtk &</action>
      <action>EXIT:exit</action>
    </button>
    '"`/usr/lib/gtkdialog/xml_scalegrip`"'
  </hbox>
</vbox>
<action signal="key-press-event" condition="command_is_true([ $KEY_SYM = Escape ] && echo true )">exit:EXIT</action>
</window>'

$GTKDIALOG -p pThemeGTK --styles=/tmp/jwmdesk/gtkrc_ptheme.css 2> /dev/null

unset pThemeGTK

exit 0
