#!/bin/sh
#LGPL Barry Kauler 2006,2007 www.puppylinux.com
#v555 pup files renamed to woofr555.sfs, woofsave.2fs.
#w001 /etc/networkmodules generated (previously done in createpuppy, Unleashed).
#w002 run gtk-update-icon-cache on hicolor icons.
#w002 run icon_switcher_cli for default desktop icon set JQ8flat.
#w007 if sfs layer with extra modules added or removed, need to run depmod.
#w012 updated for new standardised pkg database format.
#w020 april2009: fix for add/remove zdrv (modules) sfs.
#w464 tidyup, make sure /etc/puppyversion is history.
#w474 updatenetmoduleslist.sh called when zdrv loaded or unloaded.
#w477 /etc/desktop_icon_theme has default desktop icon set.
#v423 fix for PUPMODE=2, full hd installation.
#100425 generate woof-tree-official.png from svg.
#100913 new puppy filenames.
#101020 no longer using modprobe.conf.
#101228 01micko: bug fix.
#110119 giving this script an overhaul.
#110122 make sure all woof scripts are updated.
#110204 LANG in /etc/profile got overwritten.
#110422 DISTRO_VERSION variable now has dotted format. note, also now using full dotted version# in puppy filenames.
#110622 mageia1 note, pixbuf cache.
#110722 updated lists of woof scripts that must be updated.
#110722 get rid of stray modules.* files, may occur when mix usage of the two depmods.
#110826 do not call icon_switcher_cli at all.
#111009 try fix upgrading old wary save-file.
#111027 maybe need to fix permissions on /tmp and /var.
#111123 first-boot fix if did cross-build in Woof.
#120209 append LANG when call fixmenus. also run fixdesk, fixscripts
#120225 fix non-english PuppyPin when layers change.
#120323 shinobar: need some more operations when unionfs layers change.
#120505 PUPMODE==2, need to consider situation of a full-hd install that is not pre-setup.
#120522 ubuntu precise puppy must have the schemas compiled (otherwise seamonkey crashed) (normally done in 3builddistro).
#120523 precise puppy, may also need to run gio-querymodules. (refer also installpkg.sh, 3builddistro)
#120605 shinobar: need to run gtk-query-immodules-2.0 at first start.
#130203 change probedisk2 to probedisk.
#130217 depmod_func() created, to ensure deletion of modules.* files.
#130418 support kmod (see also rc.sysinit, 3builddistro).

#This script is called by /etc/rc.d/rc.sysinit. the purpose is to perform
#any required updates to files when a new version of Puppy is booted.

#this script can also be called by /usr/sbin/puppyinstaller, when doing a full install
#(option2) to hard drive. in that case, chroot to hd partition, and the ${DISTRO_PUPPYSFS}
#source files are mounted on /srcmntpt in the hd partition.
#puppyinstaller will call here with PUPMODE="option2hdinstall"

PUPMODE="$1"
KERNVER="`uname -r`"
[ -z $DISPLAY ] || XSTATUS="yes" #v3.99

[ "$PUPMODE" = "" ] && exit

depmod_func() { #130217 extracted as a function.
 #then run depmod to overwrite any depmod files on OLD layer...
 echo "Executing depmod, to update module files..."
 if which kmod;then #130418
  depmod #this is a symlink to kmod.
 else
  #110722 get rid of stray modules.* files, may occur when mix usage of the two depmods...
  mkdir -p /tmp/rc_update_dump
  mv -f /lib/modules/${KERNVER}/modules.* /tmp/rc_update_dump
  [ -f /tmp/rc_update_dump/modules.builtin ] && mv -f /tmp/rc_update_dump/modules.builtin /lib/modules/${KERNVER}/
  [ -f /tmp/rc_update_dump/modules.order ] && mv -f /tmp/rc_update_dump/modules.order /lib/modules/${KERNVER}/
  rm -f /tmp/rc_update_dump/modules.*
  if [ "`busybox | grep 'depmod'`" != "" ];then
   busybox depmod #use busybox depmod if available.
  else
   depmod
  fi
 fi
}

