#!/bin/sh
#(c) Copyright Nov 2007 Barry Kauler www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#written dec 2007 for Puppy Linux.
#v3.98 BK: fix unionfs saving problem when newer file in pup_save.
#v4.00 HairyWill: restart rox and jwm immediately.
#w477 record name of theme in /etc/desktop_icon_theme
#100411 fbpanel menu did not update.
#110721 new standardised icons to show when mounted.
#110912 modify jwm to use menu icon 'start-button.png' if present.
#120201 rodin.s: internationalized

export TEXTDOMAIN=icon_switcher
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
eval_gettext (){
	local myMESSAGE=$(gettext "$1")
	eval echo \"$myMESSAGE\"
}
export LANGORG=$LANG

func_switch (){
	NEWTHEME="$1"
	[ "$NEWTHEME" = "" ] && exit
	[ ! -d /usr/local/lib/X11/themes/${NEWTHEME} ] && exit
	#show splash if running in gui mode
	if [ "$UPDATE" = "true" ]; then
		/usr/lib/gtkdialog/box_splash -close never -fontsize large -icon_width 60 -icon /usr/share/pixmaps/puppy/puppy_config.svg -text "$(gettext 'Changing theme')" &
		XPID=$!
	fi
	#---
	for ONESVG in `find /usr/local/lib/X11/themes/${NEWTHEME}/ -maxdepth 1 -name \*.svg | tr '\n' ' '`; do
		ONEBASENAME="`basename $ONESVG .svg`"
		rm -f /usr/local/lib/X11/pixmaps/${ONEBASENAME}48.png
		rsvg-convert -w 48 -h 48 -o /usr/local/lib/X11/pixmaps/${ONEBASENAME}48.png $ONESVG
		rm -f /usr/local/lib/X11/pixmaps/${ONEBASENAME}24.png
	done
	for ONEPNG in `find /usr/local/lib/X11/themes/${NEWTHEME}/ -maxdepth 1 -type f -name \*.png | tr '\n' ' '`; do
		ONEBASENAME="`basename $ONEPNG .png`"
		cp -f $ONEPNG /usr/local/lib/X11/pixmaps/
		touch -m /usr/local/lib/X11/pixmaps/${ONEBASENAME}.png #v3.98
	done
 
	#new standardised icons to show when mounted...
	if [ "`which pngoverlay`" -a -e /usr/local/lib/X11/pixmaps/closebox_outline48.png ];then #overlay app created by vovchik (see source /usr/sbin/pngoverlay.bac)
	rm -f /usr/local/lib/X11/pixmaps/*_mntd*.png
	cd /usr/local/lib/X11/pixmaps
	cp -f /usr/sbin/pngoverlay /usr/local/lib/X11/pixmaps/ #dumb, but needs to be in same folder.
	for ONEDRV in card drive floppy optical usbdrv; do
		./pngoverlay ${ONEDRV}48.png closebox_outline48.png ${ONEDRV}_mntd48.png #background foreground output.
		./pngoverlay ${ONEDRV}48.png locked_outline48.png ${ONEDRV}_mntd_boot48.png
	done
	rm -f /usr/local/lib/X11/pixmaps/pngoverlay
	fi
 
	#modify jwm to use menu icon 'start-button.png' if present...
	#note, similar code also in 3builddistro in Woof.
	if [ -f /root/.jwmrc-tray ] && [ -f /usr/local/lib/X11/${NEWTHEME}/start-button.png ]; then #chosen theme has menu icon.
		if [ -f /usr/local/lib/X11/pixmaps/start-button.png ];then #precaution.
			if [ ! "`grep -F 'start-button.png' /root/.jwmrc-tray`" ]; then
				#the default is to use 'mini-dog.xpm' with text "Menu"...
				sed -i 's%label="Menu" icon="puppy.svg"%icon="start-button.png"%' $HOME/.jwmrc-tray
			fi
		else
			#chosen theme does not have start-button.png, so go back to the mini-dog.xpm...
			sed -i 's%icon="start-button.png"%label="Menu" icon="puppy.svg"%' $HOME/.jwmrc-tray
		fi
	fi

	#record current theme...
	echo -n "$NEWTHEME" > /etc/desktop_icon_theme
	if [ "$UPDATE" = "true" ]; then
		#restart rox and jwm immediately...
		rox -p=/root/Choices/ROX-Filer/PuppyPin
		jwm -restart
		#kill splash
		kill $XPID
	fi
}
export -f func_switch

#parameters
while [ $# != 0 ]; do
	I=1
	while [ $I -le `echo $# | wc -c` ]; do 
		case $1 in
			-a) NEWTHEME="$2"; shift;;
			-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

if [ "$NEWTHEME" ]; then
	func_switch "$NEWTHEME"
else
	GUI=true
	UPDATE=true
	ICONSETSGUI=""
	for ONEDIR in `find /usr/local/lib/X11/themes/ -mindepth 1 -maxdepth 1 -type d | tr '\n' ' '`; do
		ONENAME="`basename $ONEDIR`"
		[ -f $ONEDIR/folder.svg ] && rsvg-convert -w 48 -h 48 -o /tmp/pis-${ONENAME}-folder.png $ONEDIR/folder.svg
		[ -f $ONEDIR/folder48.png ] && cp -f $ONEDIR/folder48.png /tmp/pis-${ONENAME}-folder.png
		[ -f $ONEDIR/www.svg ] && rsvg-convert -w 48 -h 48 -o /tmp/pis-${ONENAME}-www.png $ONEDIR/www.svg
		[ -f $ONEDIR/www48.png ] && cp -f $ONEDIR/www48.png /tmp/pis-${ONENAME}-www.png
		[ -f $ONEDIR/word.svg ] && rsvg-convert -w 48 -h 48 -o /tmp/pis-${ONENAME}-word.png $ONEDIR/word.svg
		[ -f $ONEDIR/word48.png ] && cp -f $ONEDIR/word48.png /tmp/pis-${ONENAME}-word.png
		[ -f $ONEDIR/spread.svg ] && rsvg-convert -w 48 -h 48 -o /tmp/pis-${ONENAME}-spread.png $ONEDIR/spread.svg
		[ -f $ONEDIR/spread48.png ] && cp -f $ONEDIR/spread48.png /tmp/pis-${ONENAME}-spread.png
		ICONSETSGUI=$ICONSETSGUI'
		<hbox space-expand="true" space-fill="true">
		  <hbox border-width="8" spacing="20" space-expand="false" space-fill="false">
		    <pixmap space-expand="false" space-fill="false"><input file>/tmp/pis-'${ONENAME}'-folder.png</input></pixmap>
		    <pixmap space-expand="false" space-fill="false"><input file>/tmp/pis-'${ONENAME}'-www.png</input></pixmap>
		    <pixmap space-expand="false" space-fill="false"><input file>/tmp/pis-'${ONENAME}'-word.png</input></pixmap>
		    <pixmap space-expand="false" space-fill="false"><input file>/tmp/pis-'${ONENAME}'-spread.png</input></pixmap>
		  </hbox>
		  <text width-request="20" space-expand="false" space-fill="false"><label>""</label></text>
		  <vbox space-expand="true" space-fill="true">
		    <text height-request="1" space-expand="true" space-fill="true"><label>""</label></text>
		    <button space-expand="false" space-fill="false">
		      <label>'${ONENAME}'</label>
		      <action>export UPDATE=true; func_switch "'${ONENAME}'"</action>
		    </button>
		    <text height-request="10" space-expand="false" space-fill="false"><label>""</label></text>
		  </vbox>
		</hbox>'
	done

	S='
	<window title="'$(gettext 'Puppy Icon Switcher')'" icon-name="gtk-convert" height-request="500" width-request="520">
	<vbox>
	  '"`/usr/lib/gtkdialog/xml_info fixed "desktop_icons.svg" 60 "$(gettext 'The Puppy icon switcher sets icons for both the <b>desktop</b> and the <b>menu</b>')"`"' 
	  <vbox space-expand="true" space-fill="true">
	    <vbox scrollable="true">
	      <eventbox name="bg" above-child="false" visible-window="true" space-expand="true" space-fill="true">
	        <vbox border-width="20" space-expand="false" space-fill="false">
	          '${ICONSETSGUI}'
	        </vbox>
	      </eventbox>
	    </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=31059 &</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 quit`"'
	      <label>'$(gettext 'Quit')'</label>
	    </button>
	    '"`/usr/lib/gtkdialog/xml_scalegrip`"'
	  </hbox>
	</vbox>
	</window>'
	export ICONSWITCHGUI="$S"
	
	#set background pic
	BG_FILE="`cat $HOME/.config/wallpaper/bg_img`"
	[ ! "$BG_FILE" ] && BG_FILE="`grep -F '<backdrop' $HOME/.config/rox.sourceforge.net/ROX-Filer/pb_default | cut -d'>' -f2 | cut -d'<' -f1`"
	[ ! "$BG_FILE" ] && BG_FILE="`cat $HOME/Choices/ROX-Filer/PuppyPin | grep '<backdrop ' | cut -f 2 -d '>' | cut -f 1 -d '<'`"
	BG_PATH="`dirname "$BG_FILE"`"
	BG_NAME="`basename "$BG_FILE"`"
	echo '
	pixmap_path "'$BG_PATH'"
	style "bg" { bg_pixmap[NORMAL] = "'$BG_NAME'" }
	widget "*bg*" style "bg"' > /tmp/gtkrc_bg
	export GTK2_RC_FILES=/tmp/gtkrc_bg:/root/.gtkrc-2.0

	. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
	gtkdialog -p ICONSWITCHGUI
fi
