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

#  <text><label>\"${CHARPAD}\"</label></text>

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>
  <pixmap><input file>/tmp/pis-${ONENAME}-folder.png</input></pixmap>
  <pixmap><input file>/tmp/pis-${ONENAME}-www.png</input></pixmap>
  <pixmap><input file>/tmp/pis-${ONENAME}-word.png</input></pixmap>
  <pixmap><input file>/tmp/pis-${ONENAME}-spread.png</input></pixmap>
  <vbox>
   <pixmap><input file>/usr/local/lib/X11/pixmaps/invisible96x8.png</input></pixmap>
   <button><label>${ONENAME}</label></button>
  </vbox>
 </hbox>
"

done

export ICONSWITCHGUI="
<window title=\"$(gettext 'Puppy Icon Switcher')\" icon-name=\"gtk-convert\">
 <vbox>
  <text><label>$(gettext 'Choose the icon-set for desktop and menu')</label></text>

  ${ICONSETSGUI}
  
  <hbox>
   <button><input file stock=\"gtk-quit\"></input><label>$(gettext 'QUIT')</label></button>
  </hbox>
 </vbox>
</window>"

echo "$ICONSWITCHGUI" > /tmp/iconswitchgui
#echo "$ICONSWITCHGUI" | gtkdialog3 --stdin
RETSTUFF="`gtkdialog3 --program=ICONSWITCHGUI`"

eval "$RETSTUFF"

NEWTHEME="$EXIT"
#[ "`echo "$EXIT" | grep 'theme$'`" != "" ] && NEWTHEME="`echo "$EXIT" | head -n 1`"
[ "$NEWTHEME" = "" ] && exit
[ ! -d /usr/local/lib/X11/themes/${NEWTHEME} ] && exit

if [ "$NEWTHEME" != "" ];then
 for ONESVG in `find /usr/local/lib/X11/themes/${NEWTHEME}/ -maxdepth 1 -type f -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
  #rsvg-convert -w 24 -h 24 -o /tmp/${ONEBASENAME}24.png $ONESVG
  ##if svg has transparency, JWM does not render properly in x16 bit color, screen out...
  #pngtopnm -mix /tmp/${ONEBASENAME}24.png | pnmtopng > /usr/local/lib/X11/pixmaps/${ONEBASENAME}24.png
  rsvg-convert -w 24 -h 24 -o /usr/local/lib/X11/pixmaps/${ONEBASENAME}24.png $ONESVG
 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
  xONEBASENAME="`basename $ONEBASENAME 48`"
  if [ "$xONEBASENAME" != "$ONEBASENAME" ];then #scale to 24 pixels...
   pngtopnm -alpha $ONEPNG > /tmp/temp.pbm #separate transparency channel.
   pnmscale -reduce 2 /tmp/temp.pbm > /tmp/temp2.pbm
   pngtopnm $ONEPNG | pnmscale -reduce 2 | pnmtopng -alpha=/tmp/temp2.pbm > /usr/local/lib/X11/pixmaps/${xONEBASENAME}24.png
   touch -m /usr/local/lib/X11/pixmaps/${xONEBASENAME}24.png #v3.98
  fi
 done
 
  #110721 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
 
 #110912 modify jwm to use menu icon 'start-button.png' if present...
 #note, similar code also in 3builddistro in Woof.
 if [ -f /root/.jwmrc-tray ];then
  if [ -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 'start-button\.png' /root/.jwmrc-tray`" = "" ];then
     #the default is to use 'mini-dog.xpm' with text "Menu"...
     PTN1='s%label="Menu" icon="mini-dog\.xpm"%icon="start-button.png"%'
     sed -i -e "$PTN1" /root/.jwmrc-tray
    fi
   else
    #chosen theme does not have start-button.png, so go back to the mini-dog.xpm...
    PTN2='s%icon="start-button\.png"%label="Menu" icon="mini-dog.xpm"%'
    sed -i -e "$PTN2" /root/.jwmrc-tray
   fi
  fi
 fi
 
# export ENDGUI="
#<window title=\"Puppy Icon Switcher\" icon-name=\"gtk-convert\">
# <vbox>
#  <text><label>\"You have chosen the '${NEWTHEME}' theme.
#These are icons for the desktop and top-level of menu.
#However, you need to restart X to see the change
#(see 'Restart X server' in 'Shutdown' menu)\"</label></text>
#  <hbox>
#   <button><input file stock=\"gtk-quit\"></input><label>OK</label></button>
#  </hbox>
# </vbox>
#</window>"
# gtkdialog3 --program=ENDGUI

 #v4.00 HairyWill restart rox and jwm immediately...
 export ENDGUI="
<window title=\"$(gettext 'Puppy Icon Switcher')\" icon-name=\"gtk-convert\">
 <vbox>
  <text><label>\"`eval_gettext \"You have chosen the '\\\${NEWTHEME}' theme.
These are icons for the desktop and top-level of menu.
Click OK button and you will see them...\"`\"</label></text>
  <hbox>
   <button><input file stock=\"gtk-quit\"></input><label>OK</label></button>
  </hbox>
 </vbox>
</window>"
 gtkdialog3 --program=ENDGUI
 
 #w477 record current theme...
 echo -n "$NEWTHEME" > /etc/desktop_icon_theme

 rox -p=/root/Choices/ROX-Filer/PuppyPin
 fixmenus #100411
 jwm -restart

fi


###END###