case $PUPMODE in
 "7") #tmpfs (pup_rw), hd for persistent storage (pup_ro1), ${DISTRO_PUPPYSFS} (pup_ro2).
  NEWFILESMNTPT="/initrd/pup_ro2"
  OLDFILESMNTPT="/initrd/pup_ro1"
 ;;
 "6") #no tmpfs, PDEV1 (pup_rw), ${DISTRO_PUPPYSFS} (pup_ro2) 
  #have booted from PDEV1 partition, which has initrd.gz & ${DISTRO_PUPPYSFS} files on it, and
  #session has been saved direct to the partition. (very similar to mode 12)
  NEWFILESMNTPT="/initrd/pup_ro2"
  OLDFILESMNTPT="/initrd/pup_rw"
 ;;
 "12") #no tmpfs, ${DISTRO_FILE_PREFIX}save.3fs (pup_rw), nothing (pup_ro1), ${DISTRO_PUPPYSFS} (pup_ro2)
  #example: boot from live-cd, ${DISTRO_FILE_PREFIX}save.3fs on a fast h.d. partition.
  NEWFILESMNTPT="/initrd/pup_ro2"
  OLDFILESMNTPT="/initrd/pup_rw"
 ;;
 "13") #tmpfs (pup_rw), ${DISTRO_FILE_PREFIX}save.3fs (pup_ro1), ${DISTRO_PUPPYSFS} (pup_ro2).
  #example: boot from usb flash, ${DISTRO_FILE_PREFIX}save.3fs on flash media (needs restrained writes).
  NEWFILESMNTPT="/initrd/pup_ro2"
  OLDFILESMNTPT="/initrd/pup_ro1"
 ;;
 "77") #tmpfs (pup_rw), folders (pup_ro1), ${DISTRO_PUPPYSFS} (pup_ro2).
  #example: boot from multisession live-cd/dvd, pup_ro1 is a tmpfs folders copied from cd.
  NEWFILESMNTPT="/initrd/pup_ro2"
  OLDFILESMNTPT="/initrd/pup_ro1"
 ;;
 "option2hdinstall")
  NEWFILESMNTPT="/srcmntpt"
  OLDFILESMNTPT=""
 ;;
 5) #v423
  #PUPMODE=5 is first boot, ignore.
  if [ -f /etc/rc.d/WOOFMERGEVARS ];then #111123 inserted by 3builddistro.
. /etc/rc.d/WOOFMERGEVARS
   if [ "$WOOF_HOSTARCH" != "$WOOF_TARGETARCH" ];then #woof did a cross-build
    echo -n " cross-build-fix" >/dev/console
    #these are normally done in 3builddistro.
    ldconfig #ldconfig put in by 3builddistro. rebuild /etc/ld.so.cache
    /usr/sbin/indexgen.sh #create master help index.
    /usr/sbin/fixmenus #Reconstruct configuration files for JWM, Fvwm95, IceWM. 120323 note: fixmenus corrects $LANG.
    /usr/bin/fc-cache -s -v #fontconfig cache
    PANGOMODULES="`find /etc/pango -type f -name pango.modules`"
    [ "$PANGOMODULES" = "" ] && PANGOMODULES='/etc/pango/pango.modules'
    [ ! -s ${PANGOMODULES} ] && pango-querymodules > ${PANGOMODULES}
    #120522 ubuntu precise puppy must have the schemas compiled (otherwise seamonkey crashed)...
    [ -d /usr/share/glib-2.0/schemas ] && [ -e /usr/bin/glib-compile-schemas ] && /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
    #120523 this too...
    [ -d /usr/lib/gio/modules ] && [ -e /usr/bin/gio-querymodules ] && /usr/bin/gio-querymodules /usr/lib/gio/modules
    #120605 note, /usr/lib/gtk-2.0/2.10.0/gtk.immodules is a symlink to this...
    gtk-query-immodules-2.0 > /etc/gtk-2.0/gtk.immodules #120605 shinobar.
   fi
  fi
  NEWPVERSION="`grep '^DISTRO_VERSION' /etc/DISTRO_SPECS | cut -f 2 -d '=' | cut -f 2 -d '"' | cut -f 2 -d "'" | cut -f 1 -d ' '`" #110422
  #w001 generate /etc/networkmodules...
  echo -n " network-drivers-list" >/dev/console
  /usr/sbin/updatenetmoduleslist.sh
  #110119 just in case something missing (like svg loader)...
  echo -n " pixbuf-loaders" >/dev/console
  gdk-pixbuf-query-loaders > /etc/gtk-2.0/gdk-pixbuf.loaders
  #110622 note, mageia1 requires above file to be at /usr/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache -- symlink created by 3builddistro.
  #w002 update icon cache (supposed to speed things up)...
  echo -n " gtk-icon-cache" >/dev/console
  #note, can use  --ignore-theme-index if want to...
  /usr/bin/gtk-update-icon-cache -f /usr/share/icons/hicolor/
  #w002 generate desktop icons (previously done in unleashed)...
  echo -n " desk-icons" >/dev/console
  if [ -f /etc/desktop_icon_theme ];then #110119
   DEFAULT_DESK_ICONS="`cat /etc/desktop_icon_theme`" #Woof creates this.
  else #precaution
   DEFAULT_DESK_ICONS="`ls -1 /usr/local/lib/X11/themes | tail -n 1`"
  fi
  #110826 not needed, now done in 3builddistro...
  #/usr/sbin/icon_switcher_cli $DEFAULT_DESK_ICONS
  #[ -f /usr/share/doc/woof-tree-official.svg ] && [ ! -f /usr/share/doc/woof-tree-official.png ] && rsvg-convert -w 648 -a -o /usr/share/doc/woof-tree-official.png /usr/share/doc/woof-tree-official.svg #100425
  exit ###EXIT###
 ;;
 *) #v423 PUPMODE=2 (full hd install) then just exit.
  #exit
  #120505 need to consider situation of a full-hd install that is not pre-setup, as would normally be done by 3builddistro in Woof and the Universal Installer.
  #one exception is a cross-build in Woof. in such a situation /etc/networkmodules would not exist...
  [ -f /etc/networkmodules ] && exit ###EXIT###
  #code as per PUPMODE==5 above...
  if [ -f /etc/rc.d/WOOFMERGEVARS ];then #111123 inserted by 3builddistro.
