#!/bin/sh
#(c) Copyright Nov 2009 Barry Kauler puppylinux.com
#2009 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#code taken out of the icon_switcher gui app.
#i created this script for desk_icon_theme_jq8flat-1 pet pkg, called from pinstall.sh
#w002 first creation. script is called from /etc/rc.d/rc.update.
#w477 record name of theme in /etc/desktop_icon_theme
#110721 new standardised icons to show when mounted.
#110803 workaround for 'pngoverlay' needs X runnning, also avoid cpu overhead at first startup.
#110826 do not need 110803 workaround. rc.update no longer calls this script. (in fact, this script is not being used at all)
#110912 modify jwm to use menu icon 'start-button.png' if present.

. /etc/rc.d/PUPSTATE #110803
#110826 no longer needed...
##DONEINWOOF 110803: '[ "$PUPMODE" = "5" ] && exit' 3builddistro may insert this line, to disable icon_switcher_cli

NEWTHEME="$1"

[ "$NEWTHEME" = "" ] && exit
[ ! -d /usr/local/lib/X11/themes/${NEWTHEME} ] && exit

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 2> /dev/null
  pngtopnm $ONEPNG | pnmscale -reduce 2 2>/dev/null | 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

if [ "$DISPLAY" ];then #110726 pngoverlay does not work when X not running. --quick fix, perform this operation in woof in 3builddistro.
 #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
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
 
#w477 record current theme...
echo -n "$NEWTHEME" > /etc/desktop_icon_theme

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

###END###