. /etc/rc.d/WOOFMERGEVARS
   if [ "$WOOF_HOSTARCH" != "$WOOF_TARGETARCH" ];then #woof did a cross-build
    echo -n " cross-build-fix" >/dev/console
    #these are normally done in 3builddistro.
    ldconfig #ldconfig put in by 3builddistro. rebuild /etc/ld.so.cache
    /usr/sbin/indexgen.sh #create master help index.
    /usr/sbin/fixmenus #Reconstruct configuration files for JWM, Fvwm95, IceWM. 120323 note: fixmenus corrects $LANG.
    /usr/bin/fc-cache -s -v #fontconfig cache
    PANGOMODULES="`find /etc/pango -type f -name pango.modules`"
    [ "$PANGOMODULES" = "" ] && PANGOMODULES='/etc/pango/pango.modules'
    [ ! -s ${PANGOMODULES} ] && pango-querymodules > ${PANGOMODULES}
    #120522 ubuntu precise puppy must have the schemas compiled (otherwise seamonkey crashed)...
    [ -d /usr/share/glib-2.0/schemas ] && [ -e /usr/bin/glib-compile-schemas ] && /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
    #120523 this too...
    [ -d /usr/lib/gio/modules ] && [ -e /usr/bin/gio-querymodules ] && /usr/bin/gio-querymodules /usr/lib/gio/modules
    #120605 note, /usr/lib/gtk-2.0/2.10.0/gtk.immodules is a symlink to this...
    gtk-query-immodules-2.0 > /etc/gtk-2.0/gtk.immodules #120605 shinobar.
   fi
  fi
  NEWPVERSION="`grep '^DISTRO_VERSION' /etc/DISTRO_SPECS | cut -f 2 -d '=' | cut -f 2 -d '"' | cut -f 2 -d "'" | cut -f 1 -d ' '`" #110422
  #w001 generate /etc/networkmodules...
  echo -n " network-drivers-list" >/dev/console
  /usr/sbin/updatenetmoduleslist.sh #creates /etc/networkmodules
  #110119 just in case something missing (like svg loader)...
  echo -n " pixbuf-loaders" >/dev/console
  gdk-pixbuf-query-loaders > /etc/gtk-2.0/gdk-pixbuf.loaders
  #110622 note, mageia1 requires above file to be at /usr/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache -- symlink created by 3builddistro.
  #w002 update icon cache (supposed to speed things up)...
  echo -n " gtk-icon-cache" >/dev/console
  #note, can use  --ignore-theme-index if want to...
  /usr/bin/gtk-update-icon-cache -f /usr/share/icons/hicolor/
  #w002 generate desktop icons (previously done in unleashed)...
  echo -n " desk-icons" >/dev/console
  if [ -f /etc/desktop_icon_theme ];then #110119
   DEFAULT_DESK_ICONS="`cat /etc/desktop_icon_theme`" #Woof creates this.
  else #precaution
   DEFAULT_DESK_ICONS="`ls -1 /usr/local/lib/X11/themes | tail -n 1`"
  fi
  #110826 not needed, now done in 3builddistro...
  #/usr/sbin/icon_switcher_cli $DEFAULT_DESK_ICONS
  exit ###EXIT###
 ;;
esac

. $NEWFILESMNTPT/etc/DISTRO_SPECS #w020 has DISTRO_VERSION, DISTRO_BINARY_COMPAT, DISTRO_FILE_PREFIX

NEWPVERSION="`grep '^DISTRO_VERSION' $NEWFILESMNTPT/etc/DISTRO_SPECS | cut -f 2 -d '=' | cut -f 2 -d '"' | cut -f 2 -d "'" | cut -f 1 -d ' '`" #110422
#note, reboot after creating save file, this might not be there...
# (but does get created, see bottom of this script)
if [ -f $OLDFILESMNTPT/etc/puppyversion ];then
 OLDPVERSION="`cat $OLDFILESMNTPT/etc/puppyversion`" #110422
 rm -f $OLDFILESMNTPT/etc/puppyversion #init does this, but not in case of full hd install.
fi
[ -f $OLDFILESMNTPT/etc/DISTRO_SPECS ] && OLDPVERSION="`grep '^DISTRO_VERSION' $OLDFILESMNTPT/etc/DISTRO_SPECS | cut -f 2 -d '=' | cut -f 2 -d '"' | cut -f 2 -d "'" | cut -f 1 -d ' '`" #110422
[ "$OLDPVERSION" = "" ] && OLDPVERSION="$NEWPVERSION" #110422
if [ "$OLDPVERSION" = "0" ];then #101228 01micko: workaround for weird bug, full hd install.
 #OLDPVERSION=`expr $NEWPVERSION - 1`
 OLDPVERSION='0.0' #110422
fi

#if [ $NEWPVERSION -gt $OLDPVERSION ];then
FLAGnew='false' #110422
if vercmp $NEWPVERSION gt $OLDPVERSION ; then #110422
 FLAGnew='true' #110422
 VERstr='to version'
 [ "$OLDPVERSION" != "0.0" ] && VERstr="from version $OLDPVERSION to" #110422
 echo "Upgrading ${DISTRO_NAME} ${VERstr} ${NEWPVERSION}..." #110422
 echo -n " $OLDPVERSION to $NEWPVERSION" >/dev/console

 #need to identify files that absolutely must always be updated, when
 #there is a new version, and forcefully copy them...
 echo "Updating w.m. menus..."
 cp -af $NEWFILESMNTPT/root/.packages/Packages-* /root/.packages/ #w012
 cp -af $NEWFILESMNTPT/root/.packages/DISTRO_PKGS_SPECS /root/.packages/ #w012
 
 #110119...
 rm -rf /root/.packages/builtin_files
 cp -af $NEWFILESMNTPT/root/.packages/builtin_files /root/.packages/
 cp -af $NEWFILESMNTPT/root/.packages/woof-installed-packages /root/.packages/
 PKG_REPOS_ENABLED="`grep '^PKG_REPOS_ENABLED' /root/.packages/PKGS_MANAGEMENT | cut -f 2 -d "'"`" #geany'
 grep -v '^PKG_REPOS_ENABLED' $NEWFILESMNTPT/root/.packages/PKGS_MANAGEMENT > /tmp/PKGS_MANAGEMENT-tmp
 mv -f /tmp/PKGS_MANAGEMENT-tmp /root/.packages/PKGS_MANAGEMENT
 echo "PKG_REPOS_ENABLED='${PKG_REPOS_ENABLED}'" >> /root/.packages/PKGS_MANAGEMENT
 [ -f /etc/puppyversion ] && rm -f /etc/puppyversion #about time got rid of this.

 cp -af $NEWFILESMNTPT/etc/DISTRO_SPECS /etc/ #w012
 cp -af /root/.fvwm95rc /root/DOTfvwm95rc.bak 2>/dev/null
 cp -af /root/.jwmrc /root/DOTjwmrc.bak 2>/dev/null
 cp -af /root/.icewm/menu /root/DOTicewmmenu.bak 2>/dev/null #v1.0.5
 cp -af $NEWFILESMNTPT/root/.fvwm95rc /root/ 2>/dev/null
 cp -af $NEWFILESMNTPT/root/.jwmrc /root/ 2>/dev/null 
 cp -af $NEWFILESMNTPT/root/.icewm/menu /root/.icewm/ 2>/dev/null #v1.0.5
 #also update every boot and shutdown script...
 echo "Updating boot and shutdown system scripts..."
 cp -af $NEWFILESMNTPT/etc/rc.d/rc.country /etc/rc.d/
 cp -af $NEWFILESMNTPT/etc/rc.d/rc.network /etc/rc.d/
 cp -af $NEWFILESMNTPT/etc/rc.d/rc.shutdown /etc/rc.d/
 cp -af $NEWFILESMNTPT/etc/rc.d/rc.sysinit /etc/rc.d/
 cp -af $NEWFILESMNTPT/etc/rc.d/rc.update /etc/rc.d/
 
 #110119...
 cp -af $NEWFILESMNTPT/etc/rc.d/rc.services /etc/rc.d/
 cp -af $NEWFILESMNTPT/etc/rc.d/functions4puppy /etc/rc.d/
 cp -af $NEWFILESMNTPT/etc/rc.d/functions4puppy4 /etc/rc.d/
 SKIPLIST="`grep '^SKIPLIST' /etc/rc.d/MODULESCONFIG | cut -f 2 -d '"'`" #geany'
 ADDLIST="`grep '^ADDLIST' /etc/rc.d/MODULESCONFIG | cut -f 2 -d "'"`" #geany'
 REGEX1="s%^SKIPLIST.*%SKIPLIST=\"${SKIPLIST}\"%"
 REGEX2="s%^ADDLIST.*%ADDLIST='${ADDLIST}'%"
 cp -af $NEWFILESMNTPT/etc/rc.d/MODULESCONFIG /etc/rc.d/
 sed -i -e "${REGEX1}" -e "${REGEX2}" /etc/rc.d/MODULESCONFIG
 
 #110204 problem, as overwrites LANG setting...
 #cp -af $NEWFILESMNTPT/etc/profile /etc/
 xLANG="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
 #110216 still not working. so, do this bad hack (see rc.shutdown)...
 [ -f /var/local/etc_profile_at_shutdown ] && xLANG="`grep '^LANG=' /var/local/etc_profile_at_shutdown | cut -f 2 -d '=' | cut -f 1 -d ' '`" #110216
 REGEX3="s%^LANG=.*%LANG=${xLANG}%"
 sed -e "$REGEX3" $NEWFILESMNTPT/etc/profile > /etc/profile
 
 #cp -af $NEWFILESMNTPT/usr/X11R7/bin/xwin /usr/X11R7/bin/
 cp -af $NEWFILESMNTPT/root/.xinitrc /root/
 
 #110122 make sure all woof scripts are updated... 110722 updated lists... 111009 update...
 for ONESCRIPT in df initrd_layer_test mount ps pupkill umount
 do
  cp -a -f --remove-destination $NEWFILESMNTPT/bin/$ONESCRIPT /bin/
 done
 for ONESCRIPT in clean_desk_icons dir2sfs dir2tgz gtklogfileviewer initNEW poweroff probedisk probepart pup_event_backend_firmware pup_event_backend_modprobe pup_event_backend_modprobe_protect pup_event_frontend_d reboot runlevel test-eide test-scsi
 do
  cp -a -f --remove-destination $NEWFILESMNTPT/sbin/$ONESCRIPT /sbin/ 2>/dev/null
 done
 for ONESCRIPT in dir2pet dirs2pets evince gtkdialog-splash lprshell man new2dir pet2tgz petspec pman pup2pet pupdialog restartwm startx tgz2pet wmexit wmpoweroff wmreboot wvdialshell xterm xwin yaf-splash
 do
  cp -a -f --remove-destination $NEWFILESMNTPT/usr/bin/$ONESCRIPT /usr/bin/ 2>/dev/null
 done
 for ONESCRIPT in alsaconf alsawizard askpass background_reshape bcrypt_gui bootflash bootmanager burniso2cd calcfreespace.sh cdburner-wizard chooselocale connectwizard connectwizard_2nd countrywizard cups_shell delayedrun deskshortcut.sh dotpup dotpuprox.sh download_file e2ore3.sh eventmanager exploderpm filemnt fitimage2root fixdesk fixmenus fixPuppyPin floppy-format.sh framebufferwizard freememappletshell gaim-autosetup.sh gaimshell gen_modem_init_string get_bluetooth_connection get_modem_alternate_device grubconfig grubconfigREADME.txt icon_switcher icon_switcher_cli importfrompuppy1 indexgen.sh input-wizard ipinfo loginmanager mailto.sh modemdisconnect modemprobe modemprobe_erase modemprobe_help modemtest networkdisconnect partview partviewOLD pcur pdict Pdisk petget pgprs-shell pmodemdiag pmonitork.sh pmonitorx.sh pmount pmovie pngoverlay pngoverlay.bac pngoverlay.sh pppconnect pppdisconnect pppsetup Pudd pup-advert-blocker pupautodetect pupcamera pupdial pupdial_init_hotpluggable pupdial_wizard_helper puppyhelp puppyinstaller puppypdf pupscan remasterpup2 remove_builtin report-video resizepfile.sh root2user save2flash save2image savesession-dvd scannerwizard.sh securetelnetshell set_bg set_hwclock_type set-time-for-puppy set-xftdpi sfsget shutdownconfig snapmergepuppy sns spacereplace systemvalues timezone-set tkgamma updatenetmoduleslist.sh usb_modem_special_status usb_modeswitch_status video_upgrade_wizard video-wizard wakepup2 welcome1stboot welcome1stboot.bac wirelesswizard wizardwizard xdelta_gui xf86svga336wizard xgamma-gui xinitrc_test xorgwizard xorgwizard-automatic xrandrshell xserverwizard
 do
  cp -a -f --remove-destination $NEWFILESMNTPT/usr/sbin/$ONESCRIPT /usr/sbin/ 2>/dev/null
 done
 cp -a -f --remove-destination $NEWFILESMNTPT/usr/local/petget /usr/local/
 cp -a -f --remove-destination $NEWFILESMNTPT/usr/local/simple_network_setup /usr/local/
 cp -a -f --remove-destination $NEWFILESMNTPT/usr/local/video_upgrade /usr/local/

# #110810 kernels now being built with, ex /lib/modules/2.6.32 unchanged when version upgrade...
# # ex 2.6.32-40, 2.6.32-44
# # so, wipe any remnants of previous modules that might have got copied-up or installed...
# rm -rf $OLDFILESMNTPT/lib/modules/$KERNVER/*

 depmod_func #run depmod. 130217 extracted as a function.
  
 #alsaconf generates alsaconf.cards, maybe have to get rid of old one?... 110119
 [ -f /var/tmp/alsaconf.cards ] && rm -f /var/tmp/alsaconf.cards
 
 if [ -f /etc/modprobe.conf ];then #110119 deprecated.
  SNDCARD0="`grep '^alias snd\-card\-0' /etc/modprobe.conf`"
  SNDSLOT0="`grep '^alias sound\-slot\-0' /etc/modprobe.conf`"
  rm -f /etc/modprobe.conf
  [ "$SNDCARD0" ] && echo "$SNDCARD0" >> /etc/modprobe.d/alsa.conf
  [ "$SNDSLOT0" ] && echo "$SNDSLOT0" >> /etc/modprobe.d/alsa.conf
 fi

 #v2.14 Jesse suggested this. remove any broken shared library symlinks...
 find / -mount -noleaf -path '*/lib/*' -type l -name *.so -o -noleaf -path '*/lib/*' -type l -name *.so.* |
 while read ONESYMLINK
 do
  ONESYMDEST="`readlink $ONESYMLINK`"
  ONESYMDIR="`dirname $ONESYMLINK`"
  FIRSTCHAR="`echo -n "$ONESYMDEST" | cut -b 1`"
  if [ "$FIRSTCHAR" = "/" ];then
   [ ! -e "$ONESYMDEST" ] && rm -f $ONESYMLINK
  else
   [ ! -e $ONESYMDIR/$ONESYMDEST ] && rm -f $ONESYMLINK
  fi
 done

 #111009 try fix upgrading old wary save-file...
 if [ "$PUPMODE" != "option2hdinstall" ];then
  case $DISTRO_FILE_PREFIX in
   wary)
    #[ -d /usr/lib/xorg/modules/drivers-backup ] && rm -rf /usr/lib/xorg/modules/drivers-backup
    #[ -d /usr/lib/xorg/modules/drivers-alternate ] && rm -rf /usr/lib/xorg/modules/drivers-alternate
    if vercmp $OLDPVERSION lt 5.1.5;then #before i recompiled everything in t2.
     #x might not work, wipe all stuff in save-file...
     rm -rf $OLDFILESMNTPT/usr/X11R7/* #hmmm, will this show on top?
     rm -f /etc/X11/xorg.conf
     #111026 upnorth posted this.. (http://murga-linux.com/puppy/viewtopic.php?t=72258&start=90)
     rm /etc/gtk-2.0/gdk-pixbuf.loaders
     gdk-pixbuf-query-loaders >/etc/gtk-2.0/gdk-pixbuf.loaders
    fi
   ;;
  esac
 fi

 #w001 generate /etc/networkmodules...
 echo -n " network-drivers-list" >/dev/console
 /usr/sbin/updatenetmoduleslist.sh
 
 #w002 update icon cache (supposed to speed things up)...
 echo -n " gtk-icon-cache" >/dev/console
 /usr/bin/gtk-update-icon-cache -f /usr/share/icons/hicolor/

 #110119 just in case something missing (like svg loader)...
 echo -n " pixbuf-loaders" >/dev/console
 gdk-pixbuf-query-loaders > /etc/gtk-2.0/gdk-pixbuf.loaders
 #110622 note, mageia1 requires above file to be at /usr/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache -- symlink created by 3builddistro.

 #110826 don't do any of this...
# #w002 generate desktop icons (previously done in unleashed)...
# #(doing it here saves space in the iso file)
# echo -n " desk-icons" >/dev/console
# if [ -f /etc/desktop_icon_theme ];then #110119
#  DEFAULT_DESK_ICONS="`cat /etc/desktop_icon_theme`" #Woof creates this.
# else #precaution
#  DEFAULT_DESK_ICONS="`ls -1 /usr/local/lib/X11/themes | tail -n 1`"
# fi
 #110826 don't call this, can't run pngoverlay without X...
 #/usr/sbin/icon_switcher_cli $DEFAULT_DESK_ICONS
 #110826 instead, copy-up icons...
 # ...um, no, leave the user's theme alone.
 
 #111027 sometimes these go back to 755... 01micko reports this problem with full install
 chmod 777 /tmp
 chmod 777 /var

 #moved this stuff down from line 166. fixmenus must run after desktop icons setup...
 #master help index has to be updated...
 /usr/sbin/indexgen.sh
 #120209 i think these also need to run, in case a target file has changed and needs to be re-translated...
 #but, i think the english target files will have to be copied to top first...
 SSSDESK="`grep '^\[' $NEWFILESMNTPT/usr/share/sss/desk_strings/desk_strings | tr -d '[' | tr -d ']' | tr '\n' ' '`"
 SSSSCRIPTS="`grep '^\[' $NEWFILESMNTPT/usr/share/sss/script_strings/script_strings | tr -d '[' | tr -d ']' | tr '\n' ' '`"
 for ASECTIONID in ${SSSDESK} ${SSSSCRIPTS} #ex: general _woof_.jwmrc
 do
  #allow any character to be the 'marker' for / in the section-id...
  MARKERCHAR="`echo -n "$ASECTIONID" | cut -c 1`"
  TARGETFILE="`echo -n "$ASECTIONID" | tr "${MARKERCHAR}" '/'`" #ex: /root/Choices/ROX-Filer/PuppyPin
  if [ -f ${NEWFILESMNTPT}${TARGETFILE} ];then #precaution.
   TARGETPATH="`dirname $TARGETFILE`"
   cp -a -f ${NEWFILESMNTPT}${TARGETFILE} ${TARGETPATH}/
  fi
  [ -f /var/local/sss${TARGETFILE} ] && rm -f /var/local/sss${TARGETFILE} #remove previous en backup (see fixdesk)
 done
 LANG=$xLANG /usr/sbin/fixscripts
 LANG=$xLANG /usr/sbin/fixdesk
 #Reconstruct configuration files for JWM, Fvwm95, IceWM... 120209 append LANG...
 LANG=$xLANG /usr/sbin/fixmenus

fi
xLANG="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"


#stuff to update on every boot...

#think have to do this everytime...
cp -af $NEWFILESMNTPT/etc/DISTRO_SPECS /etc/
touch /etc/DISTRO_SPECS #important, as snapmergepuppy uses '-u' cp option. ...huh, why?

#v2.16 if unionfs layers have changed, may need to fix menu (etc)...
#note, BootManager may be popped-up, see /usr/sbin/delayedrun.
if [ -d /initrd ];then #test it isn't full hd installation.
 . /etc/rc.d/BOOTCONFIG
 
 #v2.16.1 bug with multisession-cd, different folder at each startup, so screen out...
 xLASTUNIONRECORD="`echo -n "$LASTUNIONRECORD" | sed -e 's/^20[0-9][0-9][-0123456789]* //'`"
 xPREVUNIONRECORD="`echo -n "$PREVUNIONRECORD" | sed -e 's/^20[0-9][0-9][-0123456789]* //'`"

 ##v2.17 2nd boot, new ${DISTRO_FILE_PREFIX}save, prevent update....
 #[ " ${DISTRO_PUPPYSFS}" = "$PREVUNIONRECORD" ] && xPREVUNIONRECORD="$xLASTUNIONRECORD"
 #...no, want to do fix if using an old ${DISTRO_FILE_PREFIX}save file.
 
 if [ "$xLASTUNIONRECORD" != "$xPREVUNIONRECORD" ];then
  echo -en " layered-filesystem \\033[1;35mnext boot will be faster!\\033[0;39m" > /dev/console
  echo "Unionfs layers have changed since previous boot, fixing menu..."
  #master help index has to be updated...
  /usr/sbin/indexgen.sh
  #Reconstruct configuration files for JWM, Fvwm95, IceWM...
  LANG=$xLANG /usr/sbin/fixmenus
  fc-cache -f #120323 shinobar: some sfs files may have fonts
  which gtk-query-immodules-2.0 &>/dev/null && gtk-query-immodules-2.0 > /etc/gtk-2.0/gtk.immodules #120323 shinobar: refresh gtk.immodules
  depmod_func #120323 shinobar: and some sfs may have kernel modules. 130217 depmod extracted as a function.
  #w007 has a 'zdrv' layer been added or removed?...
  EXMOD_CNT=0
  zzPATTERN='zdrv' #120323 have settled on this in all zdrv sfs filenames.
  EXMOD_SFS1="`echo "$xLASTUNIONRECORD" | tr ' ' '\n' | grep "$zzPATTERN"`"
  EXMOD_SFS2="`echo "$xPREVUNIONRECORD" | tr ' ' '\n' | grep "$zzPATTERN"`"
  [ "$EXMOD_SFS1" != "" ] && EXMOD_CNT=1
  [ "$EXMOD_SFS2" != "" ] && EXMOD_CNT=`expr $EXMOD_CNT + 1`
  if [ $EXMOD_CNT -eq 2 ];then
   [ "$EXMOD_SFS1" != "$EXMOD_SFS2" ] && EXMOD_CNT=1 #may have changed to a different zdrv sfs.
  fi
  if [ $EXMOD_CNT -eq 1 ];then
   #depmod #120323 removed, already done above.
   /usr/sbin/updatenetmoduleslist.sh
  fi
 fi
 
 #fix the desktop...
 #note, init does 'touch' on PuppyPin and globicons to prevent overwrite at version upgrade.
 #if [ $NEWPVERSION -gt $OLDPVERSION -o "$xLASTUNIONRECORD" != "$xPREVUNIONRECORD" ];then
 if [ "$FLAGnew" = "true" -o "$xLASTUNIONRECORD" != "$xPREVUNIONRECORD" ];then #110422
  #need some way to retain user-modifications, especially additions to desktop...
  #one way is to just merge everything and then delete invalid entries...
  #DennisF reported backdrop image reverted to default.jpg, so screen out default here...
  grep -v '<backdrop' $NEWFILESMNTPT/root/Choices/ROX-Filer/PuppyPin >> /root/Choices/ROX-Filer/PuppyPin
  cat $NEWFILESMNTPT/root/Choices/ROX-Filer/globicons >> /root/Choices/ROX-Filer/globicons
  #an inconstency, user-mod to desktop, PuppyPin gets changed in
  #/root/Choices/ROX-Filer/, but globicons gets copied and updated in
  #/root/.config/rox.sourceforge.net/ROX-Filer/...
  if [ -f /root/.config/rox.sourceforge.net/ROX-Filer/globicons ];then
   cat /root/.config/rox.sourceforge.net/ROX-Filer/globicons >> /root/Choices/ROX-Filer/globicons
   rm -f /root/.config/rox.sourceforge.net/ROX-Filer/globicons
  fi
  for LAYERNUM in 3 4 5 6 7 8 9 #v431 added 6 7 8 9
  do
   [ -f /initrd/pup_ro$LAYERNUM/root/Choices/ROX-Filer/PuppyPin ] && cat /initrd/pup_ro$LAYERNUM/root/Choices/ROX-Filer/PuppyPin >> /root/Choices/ROX-Filer/PuppyPin
   [ -f /initrd/pup_ro$LAYERNUM/root/Choices/ROX-Filer/globicons ] && cat /initrd/pup_ro$LAYERNUM/root/Choices/ROX-Filer/globicons >> /root/Choices/ROX-Filer/globicons
  done
  echo '<?xml version="1.0"?>' > /tmp/PuppyPinTmp
  echo '<pinboard>' >> /tmp/PuppyPinTmp
  grep -v -E 'pinboard>|<\?xml' /root/Choices/ROX-Filer/PuppyPin | sort -u >> /tmp/PuppyPinTmp
  echo '</pinboard>' >> /tmp/PuppyPinTmp
  #120225 non-english locale may have translations for PuppyPin. translate en text now, and
  #duplicates will get removed further down...
  if [ -f /usr/share/sss/desk_strings/desk_strings.${xLANG%_*} ];then #ex: script_strings.de
   sPTN="/^\[_root_Choices_ROX-Filer_PuppyPin\]/,/^$/p" #this is a multi-line block find expression.
   CODEBLOCK="`sed -n "$sPTN" /usr/share/sss/desk_strings/desk_strings.${xLANG%_*} | sed -e '/^#/d' -e '/%%/d' -e '/^$/d' -e '/^\[/d'`" #extracts just the relevant block of lines.
   if [ "$CODEBLOCK" ];then
    echo "$CODEBLOCK" > /tmp/rc-update-puppypin-codeblock
    sed -i -f /tmp/rc-update-puppypin-codeblock /tmp/PuppyPinTmp
   fi
  fi
  #sort -u only gets rid of completely duplicate lines. delete duplicate execs...
  #also delete line if exec is invalid. also avoid overlapping icons...
  EXECSLOG="";XYLOG=""
  echo -n "" > /root/Choices/ROX-Filer/PuppyPin
  cat /tmp/PuppyPinTmp |
  while read ONELINE
  do
   if [ "`echo -n "$ONELINE" | grep -v '<icon '`" != "" ];then
    echo "$ONELINE" >> /root/Choices/ROX-Filer/PuppyPin
    continue
   fi
   ONEEXEC="`echo -n "$ONELINE" | cut -f 2 -d '>' | cut -f 1 -d '<'`"
   ONEX=`echo -n "$ONELINE" | cut -f 1 -d '>' | cut -f 2 -d '"'` #'geany
   ONEY=`echo -n "$ONELINE" | cut -f 1 -d '>' | cut -f 4 -d '"'` #'geany
   ONEXY0="${ONEX}_${ONEY}"
   ONEXY="${ONEX}_${ONEY}"
   while [ "`echo -n "$XYLOG" | grep "$ONEXY"`" != "" ];do
    ONEX=`expr $ONEX + 32` #64
    ONEY=`expr $ONEY + 48` #96
    ONEXY="${ONEX}_${ONEY}"
   done
   if [ "`echo "$EXECSLOG" | grep "$ONEEXEC"`" = "" ];then
    if [ -e $ONEEXEC ];then #could be a file, symlink or directory(rox-dir).
     XYLOG="$XYLOG $ONEXY"
     if [ "$ONEXY" = "$ONEXY0" ];then
      echo "$ONELINE" >> /root/Choices/ROX-Filer/PuppyPin
     else
      #icon has to be moved...
      XPATTERN="s/x=\"[0-9]*\"/x=\"${ONEX}\"/g"
      YPATTERN="s/y=\"[0-9]*\"/y=\"${ONEY}\"/g"
      echo "$ONELINE" | sed -e "$XPATTERN" | sed -e "$YPATTERN" >> /root/Choices/ROX-Filer/PuppyPin
     fi
    fi
   fi
   EXECSLOG="$EXECSLOG $ONEEXEC"
  done
  #now do globicons file...
  echo '<?xml version="1.0"?>' > /tmp/globiconsTmp
  echo '<special-files>' >> /tmp/globiconsTmp
  grep -v -E 'special\-files>|<\?xml' /root/Choices/ROX-Filer/globicons >> /tmp/globiconsTmp
  echo '</special-files>' >> /tmp/globiconsTmp
  #get rid of duplicate entries...
  RULESLOG="";FLAGIGNORE="";ONERULE=""
  echo -n "" >  /root/Choices/ROX-Filer/globicons
  #v431 add tests that executables and icons exist...
  #v431 make sure tags start new lines...
  cat /tmp/globiconsTmp | tr -d '\n' | sed -e 's%$%\n%' -e 's%<icon>%\n<icon>%g' -e 's%<rule%\n<rule%g' -e 's%</rule>%\n</rule>%g' -e 's%</special%\n</special%g' -e 's%<special%\n<special%g' |
  while read ONELINE
  do
   if [ "`echo -n "$ONELINE" | grep 'rule match'`" != "" ];then
    ONERULE="`echo -n "$ONELINE" | cut -f 2 -d '"'`" #'geany
    read ONEICON
    read ENDRULE
    [ "`echo -n "$RULESLOG" | grep "$ONERULE"`" != "" ] && continue #avoid duplicates.
    [ ! -e "$ONERULE" ] && continue #test executable exists.
    REALICON="`echo -n "$ONEICON" | cut -f 2 -d '>' | cut -f 1 -d '<'`"
    [ ! -e "$REALICON" ] && continue #test icon exists.
    RULESLOG="$RULESLOG $ONERULE"
    echo "$ONELINE
$ONEICON
$ENDRULE" >> /root/Choices/ROX-Filer/globicons
   else
    echo "$ONELINE" >> /root/Choices/ROX-Filer/globicons
   fi
  done
 fi
fi

###END###
