#!/bin/sh
#(c) Copyright Barry Kauler 2009.
#2createpackages has already built the generic packages in packages-$DISTRO_FILE_PREFIX/.
#these packages have generic names as specified in the second field of PKGS_SPECS_TABLE,
#which is a variable in ./DISTRO_PKGS_SPECS. The format of entries in table:
# yes|abiword|abiword,abiword-common|exe,dev,doc,nls
# ...if first field is 'yes' then it will be built into distro.
#100628 my 2.6.34 pet has modules.builtin and modules.order, don't delete.
#100629 if nouveau xorg driver exists, move nv driver to 'drivers-alternate'.
#100801,100806 file inline_get_pet, inline_get_compat have common code.
#100831 hack.
#100903 extract all kernel-specific files to zdrv, to support mix-n-match.
#100911 handle Packages-puppy-lucid-official.
#100911 simple filenames vmlinuz, initrd.gz, puppy.sfs, zdrv.sfs, devx.sfs.
#100912 choose between old and new puppy filenames.
#100916 don't bother record devx sfs name. second help screen at bootup.
#101007 the latest busybox depmod generates relative paths in modules.dep, breaks modprobe in initrd.
#101012 have restored 'depmod-FULL' name to 'depmod'.
#101013 read variable PKG_PREFER_OLDER in file PKGS_MANAGEMENT to choose between 2 pkgs.
#101023 small bugfix. 101024 bugfix. 101025 bugfix.
#101026 display "wary <version>" on boot splash screen.
#101027 Iguleder reported DISTRO_SPECS did not have a carriage-return on last line.
#101028 added ko* as have some named .koHIDE.
#101126 Quirky is now built from wary5 pet packages.
#101217 get latest BaCon in devx.
#101222 some big modems named *HIDE, not *.ko.
#110213 fix if /usr/share/X11/locale/locale.alias missing .utf8 entries.
#110215 extra logic for BaCon compiler in devx.
#110329 move some python files to the 'devx'.
#110412 move all .bac, .pot and .pupdev files to 'devx'.
#110413 variable DISTRO_XORG_AUTO in /etc/DISTRO_SPECS, set in Woof '3builddistro'.
#110417 this will make Thunar use ROX-Filer's icons for files
#110421 vercmp utility.
#110422 DISTRO_VERSION variable now has dotted format. note, also now using full dotted version# in puppy filenames.
#110426 help file on CD for Windows users.
#110429 fix ownership of /root/spot just in case.
#110429 may need a fix, think want glibc scsi headers in usr/include/scsi.
#110512 fix for kernel pet name linux_kernel-2.6.32-40-w5.pet
#110516 attempt fix dependcheck() hanging.
#110517 debian squeeze hack, copy pango-querymodules from devx.
#110517 squeeze debian, boot splash message "squeezed <version>".
#110517 turn on kms for intel,radeon,nouveau if recent versions.
#110518 radical culling of modules, add fbcon.ko.
#110523 override files from rootfs-skeleton/root/Choices/MIME-types, if rox pkg has older files.
#110608 copy Packages-puppy-common-official, not -woof-.
#110612 copy rpm2ppm and mageia2ppm needed by 0setup in built puppy.
#110620 do same as 110523, for mime icons.
#110621 fix for turning on kms.
#110622 hack for mageia 1 (otherwise can't find pango.modules)...
#110622 mageia1 requires pixbuf cache file to be at /usr/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache.
#110701 possible lib clash between e2fsprogs and util-linux (i might use old util-linux pet).
#110702 mageia1 python pkg puts /etc/profile.d/30python.csh in devx.
#110712 modinfo won't work if modules.* files missing.
#110712 reduce hid-* drivers moved to initrd, so when usbhid configured builtin, none be moved to initrd.
#110713 support xz compression for squashfs 4.2 and kernel 2.6.39+.
#110721 Drake Puppy: display "drake <version>" on boot splash screen.
#110726 Drake Puppy: config files put in usr/bin/multiarch-i386-linux, move to usr/bin.
#110727 running drake pup 0.3 with k2.6.39-3 new laptop, mysterious loss of dirs/files. try sync.
#110727 no longer using $PKG_PREFER_OLDER to select kernel-version-sensitive pkg alternatives.
#110803 workaround for 'pngoverlay' needs X runnning, also avoid cpu overhead at first startup.
#110806 if yad installed, no zenity, make sure zenity is symlink to yad.
#110816 add Packages-puppy-slacko-official.
#110821 copy support/findpkgs into build.
#110822 removed fossil_fixup_func. improved choice of KMS on/off.
#110822 customizations script provided by 01micko for Slacko.
#110823 move asking for kernel pet from 3builddistro to 1download.
#110823 revert 110821 (also have removed rootfs-skeleton/usr/local/petget/findwoofinstalledpkgs.sh)
#110823 fix creating file woof-installed-packages.
#110824 add common repo default in PPM, set defaults for slacko.
#110826 no longer calling icon_switcher_cli from rc.update, modify 110803.
#110826 get the kernel version out of inside the PET pkg.
#110831 support png background image.
#110901 i have experimented with building a wary "5.2pre" without libxcb.
#110901 temp hack, if only have gtkdialog4 pkg.
#110903 pixbuf loader cache fix.
#110904 revert 110901 libxcb hack.
#110906 fix syntax error.
#110907 rcrsn51: network printing, need pdftops symlink.
#110910 make sure gtkdialog links to latest.
#110911 'drivers-alternate' moved from /usr/lib/xorg/modules/ to /usr/lib/x/.
#110912 modify jwm to use menu icon 'start-button.png' if present.
#110917 support file prefix 'racy'.
#110918 kernel-skeleton now has a pinstall.sh script.
#110918 radeon, default kms off. testers found can't shutdown with it on.
#110923 avoid conflict between busybox applets and full utilities.
#110928 boot splash image for Racy. 111003 racy logo.
#111004 for 110923, need to check in devx also.
#111006 generic logo mechanism for all pups. replaces 091206 100516 111003
#111010 set desktop font.
#111014 support 3.x kernel.
#111026 davids45: /tmp needs 777 permissions. i do have this in rootfs-skeleton, but lost in sandbox3/rootfs-complete
#111027 revert 110907. was symlinking to pdftops from poppler pkg, but must have actual pdftops from cups pkg.
#111114 support older gtk (2.20.1) for pixbuf loaders in new location.
#111121 noryb009: improve arch linux support, patched: 0setup, 1download, 2createpackages, 3builddistro, DISTRO_COMPAT_REPOS-arch, support/arch/runDotInstalls
#111123 support cross-build.
#111127 no need to copy 'printcols', 'vercmp' to target build, now done in 'merge2out' script.
#111203 remove offer to download and compile bacon (no good for cross-builds).
#111204 remove zdump from list.
#120215 language translations for init script.
#120216 set default LANG and KMAP. 120217 recommend UTF-8.
#120217 translate early-boot splash screens... (expect building with langpack)
#120224 move rootfs-complete/usr/share/doc/nls to 'devx'.
#120225 DISTRO_PUPPYDATE -> DISTRO_SPECS. this is read by /usr/share/doc/convert-raw-docs. (note: this script no longer exists)
#120305 copy Packages-puppy-noarch-official to build.
#120312 removed Packages-puppy-common-official from ppm default selection.
#120315 Ubuntu Precise Pangolin has done a big move of files from /lib to /lib/i386-linux-gnu and /usr/lib to /usr/lib/i386-linuxgnu
#120331 bugfix generating README.HTM on cd.
#120401 improve choosing locale.
#120502 more support for arm build.
#120506 support creation of SD-card image (for arm build).
#120506b check that sd card big enough.
#120510 offer to resize 2nd partition of SD card to fill card.
#120521 for SD-image build, copy-only console fonts and keyboard layouts to initrd, copy-only kernel modules to initrd.
#120522 precise puppy, seamonkey was crashing. needed /usr/share/glib-2.0/schemas/apps.gecko-mediaplayer.preferences, also must be compiled.
#120523 precise puppy, may also need to run gio-querymodules. (refer also installpkg.sh, rc.update)
#120526 try radeon kms default on. 120605 turn off again.
#120605 shinobar: generate gtk.immodules. ref: http://murga-linux.com/puppy/viewtopic.php?t=76961&start=195
#120613 kernel image can have different names. rename to vmlinuz, however change back later if build sd card.
#120616 check dri drivers exist before offer kernel modesetting.
#120618 raspi debian squeeze, links as main browser, helpsurfer html viewer, gtkmoz missing. 120620 modify.
#120628 certain utilities are not appropriate when booting on an arm board.
#120703 sd card: allow 2nd partition to be ext2, ext3 or ext4.
#120704 ask if want to load swap partition/file at bootup.
#120704 sd card image, ext2 f.s., offer to bump to ext4 (without journal).
#120705 refine upgrade ext2 to ext4. 120706 disktype misreports ext4 without journal as ext2.
#120707 change relatime to noatime. see also rc.sysinit.
#120708 added BOOT_QUICKSETUP='quickarm'. quickarm is symlink to /usr/sbin/quicksetup. leave out X config frame for arm boards.
#120709 fix for pci.ids and usb.ids in "wrong" place. new BOOT_UDEVDCHILDREN.
#120714 revert 120708. instead, BOOT_BOARD (= raspi, mele, odroidx, etc.) read by quicksetup.
#120720 raspbian wheezy has files in /lib/arm-linux-gnueabihf and /usr/lib/arm-linux-gnueabihf
#120721 support ubuntu and debian arm multiarch.
#120723 BOOT_DISABLEXORGWIZARD variable.
#120724 'alsaconf' does not work on arm boards, BOOT_DISABLEALSACONF variable. refer /usr/sbin/alsawizard.
#120830 my hunspell-en-us pet has pinstall.sh that creates symlinks of en_US.aff and en_US.dic from seamonkey and firefox. however, need to do this thoroughly.
#120831 simplify repo-list radiobuttons for ppm.
#120903 default radeon kms on again.
#120909 multiarch support for /usr/bin. (see also 2createpackages).
#120922 was getting "nv" listing twice (precise puppy) for xorg drivers-alternate.
#120925 ignore debian/ubuntu multiarch circular symlink (these are create by 2createpackages), when creating builtin_files lists.
#121010 fix for 120925.
#121022 fix for python not in devx, in main f.s.
#121028 copy devx-only-installed-packages to build. (refer findwoofinstalledpkgs)
#121102 file DISTRO_SPECS has new variable DISTRO_DB_SUBNAME. ex: for 14.0-based slacko, DISTRO_DB_SUBNAME=slacko14
#121105 fix enabled-repo list for ppm.
#121111 debdb2pupdb, new fast deb to pup db converter, called by 0setup.
#121123 changed default locale from en_US to en_US.UTF-8.
#121123 some chroot operations may leave something inside sandbox3/rootfs-complete/tmp, delete.
#121123 new variable DISTRO_ARCHDIR in /etc/DISTRO_SPECS
#121203 /usr/sbin/welcome1stboot needs this lib: /usr/lib/libgtk-x11-2.0.so (symlink was only in devx).
#121210 fix, ARCHDIR was not getting set.
#121212 puninstall.sh was not getting removed from root of sandbox3/rootfs-complete.
#121227 initrd: if kernel has f.s. drivers as modules (quirky6), added ext2.ko ext3.ko ext4.ko fat.ko msdos.ko vfat.ko ntfs.ko reiserfs.ko udf.ko, and deps: jbd.ko mbcache.ko jbd2.ko
#130111 proper handling of multiarch for mageia (refer 2createpackages).
#130112 multiarch symlinks now optional.
#130114 revert 130112.
#130129 offer to change X dpi from 78 to 96.
#130129 busybox: deb 'bsdmainutils' has broken 'cal' and 'hd' symlinks. 130130 tweak.
#130203 fix /tmp permissions.
#130204 01micko: fix for spaces in names.
#130226 hack for missing /usr/bin/X.
#130306 fix arch linux build. 130307 fix arch post-install script execution.
#130314 want to run arch post-install script from ppm also.
#130316 disable execution of arch install scripts.
#130316 now have rootfs-complete/usr/local/petget/invaliddepslist.
#130320 util-linux libblkid and libuuid have preference over e2fsprogs.
#130321 fix call to slacko customisation script.
#130326 X dpi now 96. 130327 offer choice of next-larger.
#130329 append extra line, needed for Qt4 theme matching. (see also /etc/profile.d/pup_gtk and /root/.config/Trolltech.conf).
#130330 Xorg: move modesetting_drv.so to drivers-alternate directory.
#130330 want 'universe' radiobutton before 'main'.
#130401 01micko: support gtk3 themes (see also 130329).
#130404 01micko: link to actual gtk3 theme.
#130418 support kmod. but, still use old depmod from module-init-tools in initrd (as unable to compile kmod statically).
#130430 coreutils 8.20 cp outputs different error message.
#130430 fix depmod symlink.
#130430 gpu/drm/sis.ko needs video/sis/sisfb.ko, must keep. WARNING: sisfb is blacklisted -- as mucks up my pc that has sis video and only vesa works.
#130510 support multiarch for debian wheezy.
#130512 scsijon: dpi size setting not working.
#130515 ask disable 'pupdesk.flg' mechanism, see also init script in initrd.
#130530 write puppy files to SD image file, not to card.
#130601 new script support/setup-spot, optional run internet apps as user 'spot'.
#130604 tweak 130601.
#130612 support devtmpfs. see also 'init' script in initrd.
#130613 kmod depmod wants modules.builtin, modules.order.
#130618 modify 130612.
#130618 restore the "layer:" lines in /etc/xdg/templates/_root_.jwmrc.
#130710 01micko: DEVTMPFSFLG bug fix.
#130720 add QtWeb option to run as spot. 130721 fixes.
#130723 add opera option to run as spot.
#130725 if alternate-browser, set defaulthtmlviewer to it. 130726 fix.
#130821 add firefox option to run as spot.

#v431 accepts passed params, from woof_gui_tabs:
#$1=$CHOICE_KERNELPKG $2=$CHOICE_SCSI $3=$CHOICE_BIGMODEM $4=$CHK_FB_STATE $5=$CHK_EXOTIC_STATE $6=$CHK_RADICAL_STATE
#100912 add $7=CHK_SIMPLE_FILENAMES

export LANG=C #faster.

#w482 use xorriso if it is installed (see also functions4puppy)...
CDRECORD='cdrecord'
MKISOFS='mkisofs'
if [ "`which xorriso`" != "" ];then
 CDRECORD='xorrecord'
 MKISOFS='xorrisofs' #growisofs reads this variable.
fi
export CDRECORD MKISOFS

RARCH=`uname -m` #running arch

. ./DISTRO_SPECS      #this has DISTRO_VERSION.
#140619
if [ "$DISTRO_KERNEL_PET" = "Huge_Kernel" ];then
 echo "You can not use this script. Use 3builddistro-Z instead or click the button"
 echo "named 'HUGE BUILD DISTRO' in the Woof GUI"
 echo "Press enter to exit."
 read huge_build
 exit 0
fi

if [ ! "$DISTRO_DB_SUBNAME" ];then #121102
 DISTRO_DB_SUBNAME="$DISTRO_COMPAT_VERSION" #fallback if DISTRO_DB_SUBNAME not defined in file DISTRO_SPECS.
 echo '#subname for online PETs dir. Ex: "slacko14", dir "pet_packages-slacko14", db file "Packages-puppy-slacko14-official":
#note: prior to existence of this variable, online subname was set to $DISTRO_COMPAT_VERSION or via some hack code.
' >> ./DISTRO_SPECS
 echo "DISTRO_DB_SUBNAME='${DISTRO_DB_SUBNAME}'" >> ./DISTRO_SPECS
fi
if [ -f ./DISTRO_COMPAT_REPOS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then
. ./DISTRO_COMPAT_REPOS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
else
. ./DISTRO_COMPAT_REPOS-${DISTRO_BINARY_COMPAT} #130306
fi
if [ -f ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then #w478
. ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} #has FALLBACKS_COMPAT_VERSIONS
else
. ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT} #has FALLBACKS_COMPAT_VERSIONS
fi

. ./PKGS_MANAGEMENT #101013 want variable PKG_PREFER_OLDER.
. ./DISTRO_PET_REPOS #120506 want variable PACKAGELISTS_PET_ORDER

if [ -f WOOFMERGEVARS ];then #111123
. ./WOOFMERGEVARS #has variables WOOF_HOSTARCH, WOOF_TARGETARCH, WOOF_COMPATDISTRO, WOOF_COMPATVERSION
else
 echo 'File WOOFMERGEVARS does not exist. This is created by script
merge2out. Your setup is wrong, quiting.'
 exit
fi

[ "$WOOF_TARGETARCH" = "x86_64" ] && lsuffix=64 || lsuffix= #140121 for 64bit ${lsuffix}

#100911 simple filenames, want idstring...
#IDDATE="`date +%y%m%d%H%M%S`" #12 bytes, ex: 100911153319
#IDPREFIX="`echo -n "$DISTRO_FILE_PREFIX" | cut -b 1`"
#vDISTRO_VERSION="`echo -n "$DISTRO_VERSION" | cut -b 1-3`" #in case it ever gets bigger than 3 chars.
#IDSTRING="${IDPREFIX}${vDISTRO_VERSION}${IDDATE}"
#110422 increase size of idstring to 32 bytes...
IDDATE="`date +%y%m%d%H%M%S`" #12 bytes, ex: 100911153319
IDPREFIX="`echo -n "$DISTRO_FILE_PREFIX" | cut -b 1`"
IDSTRING="${IDPREFIX}${IDDATE}ZZZZ${DISTRO_VERSION}"
#...ZZZZ reserved space for future use.
LEN1=`echo -n "$IDSTRING" | wc -c`
LEN2=`expr 32 - $LEN1`
EXTX=''
while [ $LEN2 -gt 0 ];do
 LEN2=`expr $LEN2 - 1`
 EXTX="${EXTX}X"
done
IDSTRING="${IDSTRING}${EXTX}" #32 bytes ex: w110423092024ZZZZ5.1.2XXXXXXXXXX

#the files that have compatible-distro pkg docs (these were downloaded by 0setup)...
PKGLISTS_COMPAT="`echo "$PKG_DOCS_DISTRO_COMPAT" | tr ' ' '\n' | cut -f 3 -d '|' | tr '\n' ' ' | sed -e 's% $%%'`" #see file DISTRO_PKGS_SPECS-ubuntu
#...format ex: 'Packages-ubuntu-intrepid-main Packages-ubuntu-intrepid-universe'

WKGDIR="`pwd`"

#120506 params passed from woof_gui_tabs. moved up...
KERNELPKG=""
[ $1 ] && KERNELPKG="$1"
CHOICE_SCSI=""
[ $2 ] && CHOICE_SCSI="$2" #Erase_SCSI, Keep_SCSI, Boot_SCSI
CHOICE_BIGMODEM=""
[ $3 ] && CHOICE_BIGMODEM="$3" #Erase_big_modems Keep_big_modems Zdrv_big_modems
CHK_FB_STATE=""
[ $4 ] && CHK_FB_STATE="$4"
CHK_EXOTIC_STATE=""
[ $5 ] && CHK_EXOTIC_STATE="$5"
CHK_RADICAL_STATE=""
[ $6 ] && CHK_RADICAL_STATE="$6"

#120506
echo
echo "Do you want to create a live-CD .iso file, which is the normal choice for a
PC-compatible target, or is your target an SD-card for an ARM-based board?
In the latter case, you would already have downloaded an SD-card skeleton image
file into folder 'sd-skeleton-images' (done by script '1download') -- if there
is no image file in that folder, you cannot choose the SD-card option."
echo "WARNING: for the SD-card option, you must have enough free space in the
current directory ${WKGDIR}/sandbox3
to expand the SD image file, typically 4GB."
echo "Press ENTER only for normal .iso creation, or "
echo -n "any other printable character for SD-card: "
read SDFLAG
#120714 ask what type of arm board...
if [ "$SDFLAG" != "" ];then
 echo "...ok, chose sd-card image."
 echo
 echo "Type number of board that you are building for: 
1 Raspberry Pi
2 Mele A1000 or A2000 (or other A10 board)
3 ODROID-X (or other Exynos 4412 board)"
 read boardnum
 case $boardnum in
  1) BOOT_BOARD='raspi' ;;
  2) BOOT_BOARD='mele' ;;
  *) BOOT_BOARD='odroidx' ;;
 esac
 echo "...you chose $BOOT_BOARD"
fi
if [ "$SDFLAG" != "" ];then
 #override module choices, as kernel is built customized to particular hardware...
 CHOICE_BIGMODEM="Keep_big_modems"
 CHOICE_SCSI="Keep_SCSI"
 CHK_FB_STATE="false"
 CHK_EXOTIC_STATE="false"
 CHK_RADICAL_STATE="false"
 CHK_YES_ZDRV="false"
fi

#w003 remove comments from PKGS_SPECS_TABLE
PKGS_SPECS_TABLE="`echo "$PKGS_SPECS_TABLE" | grep -v '^#'`"

mkdir -p sandbox3

export DISTRO_NAME
export DISTRO_VERSION
if [ "`echo -n "$DISTRO_VERSION" | grep '\.'`" = "" ];then #110422
 DIGIT1="`echo "$DISTRO_VERSION" | cut -b 1`"
 DIGIT2="`echo "$DISTRO_VERSION" | cut -b 2`"
 DIGIT3="`echo "$DISTRO_VERSION" | cut -b 3`"
 RIGHTVER="${DIGIT1}.${DIGIT2}.${DIGIT3}"
 [ "$DIGIT3" = "0" ] && RIGHTVER="${DIGIT1}.${DIGIT2}"
else
 RIGHTVER="$DISTRO_VERSION"
fi

#the kernel is supposed to be a .pet package, already downloaded into packages-pet...
echo
if [ "$KERNELPKG" = "" ];then
 #110823
 KERNELPKG="$DISTRO_KERNEL_PET"
 echo "The kernel pkg that you are using is: $KERNELPKG"
 echo "This is specified in variable DISTRO_KERNEL_PET in file DISTRO_SPECS,"
 echo "which is set in the first GUI tab of script woof_gui, or in script 1download,"
 echo "or you can directly edit DISTRO_SPECS -- but please do so early-on, before"
 echo "running script 1download, as Woof needs to know the kernel version to"
 echo "properly resolve some dependencies, possible packages that are dependent"
 echo "on a certain kernel version."
 echo "Note, the linux_kernel PET needs to be in directory packages-pet."
 echo -n "Press ENTER to continue: "
 read keepgoing
fi

#110826...
#KERNELVER="`echo "$KERNELPKG" | cut -f 2 -d '-'`"
##um, if it is named 'linux-2.6.29-rc7-*.pet' then get 2 fields...
#[ "`echo -n "$KERNELPKG" | grep '\-rc[0-9]\-'`" != "" ] && KERNELVER="`echo "$KERNELPKG" | cut -f 2,3 -d '-'`"
##100204 may be like this 2.6.33-rc6-git2...
#[ "`echo -n "$KERNELPKG" | grep '\-git[0-9]\-'`" != "" ] && KERNELVER="`echo "$KERNELPKG" | rev | grep -o '[0-9]tig\-.*' | rev | cut -f 2- -d '-'`"
##100209 special case...
#[ "`echo -n "$KERNELPKG" | grep '\-vortex86'`" != "" ] && KERNELVER="`echo "$KERNELPKG" | cut -f 2,3 -d '-'`"
##110512 special case... format linux-2.6.32-40-w5
#dotnum="`echo -n "$KERNELVER" | sed -e 's%[^\.]%%g' | wc -c`"
#if [ "$dotnum" = "2" ];then
# SUB_SUB_KERNELVER=`echo -n "$KERNELPKG" | cut -f 3 -d '-'`
#else
# SUB_SUB_KERNELVER=`echo -n "$KERNELVER" | cut -f 1 -d '-' | cut -f 4 -d '.'`
#fi
#SUB_KERNELVER=`echo -n "$KERNELVER" | cut -f 1 -d '-' | cut -f 3 -d '.'`

#110826 #131124 xz
KERNELVER="`tar --list -f packages-pet/${KERNELPKG} 2>/dev/null | grep -o '/lib/modules/[23]\..*' | head -n 1 | cut -f 4 -d '/'`" #120502 hide error msg.
dotnum="`echo -n "$KERNELVER" | sed -e 's%[^\.]%%g' | wc -c`"
if [ "$dotnum" = "2" ];then
 #SUB_SUB_KERNELVER=`echo -n "$KERNELPKG" | cut -f 3 -d '-'`
 SUB_SUB_KERNELVER=0 #cos we don't really know what it is.
else
 SUB_SUB_KERNELVER=`echo -n "$KERNELVER" | cut -f 1 -d '-' | cut -f 4 -d '.'`
fi
SUB_KERNELVER=`echo -n "$KERNELVER" | cut -f 1 -d '-' | cut -f 3 -d '.'`
MAJ_KERNELVER=`echo -n "$KERNELVER" | cut -f 1 -d '-' | cut -f 1 -d '.'` #111014 2 or 3.

echo
echo "You have chosen $KERNELPKG, which is version $KERNELVER."
#echo -n "Press ENTER to continue: "
#read continue1

#if >= 2.6.29 kernel, use mksquashfs v4.0...
if [ "$MAJ_KERNELVER" = "2" ];then
 MKSQUASHFS='mksquashfs3.3'
 [ $SUB_KERNELVER -ge 29 ] && MKSQUASHFS='mksquashfs4.0'
 [ $SUB_KERNELVER -ge 39 ] && MKSQUASHFS='mksquashfs4.2' #110713
 #[ $SUB_KERNELVER -eq 18 ] && MKSQUASHFS='mksquashfs3.1' #w478
 #[ $SUB_KERNELVER -eq 18 ] && MKSQUASHFS='mksquashfs3.2' #w478 old 2.6.18.1 kernel
 [ $SUB_KERNELVER -eq 18 ] && MKSQUASHFS='mksquashfs3.3' #w479 2.6.18.8 kernel
 [ $SUB_KERNELVER -eq 18 ] && [ $SUB_SUB_KERNELVER -eq 1 ] && MKSQUASHFS='mksquashfs3.2' #w479
 [ $SUB_KERNELVER -eq 27 ] && [ $SUB_SUB_KERNELVER -gt 46 ] && MKSQUASHFS='mksquashfs4.0' #100607
else #111014
 MKSQUASHFS='mksquashfs4.2'
fi

SQUASHFSMAJOR="`echo -n "$MKSQUASHFS" | cut -f 1 -d '.' | rev | cut -c 1`" #3 or 4.

if [ "$MKSQUASHFS" = "mksquashfs4.2" ];then #110713
 echo
 echo ".sfs files are normally compressed with gzip."
 echo "However, this kernel supports xz, which is higher compression."
 echo -n "Press ENTER only for gzip, any other char for xz: "
 read XZCHOICE
 if [ "$XZCHOICE" ];then
  echo "...xz chosen"
  COMPCHOICE='-comp xz -Xbcj x86 -b 512K'
 else
  echo "...gzip chosen"
  COMPCHOICE=''
 fi
fi

#110823
echo
./findwoofinstalledpkgs #creates woof-installed-packages, which later gets copied into sandbox3/rootfs-complete/root/.packages/
#121028 ...now also creates devx-only-installed-packages.

#100912 choose simple or traditional filenames...
#traditional names...
PREFIX1CHAR="`echo -n "$DISTRO_FILE_PREFIX" | cut -c 1`"
KERNEL3CHARS="`echo -n "$KERNELVER" | tr -d '.' | tr -d '\-' | tr -d '[a-z]' | rev | cut -c 1,2,3 | rev`"
#tradZDRVSFS="z${PREFIX1CHAR}${DISTRO_VERSION}${KERNEL3CHARS}.sfs" #ex: zw071629.sfs
#tradDEVXSFS="${DISTRO_FILE_PREFIX}_devx_${DISTRO_VERSION}.sfs" #ex: wary_devx_071.sfs
##tradPUPPYSFS="${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}.sfs" #ex: wary-071.sfs
##tradZDRVSFS="zdrv_${DISTRO_VERSION}.sfs"
##tradDEVXSFS="devx_${DISTRO_VERSION}.sfs"
#tradPUPPYSFS="${DISTRO_FILE_PREFIX}_${DISTRO_VERSION}.sfs"
#110422 new traditional names...
tradPUPPYSFS="puppy_${DISTRO_FILE_PREFIX}_${DISTRO_VERSION}.sfs"
tradZDRVSFS="zdrv_${DISTRO_FILE_PREFIX}_${DISTRO_VERSION}.sfs"
tradADRVSFS="adrv_${DISTRO_FILE_PREFIX}_${DISTRO_VERSION}.sfs"
tradYDRVSFS="ydrv_${DISTRO_FILE_PREFIX}_${DISTRO_VERSION}.sfs"
tradDEVXSFS="devx_${DISTRO_FILE_PREFIX}_${DISTRO_VERSION}.sfs"

if [ $7 ];then #passed in from woof_gui
 CHK_SIMPLE_FILENAMES="$7"
else
 echo
 echo "Woof can build to use traditional or simplified filenames."
 echo "The simplified filenames do not have any distro or version information in their"
 echo "names: vmlinuz, initrd.gz, puppy.sfs, zdrv.sfs"
 echo "The traditional names look like this:"
 echo "vmlinuz, initrd.gz, ${tradPUPPYSFS}, ${tradZDRVSFS}"
 echo "Note that there are many apps that use the traditional names, such as CD"
 echo "remaster applications, though the official Woof scripts should be all working"
 echo "with both naming schemes. To complicate matters, there is some variation in"
 echo "the traditional names with different versions of Puppy, so best to use scripts"
 echo "that have been updated to work will the names stored in file DISTRO_SPECS: so"
 echo "note that the chosen names are available in a running Puppy in /etc/DISTRO_SPECS"
 echo "as these variables: DISTRO_PUPPYSFS, DISTRO_ZDRVSFS"
 echo -n "Press ENTER only for simplified names (any char otherwise): "
 read read_simple
 CHK_SIMPLE_FILENAMES='true' #simple.
 [ "$read_simple" ] && CHK_SIMPLE_FILENAMES='false'
fi
if [ "$CHK_SIMPLE_FILENAMES" = "true" ];then
 echo '...simple chosen'
 PUPPYSFS='puppy.sfs'
 ZDRVSFS='zdrv.sfs'
 ADRVSFS='adrv.sfs'
 YDRVSFS='ydrv.sfs'
 DEVXSFS='devx.sfs'
else
 echo '...traditional chosen'
 PUPPYSFS="$tradPUPPYSFS"
 ZDRVSFS="$tradZDRVSFS"
 ADRVSFS="$tradADRVSFS"
 YDRVSFS="$tradYDRVSFS" 
 DEVXSFS="$tradDEVXSFS"
fi

#now build rootfs-complete, the main filesystem...
echo
echo "Now building sandbox3/rootfs-complete, with the complete filesystem..."
echo '#!/bin/sh' > sandbox3/pinstall.sh
rm -rf sandbox3/rootfs-complete
sync
mkdir sandbox3/rootfs-complete
mkdir sandbox3/rootfs-complete/etc
#no, confusing, assign to history...
#echo -n "$DISTRO_VERSION" > sandbox3/rootfs-complete/etc/puppyversion #for backwards compat. old scripts.
cp DISTRO_SPECS sandbox3/rootfs-complete/etc/

#copy the skeleton...
cp -a rootfs-skeleton/* sandbox3/rootfs-complete/
cat sandbox3/rootfs-complete/pinstall.sh >> sandbox3/pinstall.sh
# extra packages
# extra packages
for d in $(ls rootfs-packages)
do  
       [ "$d" = "change_kernels" ] && continue # can't work in legacy build
       state=true
       LIST="sfs_load|pfilesearch|pfind|firewall_ng"
       echo "$d" | egrep -q "$LIST" && def=true || def=false 
       case "$d" in
		jwm_config|ptheme)
			if grep 'jwmconfig' DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} |\
			grep -q '^yes' ;then
				state=false #cant choose them if jwmconfig is in specs.
			else
				def=true
			fi
			;;
	   *)
			if grep "$d" DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} |\
			grep -q '^yes' ;then
				state=false # don't overwrite user chosen specs
				def=false
			fi
			;;	
	   esac
       CBOXES="$CBOXES""<hbox space-expand=\"true\" space-fill=\"true\">
		<checkbox>
			<label>$d</label>
			<default>$def</default>
			<variable>$d</variable>
			<sensitive>$state</sensitive>
		</checkbox>
	</hbox>"
done
CHOICE=/tmp/rootfs_choice$$	
export GUIC="<window title=\"Choose extra packages\">
	<vbox>
		<hbox>
			<text>
				<label>Here you can choose what extra packages to install. It is recommended that you leave the checked boxes checked as these are usually dependencies of installed programs in puppy. If the checkbox for a program is disabled then it means that a coressponding program is in DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}</label>
			</text>
		</hbox>
		<hbox space-expand=\"true\" space-fill=\"true\">	
			<text use-markup=\"true\">
				<label>\"<b>These are maintained as part of woof.</b>\"</label>
			</text>
		</hbox>
		<vbox scrollable=\"true\" height=\"160\" width=\"380\">
			$CBOXES
		</vbox>
		<hbox><button ok></button></hbox>
	</vbox>
</window>"
gtkdialog -p GUIC -c > $CHOICE
. $CHOICE
[ -f /tmp/rootfs-packages.specs ] && rm /tmp/rootfs-packages.specs # in case we baled out on last build
if [ "$EXIT" = "OK" ];then
	while read line
	do 
		if echo $line|grep -q "true" ;then
			echo "copying ${line%%=*}"
			cp -a rootfs-packages/${line%%=*}/* sandbox3/rootfs-complete/
			[ -f rootfs-packages/${line%%=*}/pinstall.sh ] &&\
				cat rootfs-packages/${line%%=*}/pinstall.sh >> sandbox3/pinstall.sh
			cat sandbox3/rootfs-complete/pet.specs >> /tmp/rootfs-packages.specs
		else
			continue
		fi
	done < $CHOICE
fi
rm $CHOICE
./findwoofinstalledpkgs # moved to here so we can add /tmp/rootfs-packages.specs

echo "Continuing... please wait"

rm -f sandbox3/rootfs-complete/pinstall.sh

rm -f /tmp/3builddistro_removed_alt 2>/dev/null #101013


#120315 Ubuntu Precise Pangolin has done a big move of files from /lib to /lib/i386-linux-gnu and /usr/lib to /usr/lib/i386-linuxgnu
#well, i want to move them back. symlinks will do it (cp below does not destroy them)...
#120316 note, see also 2createpackages.
#if [ ! -e packages-${DISTRO_FILE_PREFIX}/glibc/lib/libc.so.6 ];then
# #ARCHDIR="`find packages-${DISTRO_FILE_PREFIX}/glibc/lib -mindepth 2 -maxdepth 2 -name 'libc.so.6' | head -n 1 | rev | cut -f 2 -d '/' | rev`"
# ARCHDIR=''
# case $WOOF_TARGETARCH in #see file WOOFMERGEVARS
#  x86) ARCHDIR='i386-linux-gnu' ;;
#  x86_64) ARCHDIR='x86_64-linux-gnu' ;;
#  arm) #120720 120721
#   case $WOOF_COMPATDISTRO in #see file WOOFMERGEVARS
#    raspbian) ARCHDIR='arm-linux-gnueabihf' ;;
#    debian)
#     case $WOOF_COMPATVERSION in
#      squeeze) true ;; #old release
#      *) ARCHDIR='arm-linux-gnueabihf' ;;
#     esac
#    ;;
#    ubuntu)
#     case $WOOF_COMPATVERSION in
#      jaunty|karmic|lucid) true ;; #old releases.
#      *) ARCHDIR='arm-linux-gnueabihf' ;;
#     esac
#    ;;
#   esac
#  ;;
# esac
# if [ "$ARCHDIR" ];then #ex: i386-linux-gnu
#  ln -snf ./ sandbox3/rootfs-complete/lib/${ARCHDIR}
#  ln -snf ./ sandbox3/rootfs-complete/usr/lib/${ARCHDIR}
#  ln -snf ./ sandbox3/rootfs-complete/usr/bin/${ARCHDIR} #120909
# fi
#fi
#121210 fix, ARCHDIR was not getting set...
ARCHDIR=''
if [ "$WOOF_COMPATDISTRO" = "ubuntu" ] || [ "$WOOF_COMPATDISTRO" = "trisquel" ];then
 case $WOOF_COMPATVERSION in
  jaunty|karmic|lucid|dwyn|awen|taranis) #old releases.
   true
  ;;
  *)
   case $WOOF_TARGETARCH in
    x86) ARCHDIR='i386-linux-gnu' ;;
    x86_64) ARCHDIR='x86_64-linux-gnu' ;;
   esac
  ;;
 esac
fi
if [ "$WOOF_COMPATDISTRO" = "debian" ];then #130510
 case $WOOF_COMPATVERSION in
  squeeze|lenny|etch|sarge|woody) #old releases.
   true
  ;;
  *) #wheezy and later...
   case $WOOF_TARGETARCH in
    x86) ARCHDIR='i386-linux-gnu' ;;
    x86_64) ARCHDIR='x86_64-linux-gnu' ;;
   esac
  ;;
 esac
fi
if [ "$WOOF_COMPATDISTRO" = "debian" ];then
  case $WOOF_TARGETARCH in
   x86) ARCHDIR='i386-linux-gnu' ;;
   x86_64) ARCHDIR='x86_64-linux-gnu' ;;
  esac
fi
if [ "$WOOF_COMPATDISTRO" = "mageia" ];then #130111
 case $WOOF_TARGETARCH in
  x86) ARCHDIR='multiarch-i386-linux' ;;
  *) ARCHDIR=''; echo 'dunno'; exit ;;
 esac
fi
case $WOOF_TARGETARCH in
 arm)
  case $WOOF_COMPATDISTRO in #see file WOOFMERGEVARS
   raspbian) ARCHDIR='arm-linux-gnueabihf' ;; #different from debian/ubuntu, compiled for armv6 hf.
   debian)
    case $WOOF_COMPATVERSION in
     squeeze) true ;; #old release
     *) ARCHDIR='arm-linux-gnueabihf' ;;
    esac
   ;;
   devuan)
    ARCHDIR='arm-linux-gnueabihf'
   ;;
   ubuntu|trisquel)
    case $WOOF_COMPATVERSION in
     jaunty|karmic|lucid|dwyn|awen|taranis) true ;; #old releases.
     *) ARCHDIR='arm-linux-gnueabihf' ;;
    esac
   ;;
  esac
 ;;
esac
#130114 revert 130112...
if [ "$ARCHDIR" ];then #ex: i386-linux-gnu
# #130112 multiarch symlinks now optional...
# if [ "$DISTRO_ARCHDIR_SYMLINKS" = "" ];then #this now created in 2createpackages.
#  echo
#  echo "Woof-built puppies up until Dec 2012 have multiarch dirs as symlinks,
#for example /usr/lib/i386-linux-gnu is a symlink to /usr/lib in Debian/Ubuntu
#based pups. However, in future builds will by default retain the multiarch
#sub-directories."
#  echo -n "ENTER only to retain multiarch dirs: "
#  read flagmultiarchdirs
# else
#  flagmultiarchdirs=""
#  [ "$DISTRO_ARCHDIR_SYMLINKS" = "yes" ] && flagmultiarchdirs="yes"
# fi
# if [ "$flagmultiarchdirs" != "" ];then
#  echo "...Creating multiarch symlinks"
#  ln -snf ./ sandbox3/rootfs-complete/lib/${ARCHDIR}
#  ln -snf ./ sandbox3/rootfs-complete/usr/lib/${ARCHDIR}
#  ln -snf ./ sandbox3/rootfs-complete/usr/bin/${ARCHDIR} #120909
#  xDISTRO_ARCHDIR_SYMLINKS='yes'
# else
#  echo
#  echo "...Multiarch directories retained"
#  mkdir -p sandbox3/rootfs-complete/lib/${ARCHDIR}
#  echo "/lib/${ARCHDIR}" >> sandbox3/rootfs-complete/etc/ld.so.conf
#  #do we really need to populate LD_LIBRARY_PATH if already in ld.so.conf?
#  libPTN="s%^LD_LIBRARY_PATH=\"%LD_LIBRARY_PATH=\"/lib/${ARCHDIR}:%"
#  sed -i -e "$libPTN" sandbox3/rootfs-complete/etc/profile
#  mkdir -p sandbox3/rootfs-complete/usr/lib/${ARCHDIR}
#  echo "/usr/lib/${ARCHDIR}" >> sandbox3/rootfs-complete/etc/ld.so.conf
#  libPTN="s%^LD_LIBRARY_PATH=\"%LD_LIBRARY_PATH=\"/usr/lib/${ARCHDIR}:%"
#  sed -i -e "$libPTN" sandbox3/rootfs-complete/etc/profile
#  mkdir -p sandbox3/rootfs-complete/usr/bin/${ARCHDIR}
#  binPTN="s%^PATH=\"%PATH=\"/usr/bin/${ARCHDIR}:%"
#  sed -i -e "$binPTN" sandbox3/rootfs-complete/etc/profile
#  xDISTRO_ARCHDIR_SYMLINKS='no'
# fi
 ln -snf ./ sandbox3/rootfs-complete/lib/${ARCHDIR}
 ln -snf ./ sandbox3/rootfs-complete/usr/lib/${ARCHDIR}
 ln -snf ./ sandbox3/rootfs-complete/usr/bin/${ARCHDIR} #120909
fi


PKGLIST="`echo "$PKGS_SPECS_TABLE" | grep '^yes' | cut -f 2 -d '|' | tr '\n' ' '`"
for ONEPKG in $PKGLIST
do
 if [ ! -d packages-${DISTRO_FILE_PREFIX}/${ONEPKG} ];then
  if [ ! -d packages-${DISTRO_FILE_PREFIX}/${ONEPKG}_DEV ];then #pkg may go entirely into devx.
   if [ ! -d packages-${DISTRO_FILE_PREFIX}/${ONEPKG}_DOC ];then #pkg may go entirely into docs.
    echo "WARNING, PKG DOES NOT EXIST: packages-${DISTRO_FILE_PREFIX}/${ONEPKG}"
    continue
   fi
  fi
 fi

 #110923 avoid conflict between busybox applets and full utilities...
 [ "$ONEPKG" == "busybox" ] && continue #see below.
 
 #cp -a --remove-destination packages-${DISTRO_FILE_PREFIX}/${ONEPKG}/* sandbox3/rootfs-complete/ 2> /dev/null
 #w003 problem. pet pkg seamonkey_addon_adblock has files in /usr/lib/seamonkey but in rootfs-complete
 #usr/lib/seamonkey is a symlink to seamonkey-1.1.14 and copy fails. this will follow path with a
 #symlink (hopefully doesn't break something else)...
 #looks okay. the --remove-destination ensures that if target file already exists as a symlink
 #it will be deleted first and won't be followed. shit no, it changed /usr/lib/seamonkey into a dir...
 #cp -r --preserve=mode,ownership,timestamps,links --dereference --remove-destination packages-${DISTRO_FILE_PREFIX}/${ONEPKG}/* sandbox3/rootfs-complete/ 2> /dev/null
 #/usr/sbin/petget solves this a different way, let's try that...
 
 #130430 new problem with coreutils 8.20... ex building raring pup...
 #ex: cp -a --remove-destination packages-raring/synclient/* sandbox3/rootfs-complete/
 #coreutils 5.2.1 stderr: cannot overwrite non-directory `sandbox3/rootfs-complete/usr/X11' with directory `packages-raring/synclient/usr/X11'
 #coreutils 8.20 (in Raring) stderr: cp: cannot overwrite non-directory ‘sandbox3/rootfs-complete/usr/X11’ with directory ‘packages-raring/synclient/usr/X11’
 #...!!!! ok, attempt to fix...
 
 cp -a --remove-destination packages-${DISTRO_FILE_PREFIX}/${ONEPKG}/* sandbox3/rootfs-complete/ 2> /tmp/3builddistro-cp-errlog
 sync #110727 running drake pup 0.3 with k2.6.39-3 new laptop, mysterious loss of dirs/files. try sync.
 if [ -s /tmp/3builddistro-cp-errlog ];then #130430 next line fixes those quote chars...
  cat /tmp/3builddistro-cp-errlog | grep 'cannot overwrite non-directory' | tr '[`‘’]' "'" | cut -f 2 -d "'" |
   while read ONEDIRSYMLINK
   do
    xONEDIRSYMLINK="`echo -n "$ONEDIRSYMLINK" | sed -e 's%sandbox3/rootfs-complete%%'`"
    #adding that extra trailing / does the trick...
    cp -a --remove-destination packages-${DISTRO_FILE_PREFIX}/${ONEPKG}${xONEDIRSYMLINK}/* sandbox3/rootfs-complete${xONEDIRSYMLINK}/
   done
 fi
 #...now seems ok. note, there are two places in woof v003 where this problem occurs: /usr/lib/seamonkey
 # and /usr/share/cups/model.
 
 if [ -f sandbox3/rootfs-complete/pinstall.sh ];then
  #note, do not filter #! /bin/sh (with a space)...
  grep  -E -v '^\#\!\/bin\/sh' sandbox3/rootfs-complete/pinstall.sh >> sandbox3/pinstall.sh
  rm -f sandbox3/rootfs-complete/pinstall.sh
 fi
 #get rid of a .specs file...
 rm -f sandbox3/rootfs-complete/*.specs
 #window and menu icons can also be in the top-level directory of the package...
 mv sandbox3/rootfs-complete/*24.xpm sandbox3/rootfs-complete/usr/local/lib/X11/pixmaps/ 2>/dev/null
 mv sandbox3/rootfs-complete/*32.xpm sandbox3/rootfs-complete/usr/local/lib/X11/pixmaps/ 2>/dev/null
 mv sandbox3/rootfs-complete/*32.png sandbox3/rootfs-complete/usr/local/lib/X11/pixmaps/ 2>/dev/null
 mv sandbox3/rootfs-complete/*48.xpm sandbox3/rootfs-complete/usr/local/lib/X11/pixmaps/ 2>/dev/null
 mv sandbox3/rootfs-complete/*48.png sandbox3/rootfs-complete/usr/local/lib/X11/pixmaps/ 2>/dev/null
 mv sandbox3/rootfs-complete/*.xpm sandbox3/rootfs-complete/usr/local/lib/X11/mini-icons/ 2>/dev/null
 mv sandbox3/rootfs-complete/*.png sandbox3/rootfs-complete/usr/local/lib/X11/mini-icons/ 2>/dev/null
done

#110923 fix busybox applets...
DEVXUTILS=' config ar ar86 as as86 audiofile-config autoconf autoheader autom4te automake autopoint autoreconf autoscan autoupdate bacon bacongui bcc bison c2ph catchsegv cc ccmake chem cmake combinediff config_data corelist cpack cpan cpan2dist cpanp cpanp-run-perl cpp ctest cups-config curl-config cvs cvsbug db_archive db_checkpoint db_deadlock db_dump db_hotbackup db_load db_printlog db_recover db_stat db_upgrade db_verify dehtmldiff diet dnsd dprofpp dvdnav-config dvdread-config editdiff elftrunc enc2xs envsubst eqn eqn2graph espdiff filterdiff find2perl fixcvsdiff flex flipdiff freetype-config gccbug gcov gdb gdbserver gdbtui gdiffmk gencat getconf getent gettext gettext.sh gettextize gfortran git git-cvsserver git-receive-pack git-shell git-upload-archive git-upload-pack gitk glade-3 gmake gnome-menu-spec-test gperf gphoto2-config gphoto2-port-config gprof grap2graph grepdiff grn grodvi groff groffer grog grolbp grolj4 grops grotty h2ph h2xs hpftodit iconv idle ifnames indxbib info infokey install-info instmodsh interdiff intltool-extract intltool-merge intltool-prepare intltool-update intltoolize ld ld86 ldd lddlibc4 lex lib-i386 libart2-config libnetcfg libpng-config libpng12-config libtool libtoolize libusb-config lkbib locale localedef lookbib lsdiff m4 make makeinfo man man1 man2dvi man2html mmroff msgattrib msgcat msgcmp msgcomm msgconv msgen msgexec msgfilter msgfmt msggrep msginit msgmerge msgunfmt msguniq mtrace nasm ndisasm neon-config neqn netpbm-config ngettext nm nm86 nroff objcopy objdump objdump86 onsgmls osgmlnorm ospam ospcat ospent osx patch pcap-config pcprofiledump pcre-config pdfroff pdftexi2dvi perl perlbug perldoc perlivp perlthanks pfbtops pic pic2graph piconv pkg-config pl2pm pod2html pod2latex pod2man pod2text pod2usage podchecker podselect post-grohtml pre-grohtml preconv prove psed pstruct ptar ptardiff pydoc ranlib rcs2log readelf recode-sr-latin recountdiff rediff refer rman roff2dvi roff2html roff2pdf roff2ps roff2text roff2x rpcgen s2p sane-config scrollkeeper-config scrollkeeper-extract scrollkeeper-gen-seriesid scrollkeeper-get-cl scrollkeeper-get-content-list scrollkeeper-get-extended-content-list scrollkeeper-get-index-from-docpath scrollkeeper-get-toc-from-docpath scrollkeeper-get-toc-from-id scrollkeeper-install scrollkeeper-preinstall scrollkeeper-rebuilddb scrollkeeper-uninstall scrollkeeper-update shasum size size86 soelim splain splitdiff sprof strace strace-graph strings strip svn svnadmin svndumpfilter svnlook svnserve svnsync svnversion sysprof sysprof-cli tbl texi2dvi texi2pdf texindex tfmtodit troff tzselect unwrapdiff update-mime-database vsyasm whatis xgettext xml2-config xslt-config xsubpp xtrace yacc yasm ytasm zsoelim iconvconfig makewhatis nscd rpcinfo visudo zic ' #11104 111204 remove zdump.
cp -a -f packages-${DISTRO_FILE_PREFIX}/busybox/bin/busybox ./sandbox3/rootfs-complete/bin/
osPTN="s%packages-${DISTRO_FILE_PREFIX}/busybox%%"
for ONEAPPLET in `find packages-${DISTRO_FILE_PREFIX}/busybox -type l | tr '\n' ' '`
do
 ONESPEC="`echo -n "$ONEAPPLET" | sed -e "$osPTN"`"
 ONEPATH="`dirname $ONESPEC`"
 ONENAME="`basename $ONESPEC`"
 FNDFULL="`find ./sandbox3/rootfs-complete/bin ./sandbox3/rootfs-complete/sbin ./sandbox3/rootfs-complete/usr/bin ./sandbox3/rootfs-complete/usr/sbin -name ${ONENAME}`"
 #130129 deb 'bsdmainutils' has broken 'cal' and 'hd' symlinks... 130204 01micko: fix for spaces in names...
 if [ "$FNDFULL" ];then
  if [ -h "$FNDFULL" ];then
   FULLPATH="$(dirname "$FNDFULL")"
   FULLLINK="$(readlink "$FNDFULL")"
   xFULLLINK="${FULLPATH}/${FULLLINK}"
   [ "`echo -n "$FULLLINK" | cut -c 1`" = "/" ] && xFULLLINK="sandbox3/rootfs-complete${FULLLINK}"
   if [ ! -e "${xFULLLINK}" ];then
    rm -f "$FNDFULL" #130130
    FNDFULL=""
   fi
  fi
 fi
 #111004 need to check in devx also...
 if [ ! "$FNDFULL" ];then
  if [ "$ONENAME" != "[" ];then
   if [ "$ONENAME" != "[[" ];then
    oaPTN=" ${ONENAME} "
    FNDFULL="`echo -n "$DEVXUTILS" | grep "$oaPTN"`"
   fi
  fi
 fi
 if [ "$FNDFULL" ];then
  cp -a -f $ONEAPPLET ./sandbox3/rootfs-complete${ONEPATH}/${ONENAME}-BB-NOTUSED
 else
  cp -a -f $ONEAPPLET ./sandbox3/rootfs-complete${ONEPATH}/
 fi
done
[ -f packages-${DISTRO_FILE_PREFIX}/busybox/pinstall.sh ] && cat packages-${DISTRO_FILE_PREFIX}/busybox/pinstall.sh >> sandbox3/pinstall.sh

echo 'echo "END OF pinstall.sh SCRIPT."' >> sandbox3/pinstall.sh
chmod 755 sandbox3/pinstall.sh

#pemasu start 131207
echo "Removing extra documentation"
for i in sandbox3/rootfs-complete/usr/share/doc/*; do
   if [ -d $i ]; then
      keep=0
      for j in legal pmusic pburn pbackup mtpaint help wvdial ffconvert cups nls puplogos notecase didiwiki; do #140128 added back legal, 01micko
         case `basename $i` in
            $j)
               keep=1
               break 1
               ;;
         esac
      done
      [ $keep -eq 0 ] && rm -rf $i
   fi
done
echo "Removing extra locales"
for i in sandbox3/rootfs-complete/usr/share/locale/*; do
   if [ -d $i ]; then
      keep=0
      for j in en en_US locale.alias fi; do
         case `basename $i` in
            $j)
               keep=1
               break 1
               ;;
         esac
      done
      [ $keep -eq 0 ] && rm -rf $i
   fi
done
rm -rf sandbox3/rootfs-complete/usr/share/man/* 2>/dev/null
rm -rf sandbox3/rootfs-complete/usr/share/info/* 2>/dev/null
rm -rf sandbox3/rootfs-complete/usr/share/cups/doc-root/help/* 2>/dev/null
#pemasu end

#run post-install script...
echo
echo "Executing sandbox3/pinstall.sh post-install script..."
cd $WKGDIR/sandbox3/rootfs-complete
SAVEWKGDIR="$WKGDIR"
$WKGDIR/sandbox3/pinstall.sh
echo "If you did not get 'End of pinstall.sh' message above, then"
echo -n "it aborted before completion. Press ENTER or CTRL-C: "
read isitbad
WKGDIR="$SAVEWKGDIR"
cd $WKGDIR

#100316 script to handle improper shutdown, full hd install...
if [ -f sandbox3/rootfs-complete/sbin/initNEW ];then
 rm -f sandbox3/rootfs-complete/sbin/init #a busybox link.
 mv -f sandbox3/rootfs-complete/sbin/initNEW sandbox3/rootfs-complete/sbin/init
fi

#now do dependency check...
dependcheck() {
 echo -n "" > /tmp/missinglibs.txt
 echo -n "" > /tmp/notmissinglibs0.txt
 echo -n "" > /tmp/notusedlibs.txt
 #FNDFILES="`find $WKGDIR/sandbox3/rootfs-complete -type f | tr "\n" " "`"
 #for ONEFILE in $FNDFILES
 find $WKGDIR/sandbox3/rootfs-complete -type f | #110516
 while read ONEFILE #110516
 do
  echo -n '.'
  ISANEXEC="`file --brief $ONEFILE | grep -E "LSB executable|shared object"`"
  if [ ! "$ISANEXEC" = "" ];then
   #need to chroot into rootfs-complete...
    NUMBYTESALL="`echo -n "$ONEFILE" | wc -c | tr -s " " | cut -f 2 -d " "`"
    NUMBYTESHD=`echo -n "$WKGDIR/sandbox3/rootfs-complete" | wc -c | tr -s " " | cut -f 2 -d " "`
    NUMBYTESHD="`expr $NUMBYTESHD + 1`"
    NEWROOTFILE="`echo -n "$ONEFILE" | cut -b $NUMBYTESHD-$NUMBYTESALL`"
   LDDRESULT="`chroot ${WKGDIR}/sandbox3/rootfs-complete ldd ${NEWROOTFILE}`"

   MISSINGLIBS="`echo "$LDDRESULT" | grep "not found" | cut -f 2 | cut -f 1 -d " " | tr "\n" " "`"
   if [ ! "$MISSINGLIBS" = "" ];then
    echo "File $ONEFILE has these missing library files:" >> /tmp/missinglibs.txt
    echo " $MISSINGLIBS" >> /tmp/missinglibs.txt
    echo "The missing libs are in these packages:" >> /tmp/missinglibs.txt
    for ONEMISSING in $MISSINGLIBS
    do
     find ${WKGDIR}/packages-${DISTRO_FILE_PREFIX} -name $ONEMISSING >> /tmp/missinglibs.txt
    done
   fi
   #find all libs not missing...
   NOTMISSING="`file --brief $ONEFILE | grep -v "not found" | grep "shared object"`"
   if [ "$NOTMISSING" != "" ];then
    basename "$ONEFILE" >> /tmp/notmissinglibs0.txt
   fi
  fi
 done
 sync
 sort -u /tmp/notmissinglibs0.txt > /tmp/notmissinglibs.txt
 #now go thru rootfs-complete and see if any unused libs...
 #FNDFILES="`find $WKGDIR/sandbox3/rootfs-complete -type f | tr "\n" " "`"
 #for ONEFILE in $FNDFILES
 find $WKGDIR/sandbox3/rootfs-complete -type f | #110516
 while read ONEFILE #110516
 do
  ISANLIB="`file --brief $ONEFILE | grep "shared object"`"
  if [ ! "$ISANLIB" = "" ];then
   FILEBASE='/'"`basename $ONEFILE`"'$'
   FNDMATCH="`cat /tmp/notmissinglibs.txt | grep "$FILEBASE"`"
   if [ "$FNDMATCH" = "" ];then
    echo "File $ONEFILE is in Puppy but is not used." >> /tmp/notusedlibs.txt
   fi
  fi
 done
}

echo
if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then #111123
 echo "This script can now optionally do a thorough dependency check. "
 echo "Note, this may take a long time."
 echo -e "Press \"c\" key then ENTER to do thorough check,"
 echo -n "else just press ENTER key to bypass check: "
 read mmnn
else
 echo "Normally, the script now offers to perform a thorough dependency check."
 echo "However, as the host and target architectures are different, this cannot"
 echo "be done (host=${WOOF_HOSTARCH}, target=${WOOF_TARGETARCH})."
 echo "If you want to do this check, do a build with host and target the same,"
 echo "with same package selection."
 echo -n "Press ENTER key to continue: "
 read mmnn
 mmnn=""
fi

echo
if [ "$mmnn" = "c" ];then
 echo "Wait awhile..."
 dependcheck
 if [ -s /tmp/missinglibs.txt ];then #true if file not zero size.
  echo
  echo "UNFORTUNATELY, these libs are missing:"
  cat /tmp/missinglibs.txt
  echo
  echo -e "Press \"c\" key then  ENTER key to exit,"
  echo -n "or if you really want to ignore problem, just press ENTER: "
  read goto1
 fi
 [ "$goto1" = "c" ] && exit
 if [ -s /tmp/notusedlibs.txt ];then
  echo
  echo "INTERESTING, the following libs are in Puppy but apparently not used:"
  cat /tmp/notusedlibs.txt
  echo
  echo -e "Press \"c\" key then  ENTER key to exit,"
  echo -n "or if you really want to ignore problem, just press ENTER: "
  read goto1
 fi
 [ "$goto1" = "c" ] && exit
fi
echo "...done."


#now do the kernel...
echo
echo "Now installing the kernel into build/ and modules into rootfs-complete/..."
rm -f sandbox3/$KERNELPKG
KERNPKGNAMEONLY="`basename $KERNELPKG .pet`"
rm -rf sandbox3/$KERNPKGNAMEONLY
cp packages-pet/${KERNELPKG} sandbox3/
cd sandbox3
pet2tgz $KERNELPKG
tar -xf $KERNPKGNAMEONLY.tar.?z #13124 xz
cp -a --remove-destination $KERNPKGNAMEONLY/* rootfs-complete/
#100603 i'm now putting common kernel files in here...
cp -a -f ../kernel-skeleton/* rootfs-complete/

#110918 kernel-skeleton now has a pinstall.sh script... 131207 revert ..131212 revert, revert!
cd rootfs-complete
./pinstall.sh
sync
rm -f ./pinstall.sh
cd ..

mv -f rootfs-complete/etc/modules/firmware.dep rootfs-complete/etc/modules/firmware.dep.${KERNELVER}
sync
rm -rf build
mkdir build
#120613 kernel image can have different names. rename to vmlinuz, however change back later if build sd card...
REALKERNAME='vmlinuz'
[ -f rootfs-complete/boot/vmlinuz ] && cp rootfs-complete/boot/vmlinuz build/
if [ -f rootfs-complete/boot/uImage ];then #mele a1000
 cp rootfs-complete/boot/uImage build/vmlinuz #120506 arm builds, using different name for kernel.
 REALKERNAME='uImage'
fi
if [ -f rootfs-complete/boot/kernel.img ];then #raspberry pi
 cp rootfs-complete/boot/kernel.img build/vmlinuz
 REALKERNAME='kernel.img'
fi
[ -f rootfs-complete/boot/System.map ] && cp rootfs-complete/boot/System.map ./
sync
rm -rf rootfs-complete/boot
echo -n "$IDSTRING" >> build/vmlinuz #100911 16-byte id-string appended. init can determine boot partition.

#110722 need this, to put into woof-installed-packages...
KERNEL_DB_ENTRY="`cat rootfs-complete/pet.specs`"
rm -f rootfs-complete/pet.specs

#arch .INSTALL files
#130316 NOTE: maybe could do this without chroot, as for slackware in 2createpackages (see 2createpackages-hack-install.sh).
# ok, I have looked through all pkgs used in build, most .INSTALL are trivial. these are slight exceptions: 
#  cmake glibc_dev_DEV libglade2 libwmf man_DEV openldap perl_DEV subversion_DEV
# but, don't really need to execute those either. besides, the code below is only operating upon rootfs-complete, it would be better
# to aufs mount the devx, as is done in sandbox2b in 2createpackages. comment out all of below...
if [ "${DISTRO_BINARY_COMPAT}" = "arch" ]; then
 rm -f rootfs-complete/.INSTALL*
# numberOfDotInstall=1
# for FOLDER in `ls -1 ${WKGDIR}/packages-${DISTRO_FILE_PREFIX} | tr '\n' ' '` #130307
# do
#  [ ! -f ${WKGDIR}/packages-${DISTRO_FILE_PREFIX}/${FOLDER}/.INSTALL ] && continue
#  PKGVER="`grep -e '^pkgver' "${WKGDIR}/packages-${DISTRO_FILE_PREFIX}/${FOLDER}/.PKGINFO" | sed 's/^pkgver\ =\ //' | sed 's/-[^-]//'`"
#  cp ${WKGDIR}/packages-${DISTRO_FILE_PREFIX}/${FOLDER}/.INSTALL "rootfs-complete/.INSTALL${numberOfDotInstall}-${PKGVER}"
#  let numberOfDotInstall++
# done
# if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then #130316
#  cp ${WKGDIR}/support/arch/runDotInstalls rootfs-complete/
#  chroot "${PWD}/rootfs-complete" "/runDotInstalls" &> /dev/null
#  rm -f rootfs-complete/runDotInstalls
# else
#  echo "Unfortunately, cannot execute Arch install scripts, as this is a cross-build."
# fi
# rm -f rootfs-complete/.INSTALL*
fi
#130314 want to run arch post-install script from ppm also...
mkdir -p rootfs-complete/usr/local/petget
cp ${WKGDIR}/support/arch/runDotInstalls rootfs-complete/usr/local/petget/ArchRunDotInstalls
chmod 755 rootfs-complete/usr/local/petget/ArchRunDotInstalls

if [ "$CHOICE_SCSI" = "" ];then
 #w462 true-scsi drives
 echo
 echo "Would you like the live-CD to be able to recognise and even boot from"
 echo "old true-SCSI hard drives and CD drives?"
 echo "Most likely you would just press the ENTER key only to decline, as the"
 echo "vast majority of PCs do not have true-SCSI hardware. However, 10-20 years"
 echo "ago it was popular in network server computers."
 echo "Note, really old pre-PCI computers will not be supported at all."
 echo -n "Enter only to decline, any other key for SCSI drivers in live-CD: "
 read wantscsi
 CHOICE_SCSI="Erase_SCSI"
 [ "$wantscsi" != "" ] && CHOICE_SCSI="Boot_SCSI" 
fi

#130613 kmod depmod wants these two... they are moved in later, but do it here also...
mkdir -p rootfs-complete/lib/modules/$KERNELVER
[ -f rootfs-complete/etc/modules/modules.builtin ] && cp -a -f rootfs-complete/etc/modules/modules.builtin rootfs-complete/lib/modules/$KERNELVER/
[ -f rootfs-complete/etc/modules/modules.order ] && cp -a -f rootfs-complete/etc/modules/modules.order rootfs-complete/lib/modules/$KERNELVER/

#110712 modinfo won't work if modules.* files missing...
#130418 if kmod, do not use busybox depmod...
USINGKMOD='no'
[ "`grep '^kmod' ../woof-installed-packages`" != "" ] && USINGKMOD='yes'
if [ "$USINGKMOD" = "no" ];then
 cp -f ../boot/depmod ./
 if [ ! -f rootfs-complete/lib/modules/$KERNELVER/modules.dep ];then
  busybox depmod -b $WKGDIR/sandbox3/rootfs-complete -F $WKGDIR/sandbox3/System.map $KERNELVER
 fi
else
 cp -f ../boot/kmod ./
 ln -snf kmod depmod
 if [ ! -f rootfs-complete/lib/modules/$KERNELVER/modules.dep ];then
  ./depmod -b $WKGDIR/sandbox3/rootfs-complete -F $WKGDIR/sandbox3/System.map $KERNELVER
 fi
fi

SCSIFLAG=""
SCSIDRVS="parport.ko"
if [ "$CHOICE_SCSI" = "Boot_SCSI" ];then #note, further down, scsi modules get moved to initrd.
 SCSIFLAG="-SCSI" #used in name of .iso file.
 #mkdir -p initrd-tree/lib/modules/$KERNELVER/kernel/drivers
 #cp -a -f rootfs-complete/lib/modules/$KERNELVER/kernel/drivers/scsi initrd-tree/lib/modules/$KERNELVER/kernel/drivers/
 for ONESCSI in `find rootfs-complete/lib/modules/$KERNELVER/kernel/drivers/scsi -type f -name \*.ko`
 do
  #really only want those with pci interface...
  SCSIBASE="`basename $ONESCSI`"
  SCSINAMEONLY="`basename $ONESCSI .ko`"
  ALIASFND="`modinfo -b rootfs-complete -k ${KERNELVER} ${SCSINAMEONLY} 2>/dev/null | grep '^alias:'`"
  if [ "$ALIASFND" ];then
   SCSIDRVS="$SCSIDRVS $SCSIBASE"
   #add any deps to list...
   SCSIDEPS="`modinfo -b rootfs-complete -k ${KERNELVER} ${SCSINAMEONLY} 2>/dev/null | grep '^depends:' | head -n 1 | tr -s ' ' | cut -f 2 -d ' ' | sed -e 's%,%.ko %g' -e 's%$%.ko%'`"
   [ "$SCSIDEPS" != ".ko" ] && SCSIDRVS="$SCSIDRVS $SCSIDEPS"
  fi
 done
fi

##w479 ask about removing old analog modem modules...
#INTELMODEMMOD="`find rootfs-complete/lib/modules/$KERNELVER/kernel/drivers/char -type f -name Intel*ko*`"
#if [ "$INTELMODEMMOD" != "" ];then
# echo
# echo "Would you like to remove the Intel 536 and 537 analog modem modules?"
# echo "These are an enormous waste of space (1MB and 4MB uncompressed) if you"
# echo "don't have one of those particular old modem dialup cards."
# echo -n "Press ENTER only to delete them: "
# read DELINTEL
# if [ "$DELINTEL" = "" ];then
#  for ONEMODEM in $INTELMODEMMOD
#  do
#   echo "Removing $ONEMODEM"
#   rm -f $ONEMODEM
#  done
# fi
#fi

if [ "$CHOICE_BIGMODEM" != "Erase_big_modems" ];then #v431
# INTELMODEMMOD="`find rootfs-complete/lib/modules/${KERNELVER} -type f -iname intel5*ko*`"
 INTELMODEMMOD="`find rootfs-complete/lib/modules/${KERNELVER} -type f -iname 'intel5*ko' -o -iname 'intel5*HIDE'`" #101222
 if [ "$INTELMODEMMOD" != "" ];then
  echo
  if [ "$CHOICE_BIGMODEM" ];then
   echo "You already selected what to do with the \"big modems\", however, this"
   echo "question is specifically about the Intel 536 and 537 analog modems, that"
   echo "are enormous and you might want to delete them, independently of the"
   echo "choice already made for the big modems."
  else
   echo "The next question will ask what to do with the \"big modems\", however, this"
   echo "question is specifically about the Intel 536 and 537 analog modems, that"
   echo "are enormous and you might want to delete them, independently of the"
   echo "choise that will be made next for the big modems."
  fi
  echo
  echo "Would you like to remove the Intel 536 and 537 analog modem modules?"
  echo "These are an enormous waste of space (1MB - 18MB uncompressed) if you"
  echo "don't have one of those particular old modem dialup cards."
  echo -n "Press ENTER only to delete them: "
  read DELINTEL
  if [ "$DELINTEL" = "" ];then
   for ONEMODEM in $INTELMODEMMOD
   do
    echo "Removing $ONEMODEM"
    rm -f $ONEMODEM
   done
  fi
 fi
fi

#v425 offer to move all the big analog modem modules to zdrv...
rm -rf big-modem-modules 2>/dev/null
rm -rf big-modem-firmware 2>/dev/null
if [ "$CHOICE_BIGMODEM" = "" ];then
 echo
 echo "Some of the analog (dialup) modem drivers are massive."
 echo "If you just press ENTER, the big drivers, for Conexant, Intel and Agere,"
 echo "will be deleted, but there will still be support for many software modems"
 echo "(Smartlink, Pctel, Lucent) as well as true hardware modems."
 echo "Or, type 'z' then enter to move them to a separate zdrv,"
 echo "or, any other printable char to keep them in the main live-cd file"
 echo "(warning, last option will make main f.s. much bigger)"
 echo -n "Press ENTER, 'z' or any other char: "
 read DELHSF
 CHOICE_BIGMODEM="Keep_big_modems"
 [ "$DELHSF" = "" ] && CHOICE_BIGMODEM="Erase_big_modems"
 [ "$DELHSF" = "z" ] && CHOICE_BIGMODEM="Zdrv_big_modems"
fi

if [ "$CHOICE_BIGMODEM" = "Erase_big_modems" ];then
 echo
 echo "ok, deleting big modem modules..."
 #101028 added ko* as have some named .koHIDE
 for BIGMODS in agr hcf hsf intel5 Intel5 esscom pctel
 do
#  for ONEBIGMOD in `find rootfs-complete/lib/modules/${KERNELVER}/ -type f -name ${BIGMODS}*.ko*`
  for ONEBIGMOD in `find rootfs-complete/lib/modules/${KERNELVER}/ -type f -name ${BIGMODS}*.ko -o -name ${BIGMODS}*HIDE` #101222
  do
   BIGMODNAME="`basename $ONEBIGMOD`"
   echo -n "$BIGMODNAME "
   [ -f $ONEBIGMOD ] && rm -f $ONEBIGMOD
  done
 done
 rm -rf rootfs-complete/lib/modules/all-firmware/hsfmodem 2>/dev/null
 rm -f rootfs-complete/lib/modules/all-firmware/hsfmodem.tar.gz 2>/dev/null
 rm -rf rootfs-complete/lib/modules/all-firmware/hcfpcimodem 2>/dev/null
 rm -f rootfs-complete/lib/modules/all-firmware/hcfpcimodem.tar.gz 2>/dev/null
 rm -rf rootfs-complete/lib/modules/all-firmware/intel536ep 2>/dev/null
 rm -f rootfs-complete/lib/modules/all-firmware/intel536ep.tar.gz 2>/dev/null
 rm -rf rootfs-complete/lib/modules/all-firmware/intel537* 2>/dev/null
 rm -f rootfs-complete/lib/modules/all-firmware/intel537*.tar.gz 2>/dev/null
fi

MAKEZDRV="" #100903
if [ "$CHOICE_BIGMODEM" = "Zdrv_big_modems" ];then
 echo "ok, moving big modem modules to separate zdrv sfs file..."
 MAKEZDRV="big-modems" #100903
 mkdir big-modem-modules
 mkdir big-modem-firmware
 #101028 added ko* as have some named .koHIDE
 for BIGMODS in agr hcf hsf intel5 Intel5
 do
#  for ONEBIGMOD in `find rootfs-complete/lib/modules/$KERNELVER/ -type f -name ${BIGMODS}*.ko*`
  for ONEBIGMOD in `find rootfs-complete/lib/modules/$KERNELVER/ -type f -name ${BIGMODS}*.ko -o -name ${BIGMODS}*HIDE` #101222
  do
   BIGMODNAME="`basename $ONEBIGMOD`"
   echo -n "$BIGMODNAME "
   ONEBIGDIR="`dirname $ONEBIGMOD`"
   mkdir -p big-modem-modules/$ONEBIGDIR
   mv $ONEBIGMOD big-modem-modules/$ONEBIGDIR/
  done
 done
 mv -f rootfs-complete/lib/modules/all-firmware/hsfmodem big-modem-firmware/ 2>/dev/null
 mv -f rootfs-complete/lib/modules/all-firmware/hsfmodem.tar.gz big-modem-firmware/ 2>/dev/null
 mv -f rootfs-complete/lib/modules/all-firmware/hcfpcimodem big-modem-firmware/ 2>/dev/null
 mv -f rootfs-complete/lib/modules/all-firmware/hcfpcimodem.tar.gz big-modem-firmware/ 2>/dev/null
 mv -f rootfs-complete/lib/modules/all-firmware/intel536ep big-modem-firmware/ 2>/dev/null
 mv -f rootfs-complete/lib/modules/all-firmware/intel536ep.tar.gz big-modem-firmware/ 2>/dev/null
 mv -f rootfs-complete/lib/modules/all-firmware/intel537* big-modem-firmware/ 2>/dev/null
 mv -f rootfs-complete/lib/modules/all-firmware/intel537*.tar.gz big-modem-firmware/ 2>/dev/null
fi
# 140309
echo
echo "Moving firmware"
cd rootfs-complete/lib/firmware
for ucode in RTL8192U RTL8192E rtlwifi libertas_firmware libertas_tf dvb-usb-firmware rt2x00 zd1201_firmware mwl8k rtl8188_8192su acx_firmware rtl_nic ipw2100_firmware-1.3 ipw2200_firmware-3.0 nouveau prism54_firmware iwlwifi 
 do mv ../modules/all-firmware/${ucode}/lib/firmware/* .
 #140617 decided to move as links were failing in some cases
done
cd -
echo
if [ "$CHK_FB_STATE" = "" ];then
 #w481 remove framebuffer modules...
 echo
 echo "The kernel is configured with the vesafb driver builtin, which is usually enough"
 echo "if it is ever required to boot in framebuffer video mode. The other framebuffer"
 echo "modules take up about 1.4MB uncompressed and are not normally required."
 echo -n "Press ENTER only to delete the other framebuffer modules: "
 read DELFBM
 CHK_FB_STATE="false"
 [ "$DELFBM" = "" ] && CHK_FB_STATE="true"
fi

if [ "$CHK_FB_STATE" = "true" ];then
 #130430 gpu/drm/sis.ko needs sisfb.ko, must keep...
 echo
 echo "Deleting framebuffer modules..."
 find rootfs-complete/lib/modules/$KERNELVER/kernel/drivers/video -type f -name '*fb.ko' -o -name '*fb_*.ko' |
 while read ONEMODULE
 do
  ONEBASE="`basename $ONEMODULE`"
  [ "$ONEBASE" = "sisfb.ko" ] && continue #130430
  echo -n "$ONEBASE "
  rm -f $ONEMODULE
 done
 echo
fi

if [ "$CHK_EXOTIC_STATE" = "" ];then
 echo
 echo "A bit later in the script you will be asked if you would like to cut the modules"
 echo "right down to a fairly basic selection. However, for now you can choose a minor"
 echo "cutdown of some 'exotic' modules that are unlikely to be needed."
 echo "Press any printable character to keep these, or"
 echo "press ENTER only to remove 'exotic' modules: "
 read keepexotic
 CHK_EXOTIC_STATE="false"
 [ "$keepexotic" = "" ] && CHK_EXOTIC_STATE="true"
fi

if [ "$CHK_EXOTIC_STATE" = "true" ];then
 echo
 echo "Deleting exotic modules..."
 #v423 nfs.ko needs sunrpc.ko and lockd.ko... nfsd.ko needs exportfs.ko...
 #v423 someone is working on autofs, so bring that back too...
 #EXOTICFS=' fs/autofs fs/autofs4 fs/befs fs/bfs fs/coda fs/cramfs fs/exportfs fs/freevxfs fs/jffs2 fs/jfs fs/lockd fs/ncpfs fs/romfs fs/sysv '
 #EXOTICNET=' net/appletalk net/ipv6 net/ipx net/llc net/sunrpc net/wanrouter '
 EXOTICFS=' fs/befs fs/bfs fs/coda fs/cramfs fs/freevxfs fs/jffs2 fs/jfs fs/ncpfs fs/romfs fs/sysv '
 EXOTICNET=' net/appletalk net/ipv6 net/ipx net/llc net/wanrouter '
 EXOTICDRIVERS=' drivers/block/paride drivers/block/aoe drivers/mtd drivers/telephony '
 for ONEDIR in $EXOTICFS $EXOTICNET $EXOTICDRIVERS
 do
  if [ -d rootfs-complete/lib/modules/$KERNELVER/kernel/$ONEDIR ];then
   echo -n "$ONEDIR "
   rm -rf rootfs-complete/lib/modules/$KERNELVER/kernel/$ONEDIR
  fi
 done
 sync
 echo
fi

#build the initrd-tree...
echo
echo "Now building initial ramdisk in initrd-tree/..."
rm -rf initrd-tree
cp -a ../boot/initrd-tree0 ./initrd-tree
cp ../DISTRO_SPECS ./initrd-tree/
#091222 support laptop internal sd/mmc cards at bootup...
MEMXTRAMODS='tifm_core.ko tifm_7xx1.ko mmc_core.ko mmc_block.ko tifm_sd.ko led-class.ko sdhci.ko sdhci-pci.ko'
#v423 2.6.29/30 kernels have extra hid-* modules needed for wireless keyboard to work...
#HIDXTRAMODS='hid-a4tech.ko hid-apple.ko hid-belkin.ko hid-cherry.ko hid-chicony.ko hid-cypress.ko hid-ezkey.ko hid-gyration.ko hid-logitech.ko hid-microsoft.ko hid-monterey.ko hid-ntrig.ko hid-petalynx.ko hid-pl.ko hid-samsung.ko hid-sony.ko hid-sunplus.ko hid-topseed.ko'
#110712 reduce list a bit, exclude hid drivers that are not keyboard (note, 2.6.39-3 kernel configured with them builtin)...
#  removed: hid-gyration.ko hid-ntrig.ko hid-petalynx.ko hid-pl.ko hid-sony.ko hid-sunplus.ko hid-topseed.ko
HIDXTRAMODS='hid-a4tech.ko hid-apple.ko hid-belkin.ko hid-cherry.ko hid-chicony.ko hid-cypress.ko hid-ezkey.ko hid-logitech.ko hid-microsoft.ko hid-monterey.ko hid-samsung.ko'
#copy some modules to initrd-tree/... w007 added nls_utf8.ko w468 added nls_cp850.ko w476 added nls_iso8859-2.ko, nls_cp850.ko, nls_cp852.ko. 100214 added floppy.ko, psmouse.ko
#100406 add btrfs.ko and its deps libcrc32c.ko,zlib_deflate.ko
#121227 if kernel has f.s. drivers as modules (quirky6), added ext2.ko ext3.ko ext4.ko fat.ko msdos.ko vfat.ko ntfs.ko reiserfs.ko udf.ko, and deps: jbd.ko mbcache.ko jbd2.ko
NEEDEDINITRDMODS=" ${SCSIDRVS} aufs.ko cdrom.ko fuse.ko ide-cd.ko ide-floppy.ko nls_cp437.ko nls_cp850.ko nls_cp852.ko nls_iso8859-1.ko nls_iso8859-2.ko nls_utf8.ko nls_cp850.ko sqlzma.ko squashfs.ko sr_mod.ko unionfs.ko unlzma.ko aes.ko aes_generic.ko blkcipher.ko crypto_blkcipher.ko cbc.ko cryptoloop.ko rsrc_nonstatic.ko yenta_socket.ko ehci-hcd.ko ohci-hcd.ko uhci-hcd.ko usb-storage.ko usbcore.ko usbhid.ko scsi_wait_scan.ko ssb.ko ${HIDXTRAMODS} ${DISTRO_MODULES} ${MEMXTRAMODS} floppy.ko psmouse.ko btrfs.ko libcrc32c.ko zlib_deflate.ko ext2.ko ext3.ko ext4.ko fat.ko msdos.ko vfat.ko ntfs.ko reiserfs.ko udf.ko jbd.ko mbcache.ko jbd2.ko "
NEEDEDINITRDMODS="`echo -n "$NEEDEDINITRDMODS" | tr -s ' ' | tr ' ' '\n' | sort -u | tr '\n' ' '`"
for ONENEEDED in $NEEDEDINITRDMODS
do
 echo -n "$ONENEEDED "
 FNDONE="`find rootfs-complete/lib/modules -type f -name $ONENEEDED | sed -e 's/rootfs\-complete//'`"
 if [ "$FNDONE" != "" ];then
  FNDDIR="`dirname $FNDONE`"
  mkdir -p initrd-tree$FNDDIR
  cp -a rootfs-complete${FNDONE} initrd-tree${FNDONE}
  if [ "$WOOF_TARGETARCH" != "x86_64" ];then #140716
    gzip initrd-tree${FNDONE}
  fi
  [ "$SDFLAG" = "" ] && rm -f rootfs-complete${FNDONE} #avoid duplication. 120521 SD-image, do not delete.
 fi
done

#091121 want to load keyboard layout in initrd... (note, init script copies them back at bootup)
#120521 no, do not move, copy only, in situation where building for SD image (that has no initrd)...
if [ "$SDFLAG" != "" ];then
 #not actually using an initrd, but let's keep the initrd functional...
 cp -a -f rootfs-complete/lib/keymaps initrd-tree/lib/
 cp -a -f rootfs-complete/lib/consolefonts initrd-tree/lib/
else
 mv -f rootfs-complete/lib/keymaps initrd-tree/lib/
 mv -f rootfs-complete/lib/consolefonts initrd-tree/lib/
fi
sync

if [ "$CHOICE_SCSI" != "Keep_SCSI" ];then #v431
 #get rid of scsi modules except some essentials...
 #note, above option to move some to initrd has left some old pre-PCI modules behind.
 rm -rf /tmp/scsi-keep
 mkdir /tmp/scsi-keep
 for ONEKEEP in imm.ko ppa.ko raid_class.ko sg.ko scsi_wait_scan.ko
 do
  ONEFND="`find rootfs-complete/lib/modules/$KERNELVER/kernel/drivers/scsi -type f -name $ONEKEEP`"
  [ "$ONEFND" ] && cp -a $ONEFND /tmp/scsi-keep/
 done
 rm -rf rootfs-complete/lib/modules/$KERNELVER/kernel/drivers/scsi
 cp -a /tmp/scsi-keep rootfs-complete/lib/modules/$KERNELVER/kernel/drivers/scsi
fi

#091225 remove duplicate executables...
#busybox in initrd is different, cp needed in main f.s. later by 3builddistro...
#100101 find in initrd does not support '-L' option... bit nervous taking out 'rev'...
#100128 symlinks get lost, for now leave out e2fsck, resize2fs, fsck, e3...
#100527 leave out lsmod, modinfo, ntfs-3g as causes symlinks to get deleted...
#100527 in fact, abandon this whole thing...
#INITRDEXES="`find initrd-tree/bin -type f | grep -v 'e2fsck' | grep -v 'resize2fs' | grep -v 'fsck' | grep -v 'busybox' | grep -v 'e3' | grep -v 'cp' | grep -v 'find' | grep -v 'rev' | rev | cut -f 1 -d '/' | rev | tr '\n' ' '`"
#echo -n "" > initrd-tree/bin/TARGETEXES #init script will read this.
#for ONEINITRDEXE in $INITRDEXES
#do
# echo "Remove duplicate of $ONEINITRDEXE in main f.s."
# ONEEXE="`find rootfs-complete/bin -maxdepth 1 -type f -name $ONEINITRDEXE | cut -f 2,3 -d '/'`"
# if [ "$ONEEXE" ];then
#  rm -f rootfs-complete/$ONEEXE
#  echo "$ONEEXE" >> initrd-tree/bin/TARGETEXES
#  continue
# fi
# ONEEXE="`find rootfs-complete/sbin -maxdepth 1 -type f -name $ONEINITRDEXE | cut -f 2,3 -d '/'`"
# if [ "$ONEEXE" ];then
#  rm -f rootfs-complete/$ONEEXE
#  echo "$ONEEXE" >> initrd-tree/bin/TARGETEXES
#  continue
# fi
# ONEEXE="`find rootfs-complete/usr/bin -maxdepth 1 -type f -name $ONEINITRDEXE | cut -f 2-4 -d '/'`"
# if [ "$ONEEXE" ];then
#  rm -f rootfs-complete/$ONEEXE
#  echo "$ONEEXE" >> initrd-tree/bin/TARGETEXES
#  continue
# fi
# ONEEXE="`find rootfs-complete/usr/sbin -maxdepth 1 -type f -name $ONEINITRDEXE | cut -f 2-4 -d '/'`"
# if [ "$ONEEXE" ];then
#  rm -f rootfs-complete/$ONEEXE
#  echo "$ONEEXE" >> initrd-tree/bin/TARGETEXES
#  continue
# fi
#done

#w091029 use busybox depmod, creates smaller modules* files...
if [ "$USINGKMOD" = "no" ];then #130418
 cp -f ../boot/depmod ./
 #101007 the latest busybox depmod generates relative paths in modules.dep (it is a config option)
 # but this breaks the older modprobe in the initrd, so use older depmod...
 ./depmod -b $WKGDIR/sandbox3/initrd-tree -F $WKGDIR/sandbox3/System.map $KERNELVER
 #./depmod -b $WKGDIR/sandbox3/rootfs-complete -F $WKGDIR/sandbox3/System.map $KERNELVER
 #busybox depmod -b $WKGDIR/sandbox3/initrd-tree -F $WKGDIR/sandbox3/System.map $KERNELVER
 busybox depmod -b $WKGDIR/sandbox3/rootfs-complete -F $WKGDIR/sandbox3/System.map $KERNELVER
else
 cp -f ../boot/depmod ./depmod.veryold
 cp -f ../boot/kmod ./kmod
 ln -snf kmod depmod
 #130418 we do not use kmod in initrd (as unable to compile statically)...
 #101007 the latest busybox depmod generates relative paths in modules.dep (it is a config option)
 # but this breaks the older modprobe in the initrd, so use older depmod...
 ./depmod.veryold -b $WKGDIR/sandbox3/initrd-tree -F $WKGDIR/sandbox3/System.map $KERNELVER
 ./depmod -b $WKGDIR/sandbox3/rootfs-complete -F $WKGDIR/sandbox3/System.map $KERNELVER
fi
sync

#101027 Iguleder reported DISTRO_SPECS did not have a carriage-return on last line...
xDISTROSPECS="`cat initrd-tree/DISTRO_SPECS`"
echo "$xDISTROSPECS" > initrd-tree/DISTRO_SPECS
echo "$xDISTROSPECS" > rootfs-complete/etc/DISTRO_SPECS

#100911
NEWVARIABLES="#32-byte alpha-numeric ID-string appended to vmlinuz, ${PUPPYSFS}, ${ZDRVSFS} and devx.sfs:
DISTRO_IDSTRING='${IDSTRING}'
#Puppy default filenames...
#Note, the 'SFS' files below are what the 'init' script in initrd.gz searches for,
#for the partition, path and actual files loaded, see PUPSFS and ZDRV in /etc/rc.d/PUPSTATE
DISTRO_PUPPYSFS='${PUPPYSFS}'
DISTRO_ZDRVSFS='${ZDRVSFS}'
DISTRO_ADRVSFS='${ADRVSFS}'
DISTRO_YDRVSFS='${YDRVSFS}'"
#DISTRO_DEVXSFS='${DEVXSFS}'"
echo "$NEWVARIABLES" >> initrd-tree/DISTRO_SPECS
echo "$NEWVARIABLES" >> rootfs-complete/etc/DISTRO_SPECS
sync

#120225 this is read by /usr/share/doc/convert-raw-docs...
DISTRO_PUPPYDATE="`date | tr -s " " | cut -f 2,6 -d " "`"
echo "DISTRO_PUPPYDATE='${DISTRO_PUPPYDATE}'" >> rootfs-complete/etc/DISTRO_SPECS
echo "DISTRO_PUPPYDATE='${DISTRO_PUPPYDATE}'" >> initrd-tree/DISTRO_SPECS

#120215 language translations for init script...
INITRDTRANSFILES="`find ../rootfs-skeleton/usr/share/sss/initrd_strings -mindepth 1 -maxdepth 1 -type f -name 'initrd_strings.*'`"
if [ "$INITRDTRANSFILES" ];then
 for ATRANS in $INITRDTRANSFILES
 do
  ENDBIT="`echo -n "$ATRANS" | rev | cut -f 1 -d '.' | rev`" #ex: de (from initrd_strings.de)
  mkdir -p initrd-tree/locale/${ENDBIT}
  #only supporting translating the init script for now...
  sPTN="/^\[init\]/,/^$/p" #this is a multi-line block find expression.
  CODEBLOCK="`sed -n "$sPTN" ${ATRANS} | sed -e '/^#/d' -e '/%%/d' -e '/^$/d' -e '/^\[/d'`" #extracts just the relevant block of lines.
  [ ! "$CODEBLOCK" ] && continue #precaution.
  echo "$CODEBLOCK" > initrd-tree/locale/${ENDBIT}/init #translation file, read by /init script in initrd.
 done
fi

#120216 set default LANG and KMAP... 120401 improve...
echo
echo
echo "Puppy is built with a default locale LANG=en_US and keyboard layout KMAP=us,
which may be changed after bootup.
However, if you are building a language-specific Puppy, you may change the
defaults now. But, please do make sure that your Puppy has a 'langpack' PET
for your language installed -- if one does not exist, then you will have to
create one -- see MoManager in the Utility menu, also read the Menu -> Help
-> HOWTO Internationalization."
echo
echo "These are the available LANG values (in /usr/share/i18n/locales):
aa_DJ aa_ER aa_ER@saaho aa_ET af_ZA am_ET an_ES ar_AE ar_BH ar_DZ ar_EG ar_IN ar_IQ ar_JO ar_KW ar_LB ar_LY ar_MA ar_OM ar_QA ar_SA ar_SD ar_SY ar_TN ar_YE as_IN ast_ES az_AZ be_BY be_BY@latin ber_DZ ber_MA bg_BG bn_BD bn_IN br_FR br_FR@euro bs_BA byn_ER ca_AD ca_ES ca_ES@euro ca_FR ca_IT crh_UA csb_PL cs_CZ cy_GB da_DK de_AT de_AT@euro de_BE de_BE@euro de_CH de_DE de_DE@euro de_LU de_LU@euro dz_BT el_CY el_GR el_GR@euro en_AU en_BW en_CA en_DK en_GB en_HK en_IE en_IE@euro en_IN en_NG en_NZ en_PH en_SG en_US en_ZA en_ZW es_AR es_BO es_CL es_CO es_CR es_DO es_EC es_ES es_ES@euro es_GT es_HN es_MX es_NI es_PA es_PE es_PR es_PY es_SV es_US es_UY es_VE et_EE eu_ES eu_ES@euro fa_IR fi_FI fi_FI@euro fil_PH fo_FO fr_BE fr_BE@euro fr_CA fr_CH fr_FR fr_FR@euro fr_LU fr_LU@euro fur_IT fy_DE fy_NL ga_IE ga_IE@euro gd_GB gez_ER gez_ER@abegede gez_ET gez_ET@abegede gl_ES gl_ES@euro gu_IN gv_GB ha_NG he_IL hi_IN hr_HR hsb_DE hu_HU hy_AM id_ID ig_NG ik_CA is_IS it_CH it_IT it_IT@euro iu_CA iw_IL ja_JP ka_GE kk_KZ kl_GL km_KH kn_IN ko_KR ks_IN ku_TR kw_GB ky_KG lg_UG li_BE li_NL lo_LA lt_LT lv_LV mai_IN mg_MG mi_NZ mk_MK ml_IN mn_MN mr_IN ms_MY mt_MT nb_NO nds_DE nds_NL ne_NP nl_BE nl_BE@euro nl_NL nl_NL@euro nn_NO nr_ZA nso_ZA oc_FR om_ET om_KE or_IN pa_IN pap_AN pa_PK pl_PL pt_BR pt_PT pt_PT@euro ro_RO ru_RU ru_UA rw_RW sa_IN sc_IT se_NO shs_CA sid_ET si_LK sk_SK sl_SI so_DJ so_ET so_KE so_SO sq_AL sr_ME sr_RS sr_RS@latin ss_ZA st_ZA sv_FI sv_FI@euro sv_SE ta_IN te_IN tg_TJ th_TH ti_ER ti_ET tig_ER tk_TM tl_PH tn_ZA tr_CY tr_TR ts_ZA tt_RU tt_RU@iqtelif ug_CN uk_UA ur_PK uz_UZ uz_UZ@cyrillic ve_ZA vi_VN wa_BE wa_BE@euro wal_ET wo_SN xh_ZA yi_US yo_NG zh_CN zh_HK zh_SG zh_TW zu_ZA"
echo "ENTER only for default, else type one of the above. Usually, you would want UTF-8 enabled, so append that. ex: de_DE.UTF-8."
echo "(the default is en_US.UTF-8)"
echo -n "ENTER only for default, or type it now: "
read DEFAULTLANG
if [ "$DEFAULTLANG" ];then
 echo "...ok, set to ${DEFAULTLANG}"
 DEFAULTLANG1="${DEFAULTLANG%_*}"
 if [ "$DEFAULTLANG1" != "en" ];then
  echo -n "$DEFAULTLANG" > initrd-tree/PUPPYLANG
  echo
  echo "These are the available keyboard layout values (in /lib/keymaps):
azerty be-latin1 br-abnt2 br-abnt br-latin1-abnt2 br-latin1-us by cf croat cz de de-latin1 dk dvorak dvorak-l dvorak-r es et fi fr gr hu101 hu il it jp106 lt mk nl no pl pt-latin1 ro ru se sg sk-qwerty sk-qwertz slovene sv-latin1 uk us wangbe"
  echo "You don't have to choose a default, as Puppy will try and guess at bootup. Whatever, after bootup the user will have the opportunity to change it, in the QuickSetup window."
  echo -n "Enter only for 'us', else type one of the above: "
  read DEFAULTKMAP
  [ "$DEFAULTKMAP" != "" ] && [ "$DEFAULTKMAP" != "us" ] && echo -n "$DEFAULTKMAP" > initrd-tree/PUPPYKEYMAP
  [ "$DEFAULTKMAP" = "" ] && DEFAULTKMAP="us"
  echo "...ok, set to ${DEFAULTKMAP}"
 fi
else
 DEFAULTLANG="en_US.UTF-8" #121123
 echo "...ok, set to ${DEFAULTLANG}"
fi
#i think also, set LANG in /etc/profile...
sed -i -e "s%^LANG=.*%LANG=${DEFAULTLANG}%" rootfs-complete/etc/profile
echo

#130515
echo
echo "Puppy can create a file 'pupdesk.flg' on a hard-drive partition
(giving preference to a Linux f.s. if it exists) at first bootup (PUPMODE=5).
If the graphical desktop is successfully launched, the file is removed, and 
never created again -- that is, it is a once-only ephemeral thing.
However, if the desktop fails, say hangs, and forced poweroff/reboot is
required, 'pupdesk.flg' will still exist at second bootup, which will force
the text-mode Xorg Video Wizard to run.
If you want to build a pup with this 'pupdesk.flg' mechanism, type the \"p\"
character now then press ENTER key. "
echo -n "Press ENTER only to disable 'pupdesk.flg' mechanism: "
read PUPDESKFLG
if [ "$PUPDESKFLG" = "p" ];then
 echo '...enabled'
else
 #change the message in iso 'help.msg', see further down.
 #modify 'init' script in initrd...
 sed -i -e 's%^PUPDESKFLG=.*%PUPDESKFLG=0%' initrd-tree/init
fi

#130618 check udev version... 130710 01micko: fix...
#130612 if kernel has CONFIG_DEVTMPFS=y (and CONFIG_DEVTMPFS_MOUNT=y) then empty /dev...
DEVTMPFSFLG=0
if [ "`grep 'CONFIG_DEVTMPFS.*=y' rootfs-complete/etc/modules/DOTconfig*`" != "" ];then
 UDEV3=`grep -E '^udev[_-][0-9]' ../woof-installed-packages | cut -f 3 -d '|' | cut -c 1-3 | head -n 1`
 [ ! $UDEV3 ] && UDEV3=200 #eudev
 if [ $UDEV3 -ge 176 ];then #175 is last version that creates device nodes.
  #modify 'init' script in initrd...
  sed -i -e 's%^DEVTMPFSFLG=.*%DEVTMPFSFLG=2%' initrd-tree/init
  DEVTMPFSFLG=2 #see further down.
 fi
fi
sync

#build build/initrd.gz...
cp -f ../boot/makecpioinitrd ./
cp -f ../boot/cpio ./
./makecpioinitrd
sync
mv -f initrd.gz build/

#offer to cutdown the modules, to build a smaller live-cd...
rm -rf zdrv 2>/dev/null #note, current dir is in sandbox3.
rm -f ${ZDRVSFS} #100911 zdrv.sfs

if [ "$CHK_RADICAL_STATE" = "" ];then
 echo
 echo -n "Press ENTER only to cutdown selection of modules, any other char to retain all: "
 read cutthemdown
 CHK_RADICAL_STATE="false"
 [ "$cutthemdown" = "" ] && CHK_RADICAL_STATE="true"
fi

if [ "$CHK_RADICAL_STATE" = "true" ];then
 if [ "$USINGKMOD" = "no" ];then #130418
  busybox depmod -b $WKGDIR/sandbox3/rootfs-complete -F $WKGDIR/sandbox3/System.map $KERNELVER
 else
  ./depmod -b $WKGDIR/sandbox3/rootfs-complete -F $WKGDIR/sandbox3/System.map $KERNELVER
 fi
 cd rootfs-complete/lib/modules/$KERNELVER #makes it easier to handle find results.
 #create a list of essential modules...
 echo "Creating must-have lists..."
 SOUNDMODS="`find ./kernel/sound/ -type f | sed -e 's/^\.\///g'`" #want all the alsa sound mods.
 IPV4MODS="`find ./kernel/net/ipv4 -type f | sed -e 's/^\.\///g'`" #want all the firewall mods.
 NETFILTERMODS="`find ./kernel/net/netfilter -type f | sed -e 's/^\.\///g'`"
 #IEEE80211MODS="`find ./kernel/net/ieee80211 -type f | sed -e 's/^\.\///g'`" #want all the ieee80211 mods.
 IEEE80211MODS="`find ./kernel/net/wireless -type f | sed -e 's/^\.\///g'`" #want all the wireless 80211 mods.
 RFKILLMODS="`find ./kernel/net/rfkill -type f | sed -e 's/^\.\///g'`"
 MAC80211MODS="`find ./kernel/net/mac80211 -type f | sed -e 's/^\.\///g'`"
 WIMAXMODS="`find ./kernel/net/wimax -type f | sed -e 's/^\.\///g'`"
 NETWORKMODS="`find ./kernel/drivers/net -type f | grep -vE '/wan/|/appletalk/' | sed -e 's/^\.\///g'`" #want all the network interface drivers.
 UBUNTUMODS=""
 [ -d ./kernel/ubuntu ] && UBUNTUMODS="`find ./kernel/ubuntu -type f | grep -vE '/compcache/|/gnbd/|/heci/|/lirc/' | sed -e 's/^\.\///g'`"
 MMCMODS="`find ./kernel/drivers/mmc -type f | sed -e 's/^\.\///g'`" #include mmc/sd host drivers.
 MEMSTICKMODS="`find ./kernel/drivers/memstick -type f | sed -e 's/^\.\///g'`"
 MISCMODS="`find ./kernel/drivers/misc -type f | sed -e 's/^\.\///g'`"
 USBMODS="`find ./kernel/drivers/usb -type f | sed -e 's/^\.\///g'`"
 ACPIMODS="`find ./kernel/drivers/acpi -type f | sed -e 's/^\.\///g'`"
 AGPMODS="`find ./kernel/drivers/char/agp -type f | sed -e 's/^\.\///g'`"
 I2CMODS="`find ./kernel/drivers/i2c -type f | sed -e 's/^\.\///g'`"
 PLATFORMMODS="`find ./kernel/drivers/platform -type f | sed -e 's/^\.\///g'`"
 BLUETOOTHMODS="`find ./kernel/drivers/bluetooth -type f | sed -e 's/^\.\///g'`"
 #w475 k2.6.29.3 has these usb webcam drivers...
 GSPCAMODS=""
 [ -d ./kernel/drivers/media/video/gspca ] && GSPCAMODS="`find ./kernel/drivers/media/video/gspca -type f | sed -e 's/^\.\///g'`"
 #k2.6.25.16: kernel/drivers/char/drm, k2.6.27.4: kernel/drivers/gpu/drm...
 DRMMODS="`find ./kernel/drivers -type f | grep '/drm/' | sed -e 's/^\.\///g'`"
 USBMODEMLIST="cdc-acm.ko pl2303.ko nozomi.ko hso.ko option.ko sierra.ko"
 ANALOGMODEMLIST="mwave.ko martian_dev.ko pctel.ko ltserial.ko slamr.ko slusb.ko esscom.ko"
 CPUSCALING2MODS="`find ./kernel/drivers/cpufreq -type f | sed -e 's/^\.\///g'`" #100127
 CPUSCALINGMODS="`find ./kernel/arch/x86/kernel/cpu/cpufreq -type f | sed -e 's/^\.\///g'`" #v431
 CRYPTOMODS="`find ./kernel/crypto -type f | sed -e 's/^\.\///g'`" #100126
 FBCONMODS="`find ./kernel/drivers/video/console -type f | sed -e 's/^\.\///g'`" #110518
 #create a list of extra modules... w476 added nls_cp850.ko nls_cp852.ko...
 EXTRALIST="blkcipher.ko cbc.ko apm.ko cdrom.ko mwave.ko synclink_cs.ko serial_cs.ko lp.ko n_hdlc.ko nvram.ko sonypi.ko generic_serial.ko agpgart.ko ali-agp.ko amd-k7-agp.ko amd64-agp.ko ati-agp.ko intel-agp.ko nvidia-agp.ko sis-agp.ko sworks-agp.ko via-agp.ko blowfish.ko aes.ko arc4.ko crc32c.ko deflate.ko des.ko md5.ko sha1.ko cramfs.ko ext3.ko fat.ko fuse.ko isofs.ko jbd.ko lockd.ko minix.ko msdos.ko nfs.ko nls_cp437.ko nls_cp850.ko nls_cp852.ko nls_iso8859-1.ko nls_iso8859-13.ko nls_iso8859-14.ko nls_iso8859-15.ko nls_iso8859-2.ko nls_iso8859-3.ko nls_iso8859-4.ko nls_iso8859-5.ko nls_iso8859-6.ko nls_iso8859-7.ko nls_iso8859-8.ko nls_iso8859-9.ko nls_utf8.ko ntfs.ko reiserfs.ko smbfs.ko squashfs.ko udf.ko unionfs.ko vfat.ko i2c-core.ko i2c-algo-bit.ko ide-cs.ko ide-tape.ko ide-cd.ko ide-floppy.ko gameport.ko analog.ko pcigame.ko pcspkr.ko psmouse.ko sermouse.ko serport.ko serio.ko evdev.ko joydev.ko input.ko keybdev.ko mousedev.ko crc32.ko crc-ccitt.ko libcrc32c.ko zlib_deflate.ko firmware_class.ko parport_pc.ko parport_serial.ko parport.ko parport_cs.ko i82365.ko pd6729.ko tcic.ko yenta_socket.ko i82092.ko rsrc_nonstatic.ko pcmcia_core.ko ds.ko imm.ko scsi_mod.ko scsi_transport_spi.ko sd_mod.ko sg.ko sr_mod.ko ide-scsi.ko scsi_debug.ko scsi_transport_fc.ko cdc-acm.ko usb-midi.ko usblp.ko audio.ko bluetty.ko usbcore.ko sl811-hcd.ko uhci-hcd.ko ehci-hcd.ko ohci-hcd.ko uhci.ko usb-ohci.ko usb-uhci.ko usbkbd.ko usbhid.ko hid.ko usbmouse.ko usbnet.ko ir-usb.ko usbserial.ko usb-storage.ko scanner.ko printer.ko fan.ko ibm_acpi.ko ac.ko battery.ko processor.ko thermal.ko paride.ko cryptoloop.ko pktcdvd.ko floppy.ko loop.ko 8250_pnp.ko serial_core.ko 8250.ko 8250_pci.ko ndiswrapper.ko ungrab-winmodem.ko cifs.ko ssb.ko pl2303.ko aufs.ko serio_raw.ko ecb.ko output.ko"
 #FBMODS="intelfb.ko rivafb.ko sisfb.ko fbcon-vga-planes.ko fbcon-mfb.ko fbgen.ko mdacon.ko neofb.ko tridentfb.ko vgastate.ko vga16fb.ko "
 EXTRAMODS=""
 for ONEMOD in $EXTRALIST $USBMODEMLIST #$ANALOGMODEMLIST
 do
  ONEFND="`find ./ -type f -name $ONEMOD | sed -e 's/^\.\///g'`"
  if [ "$ONEFND" != "" ];then
   EXTRAMODS="$EXTRAMODS
$ONEFND"
  fi
 done
 cd ../../../../ #back into sandbox3.
 CHOSENMODS="$SOUNDMODS
 $IPV4MODS
 $NETFILTERMODS
 $IEEE80211MODS
 $RFKILLMODS
 $MAC80211MODS
 $WIMAXMODS
 $NETWORKMODS
 $UBUNTUMODS
 $MMCMODS
 $MEMSTICKMODS
 $MISCMODS
 $USBMODS
 $ACPIMODS
 $AGPMODS
 $I2CMODS
 $PLATFORMMODS
 $GSPCAMODS
 $BLUETOOTHMODS
 $DRMMODS
 $CPUSCALING2MODS
 $CPUSCALINGMODS
 $CRYPTOMODS
 $FBCONMODS
 $EXTRAMODS"
 echo "$CHOSENMODS"| tr -s '\n'  > /tmp/chosenmods.txt
 echo "Finding all dependent modules..."
 echo -n "" > /tmp/alldeps.txt
 echo "$CHOSENMODS" |
 while read ONEMOD
 do
  [ "$ONEMOD" = "" ] && continue #precaution
  ONEBASE="`basename $ONEMOD`"
  DPATTERN="/${ONEBASE}:"
  PPATTERN="s/^\/lib\/modules\/${KERNELVER}\///g"
  ONEDEPS="`cat rootfs-complete/lib/modules/$KERNELVER/modules.dep | grep "$DPATTERN" | cut -f 2 -d ':' | tr ' ' '\n' | sed -e "$PPATTERN"`"
  [ "$ONEDEPS" != "" ] && echo "$ONEDEPS" >> /tmp/alldeps.txt
 done
 sync
 sort -u /tmp/alldeps.txt > /tmp/alldepssorted.txt #remove duplicate lines.
 cat /tmp/alldepssorted.txt >> /tmp/chosenmods.txt
 sort -u /tmp/chosenmods.txt > /tmp/allmodssorted.txt
 sync
 #now delete all mods not in the list...
 echo "Moving unwanted modules to a separate sfs file..." #"Deleting unwanted modules..."
 find rootfs-complete/lib/modules/$KERNELVER/ -type f -name '*.ko' -o -name '*.HIDE' |
 while read ONEMODSPEC
 do
  ONEMODNAME="`basename $ONEMODSPEC`"
  mPATTERN='/'"$ONEMODNAME"
  if [ "`grep "$mPATTERN" /tmp/allmodssorted.txt`" = "" ];then
   ONEMODPATH="`dirname $ONEMODSPEC`"
   xONEMODPATH="`echo -n "$ONEMODPATH" | sed -e 's%^rootfs\-complete/%%'`"
   mkdir -p zdrv/${xONEMODPATH}
   cp -a $ONEMODSPEC zdrv/${xONEMODPATH}/
   rm -f $ONEMODSPEC
   #091203 remove firmware tarball...
   ONEMODBASE="`basename $ONEMODSPEC`"
   ombPATTERN='[:,]'"$ONEMODBASE"
   FWFND="`grep "$ombPATTERN" rootfs-complete/etc/modules/firmware.dep.${KERNELVER} | cut -f 1 -d ':'`"
   if [ "$FWFND" ];then
    if [ -f rootfs-complete/lib/modules/all-firmware/${FWFND}.tar.gz ];then
     mkdir -p zdrv/lib/modules/all-firmware
     mv -f rootfs-complete/lib/modules/all-firmware/${FWFND}.tar.gz zdrv/lib/modules/all-firmware/
    fi
   fi
  fi
 done
 
 #special case:
 mv -f rootfs-complete/lib/modules/all-firmware/intel537ep.tar.gz zdrv/lib/modules/all-firmware/
 cp -a -f --remove-destination rootfs-complete/lib/modules/${KERNELVER}/intelmodem zdrv/lib/modules/${KERNELVER}/
 rm -rf rootfs-complete/lib/modules/${KERNELVER}/intelmodem

 echo "Creating ${ZDRVSFS} 'zdrv' file..." #100911
 sync
 #w091029 use busybox depmod, creates smaller modules* files...
 #./depmod -b $WKGDIR/sandbox3/rootfs-complete -F $WKGDIR/sandbox3/System.map $KERNELVER
 if [ "$USINGKMOD" = "no" ];then #130418
  busybox depmod -b $WKGDIR/sandbox3/rootfs-complete -F $WKGDIR/sandbox3/System.map $KERNELVER
 else
  ./depmod -b $WKGDIR/sandbox3/rootfs-complete -F $WKGDIR/sandbox3/System.map $KERNELVER
 fi
 sync
 MAKEZDRV="unwanted-modules" #w481 delay. 100903
else
 #w460 retaining all modules, but offer to move them to a separate zdrv... 100911
 if [ "$CHK_YES_ZDRV" = "" ];then #120506
  echo
  echo "If you wish, you can move the modules to a separate zdrv sfs file."
  echo "Right now they are in rootfs-complete, which will become ${PUPPYSFS},"
  echo "however there are advantages if the modules and firmware are in a separate"
  echo "'zdrv' sfs file -- it will not be copied into RAM at bootup, speeding bootup"
  echo "and using up less RAM, yet not sacrificing any speed."
  echo -n "Press ENTER key only to leave them alone, else any char for a separate zdrv: "
  read wantzdrv
  CHK_YES_ZDRV="false"
  [ "$wantzdrv" != "" ] && CHK_YES_ZDRV="true"
 fi
 if [ "$CHK_YES_ZDRV" = "true" ];then
  echo
  echo "Creating ${ZDRVSFS} 'zdrv' file..." #100911
  mkdir -p zdrv/lib
  mv -f rootfs-complete/lib/modules zdrv/lib/
  sync
  MAKEZDRV="all-modules" #w481 delay. 100903
 else
  echo "...okay, leaving modules inside ${PUPPYSFS}" #100911
 fi
fi

##091125 offer to cutdown fonts...
#echo
#echo "Would you like to cutdown the bitmap fonts in usr/share/X11/fonts/misc"
#echo "to the barest minimum? The bitmap fonts were used in older xlib apps"
#echo "(not gtk or qt) and some cli (non-X) apps. Okay to answer yes if want"
#echo "to build a small iso. Saving about 1.5MB uncompressed."
#echo -n "Hit ENTER only to cutdown 'misc' fonts: "
#read cutdownmisc
#if [ "$cutdownmisc" = "" ];then
# 7x14B-ISO8859-1.pcf
# 7x14-ISO8859-1.pcf #default for xlib, set in root/.Xresources
# cursor.pcf
# 8x16.pcf #used by yaf-splash, ex usr/sbin/pmount
# 9x15B-ISO8859-1.pcf #used by yaf-splash in usr/sbin/delayedrun, sbin/pup_event_frontend_d
# symb12.pcf #used by xcalc, see etc/X11/app-defaults/Xcalc
# helvB12-ISO8859-1.pcf #used by Xclipboard
# courR12-ISO8859-1.pcf # "
# helvR24-ISO8859-1.pcf #used by Xlock
# courB12-ISO8859-1.pcf #'fixed' font that some xlib apps use.
# helvR12-ISO8859-1.pcf #'variable' font that some xlib apps use.
#
#fi

#100622 slackware 13.1: just in case any got through, remove c-shell scripts...
rm -f rootfs-complete/etc/profile.d/*.csh* 2>/dev/null

#sanity check...
echo
if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then #111123
 chroot rootfs-complete echo 'testing chroot'
 if [ $? -ne 0 ];then
  echo "ERROR: could not 'chroot' into sandbox3/rootfs-complete"
  echo "This means that something is incomplete, perhaps a library"
  echo "needed by bash. Check it out. Have to exit script now."
  exit
 fi
else
 echo "Normally, the script now does a sanity-test by chrooting into"
 echo "sandbox3/rootfs-complete, however as you are doing a cross-build, this"
 echo "cannot be done."
fi

#before building puppy.sfs from rootfs-complete, check for any invalid symlinks
#and move them to the devx...
echo
echo "Finding invalid symlinks..."
rm -rf devx
mkdir devx
#v431 aaargh, latest pburn (3.1.1) has a symlink with spaces in name...
#for ONESYMLINK in `find rootfs-complete/ -type l`
find rootfs-complete/ -type l |
while read ONESYMLINK
do
 [ "`echo -n "$ONESYMLINK" | grep '/dev/'`" != "" ] && continue
 [ "`echo -n "$ONESYMLINK" | grep '/proc/'`" != "" ] && continue
 xONESYMLINK="`echo -n "$ONESYMLINK" | sed -e 's/^rootfs\-complete//'`"
 #w018 some symlinks become valid after puppy 1st boot...
 
 if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then #111123
  LINKTO="`chroot rootfs-complete readlink "$xONESYMLINK"`"
  [ "`echo -n "$LINKTO" | grep 'lib/X11/pixmaps'`" != "" ] && continue #these images created 1st boot.
  if [ "`chroot rootfs-complete readlink -e "$xONESYMLINK"`" = "" ];then
   UPONE="`dirname "$xONESYMLINK"`"
   mkdir -p devx${UPONE}
   cp -a -f "$ONESYMLINK" devx${UPONE}/
   rm -f "$ONESYMLINK"
  fi
 else
  #do it without chroot...
  LINKTO="`readlink rootfs-complete"${xONESYMLINK}"`" #120502 fix for spaces in path.
  [ -e rootfs-complete${LINKTO} ] && continue #absolute path
  UPONE="`dirname "$xONESYMLINK"`"
  [ -e rootfs-complete${UPONE}/${LINKTO} ] && continue #relative path
  mkdir -p devx${UPONE}
  cp -a -f "$ONESYMLINK" devx${UPONE}/
  rm -f "$ONESYMLINK"
 fi
 
done
#for ONESYMLINK in `find rootfs-complete/ -type l`
#do
# [ "`echo -n "$ONESYMLINK" | grep '/dev/'`" != "" ] && continue
# [ "`echo -n "$ONESYMLINK" | grep '/proc/'`" != "" ] && continue
# xONESYMLINK="`echo -n "$ONESYMLINK" | sed -e 's/^rootfs\-complete//'`"
# #w018 some symlinks become valid after puppy 1st boot...
# LINKTO="`chroot rootfs-complete readlink $xONESYMLINK`"
# [ "`echo -n "$LINKTO" | grep 'lib/X11/pixmaps'`" != "" ] && continue #these images created 1st boot.
# if [ "`chroot rootfs-complete readlink -e $xONESYMLINK`" = "" ];then
#  UPONE="`dirname $xONESYMLINK`"
#  mkdir -p devx${UPONE}
#  cp -a -f $ONESYMLINK devx${UPONE}/
#  rm -f $ONESYMLINK
# fi
#done

sync

#get info about the ubuntu pkgs...
echo "Updating pkg data in rootfs-complete/root/.packages ..."
#copy them into the live-cd also...
#in case of Ppup4, PKGLISTS_COMPAT='Packages-puppy-4xx-official'
for ONELIST in $PKGLISTS_COMPAT
do
 cp -f ../$ONELIST ./
 cp -f ../$ONELIST rootfs-complete/root/.packages/
done

for ONELIST in $PACKAGELISTS_PET_ORDER #120506
do
 cp -f ../$ONELIST ./
 cp -f ../$ONELIST rootfs-complete/root/.packages/
done

#note, Ppup4, findwoofinstalledpkgs.sh (below) needs above file, then deletes it.
#also copy the pet package databases...
cp -f ../Packages-puppy-[0-9]-official rootfs-complete/root/.packages/ 2>/dev/null
cp -f ../Packages-puppy-common-official rootfs-complete/root/.packages/ 2>/dev/null #110608
#cp -f ../Packages-puppy-wary5-official rootfs-complete/root/.packages/

cp -f ../Packages-puppy-noarch-official rootfs-complete/root/.packages/ #120305

#121102
if [ -f ../Packages-puppy-${DISTRO_DB_SUBNAME}-official ];then #110723 ex: Packages-puppy-drake-official
 cp -f ../Packages-puppy-${DISTRO_DB_SUBNAME}-official rootfs-complete/root/.packages/
 cp -f ../Packages-puppy-${DISTRO_DB_SUBNAME}-official ./
 #120506...
 ../support/rationalize_categories Packages-puppy-${DISTRO_DB_SUBNAME}-official
 cp -f /tmp/rationalized_categories rootfs-complete/root/.packages/Packages-puppy-${DISTRO_DB_SUBNAME}-official
fi

#101014 rationalise categories... 110726 must *always* copy Packages-puppy-wary5-official (it is going to be the fallback)...
#if [ "$DISTRO_COMPAT_VERSION" = "wary5" ];then #101025
if [ -f Packages-puppy-wary5-official ];then
 ../support/rationalize_categories Packages-puppy-wary5-official
 cp -f /tmp/rationalized_categories rootfs-complete/root/.packages/Packages-puppy-wary5-official
fi
#fi

#if [ "${DISTRO_FILE_PREFIX}" = "drake" ];then #110723
# ../support/rationalize_categories Packages-puppy-drake-official
# cp -f /tmp/rationalized_categories rootfs-complete/root/.packages/Packages-puppy-drake-official
#fi
#if [ "${DISTRO_FILE_PREFIX}" = "slacko" ];then #110816
# ../support/rationalize_categories Packages-puppy-slacko-official
# cp -f /tmp/rationalized_categories rootfs-complete/root/.packages/Packages-puppy-slacko-official
#fi

#also copy this...
cp -f ../PKGS_MANAGEMENT rootfs-complete/root/.packages/

#477 a puppy-puppy build will not have any compat-distro databases...
petcntlimit=3
COMPATDISTRO_DB_FILES="`ls -1 rootfs-complete/root/.packages/Packages-[^p]* 2>/dev/null`"
[ "$COMPATDISTRO_DB_FILES" = "" ] && petcntlimit=5

#w476 swap to make sure 3 pet repos get chosen...
enabledrepos=" "

#100207 now using the quirky pet repo for all puppy builds...
[ -f ../Packages-puppy-quirky-official ] && cp -f ../Packages-puppy-quirky-official rootfs-complete/root/.packages/

#100730 DISTRO_COMPAT_VERSION for puppy builds no longer limited to a number 1-5 (ex: wary5)
fNUM1=`echo -n "$DISTRO_COMPAT_VERSION" | wc -c`

#100116 quirky, want the quirky db first in list... 101126 no, now building quirky from wary5 pkgs...
#if [ "`echo "$DISTRO_NAME" | grep -i 'quirky'`" != "" ];then
# enabledrepos="Packages-puppy-quirky-official Packages-puppy-4-official Packages-puppy-3-official "
# #cp -f ../Packages-puppy-quirky-official rootfs-complete/root/.packages/
#else
 repocnt=1
 case $DISTRO_FILE_PREFIX in  #110906 fix. $DISTRO_COMPAT_VERSION in #100911
  lucid|lupu|luci) #110906 fix. 120305 120312
   enabledrepos="Packages-puppy-lucid-official Packages-puppy-noarch-official "
  ;;
  wary|racy) #110906 110917 120305 120312 121105
   enabledrepos="Packages-puppy-${DISTRO_DB_SUBNAME}-official Packages-puppy-quirky-official Packages-puppy-noarch-official Packages-puppy-4-official " #110824 added -common-
  ;;
  slacko) #110824 120305 120312 121105
   enabledrepos="Packages-puppy-${DISTRO_DB_SUBNAME}-official Packages-puppy-noarch-official "
  ;;
  dpup|sqzd) #110824 110906 120305 120312
   enabledrepos="Packages-puppy-dpup-official Packages-puppy-noarch-official "
  ;;
  *)
   #add the puppy repos... (i want 5, 4, 3)...
   for ONEREPO in `ls -1 rootfs-complete/root/.packages/Packages-puppy* | sort -r`
   do
    [ $repocnt -gt $petcntlimit ] && break #limit of 3 puppy repos.
    BASEREPO="`basename $ONEREPO`"
    [ "$BASEREPO" = "Packages-puppy-wary5-official" ] && continue #100911 handled above.
    [ "$BASEREPO" = "Packages-puppy-lucid-official" ] && continue #100911 handled above.
    [ "$BASEREPO" = "Packages-puppy-slacko-official" ] && continue #110824 handled above.
    #w478 a problem if building an old puppy-puppy, don't want more recent db's... 100730...
    if [ "$DISTRO_BINARY_COMPAT" = "puppy" ];then
     ONEREPNUM=`echo -n "$BASEREPO" | cut -f 3 -d '-'`
     fNUM2=`echo -n "$ONEREPNUM" | wc -c`
     #[ "`echo -n "$ONEREPNUM" | grep '[a-z]'`" != "" ] && continue
     [ $fNUM1 -eq 1 ] && [ $fNUM2 -eq 1 ] && [ $ONEREPNUM -gt $DISTRO_COMPAT_VERSION 2>/dev/null ] && continue
    fi
   # #w019 have decided don't want this showing up in PPM...
   # [ "$BASEREPO" = "Packages-puppy-woof-official" ] && continue
    enabledrepos="${enabledrepos}${BASEREPO} "
    repocnt=`expr $repocnt + 1`
   done
  ;;
 esac
 #add compat-distro repos...
 if [ "$COMPATDISTRO_DB_FILES" != "" ];then
  for ONEREPO in `ls -1 rootfs-complete/root/.packages/Packages-[^p]*`
  do
   [ $repocnt -gt 5 ] && break #only allow 5 active repos in PPM.
   BASEREPO="`basename $ONEREPO`"
   enabledrepos="${enabledrepos}${BASEREPO} "
   repocnt=`expr $repocnt + 1`
  done
 fi
#fi

#120831 above code ok, but really just want these...
case $DISTRO_BINARY_COMPAT in
 ubuntu|trisquel)
  xPUPPYREPOS="Packages-puppy-${DISTRO_DB_SUBNAME}-official Packages-puppy-noarch-official" #Packages-puppy-common-official 
  xCOMPATREPOS="$(ls -1 rootfs-complete/root/.packages/Packages-[^p]* | grep -E '\-main$|\-universe$|\-multiverse$' | rev | cut -f 1 -d '/' | rev | tr '\n' ' ')"
  #130330 would like universe first...
  xCOMPATREPOS1="$(ls -1 rootfs-complete/root/.packages/Packages-[^p]* | grep -E '\-universe$' | rev | cut -f 1 -d '/' | rev | tr '\n' ' ')"
  xCOMPATREPOS2="$(ls -1 rootfs-complete/root/.packages/Packages-[^p]* | grep -E '\-main$|\-multiverse$' | rev | cut -f 1 -d '/' | rev | tr '\n' ' ')"
  enabledrepos=" ${xCOMPATREPOS1} ${xCOMPATREPOS2} ${xPUPPYREPOS} "
 ;;
esac

echo "PKG_REPOS_ENABLED='${enabledrepos}'" >> rootfs-complete/root/.packages/PKGS_MANAGEMENT

if [ -f ../DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then #w478
 cp -f ../DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} rootfs-complete/root/.packages/DISTRO_PKGS_SPECS
else
 cp -f ../DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT} rootfs-complete/root/.packages/DISTRO_PKGS_SPECS
fi
#101013 remove wrong alternate choice (old/new kernel optional pkgs)...
if [ -f /tmp/3builddistro_removed_alt ];then
 for ONEREMOVE in `cat /tmp/3builddistro_removed_alt | tr '\n' ' '`
 do
  orPATTERN='yes|'"$ONEREMOVE"'|'
  grep -v "$orPATTERN" rootfs-complete/root/.packages/DISTRO_PKGS_SPECS > /tmp/DISTRO_PKGS_SPECS-tempor
  cp -f /tmp/DISTRO_PKGS_SPECS-tempor rootfs-complete/root/.packages/DISTRO_PKGS_SPECS
 done
fi

if [ -f ../DISTRO_COMPAT_REPOS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then
 cp -f ../DISTRO_COMPAT_REPOS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} rootfs-complete/root/.packages/DISTRO_COMPAT_REPOS
else #130306
 cp -f ../DISTRO_COMPAT_REPOS-${DISTRO_BINARY_COMPAT} rootfs-complete/root/.packages/DISTRO_COMPAT_REPOS
fi
#this needed to update pkg db's in a running puppy...
cp -f ../0setup rootfs-complete/usr/local/petget/
#w015 0setup calls this for faster operation...
cp -f ../support/find_cat rootfs-complete/usr/local/petget/
#w091017 0setup also needs these, fast conversion created by Wosh...
[ -f ../support/ppkg ] && cp -f ../support/ppkg rootfs-complete/usr/local/petget/
[ -f ../support/ppkg.ctg ] && cp -f ../support/ppkg.ctg rootfs-complete/usr/local/petget/
#110612 0setup needs these...
[ -f ../support/rpm2ppm ] && cp -f ../support/rpm2ppm rootfs-complete/usr/local/petget/
[ -f ../support/mageia2ppm ] && cp -f ../support/mageia2ppm rootfs-complete/usr/local/petget/
#121111 new fast deb to pup db converter, called by 0setup...
[ -f ../support/debdb2pupdb ] && cp -f ../support/debdb2pupdb rootfs-complete/usr/local/petget/
#urls etc on the pet db's...
cp -f ../DISTRO_PET_REPOS rootfs-complete/root/.packages/
#w018 homepages of heaps of pkgs (see 0pre script)...
cp -f ../PKGS_HOMEPAGES rootfs-complete/root/.packages/
#cp -f ../support/inline_get_pet rootfs-complete/root/.packages/ #100801
#cp -f ../support/inline_get_compat rootfs-complete/root/.packages/ #100806
#cp -f ../support/findpkgs rootfs-complete/root/.packages/ #110821 110823 revert.

#130316 see 0setup and debdb2pupdb...
case ${DISTRO_BINARY_COMPAT} in
 debian|devuan|ubuntu|trisquel|raspbian)
  if [ ! -f ../invaliddepslist ];then
   echo "WARNING WARNING"
   echo "File 'invaliddepslist' does not exist.
This is created by script '0setup'. You must go back and rerun all of the
scripts from 0setup, 1download, 2createpackages.
Recommend abort this script now (ctrl-c).
Or, if you press ENTER to continue, an empty 'invaliddepslist' file will
be created."
   echo -n "Press ENTER to continue: "
   read justgoon
   touch ../invaliddepslist
  fi
 ;;
esac
[ -f ../invaliddepslist ] && cp -f ../invaliddepslist rootfs-complete/usr/local/petget/

#build a db of builtin packages, woof-installed-packages ... 110823 no need to chroot...
#chroot rootfs-complete /usr/local/petget/findwoofinstalledpkgs.sh
cp ../woof-installed-packages rootfs-complete/root/.packages/ #110823 already created earlier this script.
touch ../devx-only-installed-packages #121028
cp ../devx-only-installed-packages rootfs-complete/root/.packages/ #121028

#110722 add kernel db line...
echo "$KERNEL_DB_ENTRY" >> rootfs-complete/root/.packages/woof-installed-packages
#sort alphabetically...
sort --key=1 --field-separator="|" rootfs-complete/root/.packages/woof-installed-packages > /tmp/petget_woof-installed-packages
mv -f /tmp/petget_woof-installed-packages rootfs-complete/root/.packages/woof-installed-packages

#currently not using these...
rm -f rootfs-complete/etc/rc.d/rc.sysinit-UNIPUP
rm -f rootfs-complete/etc/rc.d/rc.shutdown-UNIPUP

#120628 certain utilities are not appropriate when booting on an arm board...
if [ "$SDFLAG" != "" ];then
 rm -f rootfs-complete/usr/sbin/grubconfig
 rm -f rootfs-complete/usr/sbin/remasterpup2
 rm -f rootfs-complete/usr/sbin/resizepfile.sh
 rm -f rootfs-complete/usr/sbin/wakepup2
 rm -f rootfs-complete/usr/share/applications/Grub-bootloader-config.desktop
 rm -f rootfs-complete/usr/share/applications/BootFlash-usb-installer.desktop
 rm -f rootfs-complete/usr/share/applications/Puppy-universal-installer.desktop
 rm -f rootfs-complete/usr/share/applications/Remaster-Puppy-live-CD.desktop
 rm -f rootfs-complete/usr/share/applications/Resize-personal-storage-file.desktop
 rm -f rootfs-complete/usr/share/applications/WakePup2.desktop
fi

#130326 script to adjust fonts to suit 96 dpi (puppy used to have 78 dpi)...
rootfs-complete/usr/sbin/hackfontsize

#130618 restore the "layer:" lines in /etc/xdg/templates/_root_.jwmrc...
#note, this file is now in Woof, I took it out of the JWM pet.
JWMFLG=0
JWM3=`grep -E '^jwm[_-][0-9]' rootfs-complete/root/.packages/woof-installed-packages | cut -f 3 -d '|' | cut -c 1-3 | head -n 1`
[ ! $JWM3 ] && JWMFLG=1 #assume too old. note, there is a jwm2 pet, which is older version.
if vercmp $JWM3 ge 2.2; then JWMFLG=2; fi
if vercmp $JWM3 ge 790; then JWMFLG=2; fi
case $JWMFLG in
 2)
  sed -i -e 's%.*LAYER_MINIXCAL.*%\t\t<Option>layer:above</Option>%' rootfs-complete/etc/xdg/templates/_root_.jwmrc
  sed -i -e 's%.*LAYER_GTKDIALOGSPLASH.*%\t\t<Option>layer:above</Option>%' rootfs-complete/etc/xdg/templates/_root_.jwmrc
  sed -i -e 's%.*LAYER_SPLASHWINDOW.*%\t\t<Option>layer:above</Option>%' rootfs-complete/etc/xdg/templates/_root_.jwmrc
 ;;
esac

#fix the menus...
#111123 ***NOTICE*** cross-build, will have to run fixmenus and indexgen.sh at first bootup.
if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then #111123
 echo
 echo "Constructing configuration files for JWM, Fvwm95, IceWM, openbox..."
 chroot rootfs-complete /usr/sbin/fixmenus
 #generate help index...
 chroot rootfs-complete /usr/sbin/indexgen.sh
 #...note, rootfs-skeleton/pinstall.sh pre-processes the help files.
fi

#shared library loading...
echo "/lib${lsuffix}" > rootfs-complete/etc/ld.so.conf
echo "/usr/lib${lsuffix}" >> rootfs-complete/etc/ld.so.conf
[ -d rootfs-complete/usr/local/lib${lsuffix} ] && echo "/usr/local/lib${lsuffix}" >> rootfs-complete/etc/ld.so.conf
[ -d rootfs-complete/usr/X11R7/lib${lsuffix} ] && echo "/usr/X11R7/lib" >> rootfs-complete/etc/ld.so.conf
[ -d rootfs-complete/opt/gnome2/lib ] && echo "/opt/gnome2/lib" >> rootfs-complete/etc/ld.so.conf
[ -d rootfs-complete/opt/qt4/lib ] && echo "/opt/qt4/lib" >> rootfs-complete/etc/ld.so.conf
[ -d rootfs-complete/opt/mozilla.org/lib ] && echo "/opt/mozilla.org/lib" >> rootfs-complete/etc/ld.so.conf
[ -d rootfs-complete/opt/samba/lib ] && echo "/opt/samba/lib" >> rootfs-complete/etc/ld.so.conf
echo "/root/my-applications/lib" >> rootfs-complete/etc/ld.so.conf
#generate /etc/ld.so.cache shared lib loading cache file... w019 not always there...
#cp -a ../packages-${DISTRO_FILE_PREFIX}/glibc_DEV/sbin/ldconfig rootfs-complete/
#100527 stupid thing, needs '/' on end of path in case of 'packages-qret' being a symlink...

if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then #111123
 LDCONFIG="`find ../packages-${DISTRO_FILE_PREFIX}/ -type f -name ldconfig | grep '/sbin/' | head -n 1`"
 cp -a $LDCONFIG rootfs-complete/
 chroot rootfs-complete ./ldconfig
 rm -f rootfs-complete/ldconfig
 sync
else
 #***NOTICE*** have to run ldconfig at first boot.
 LDCONFIG="`find ../packages-${DISTRO_FILE_PREFIX}/ -type f -name ldconfig | grep '/sbin/' | head -n 1`"
 cp -a $LDCONFIG rootfs-complete/sbin/
fi

#i don't know what 'lintian' is for...
rm -rf rootfs-complete/usr/share/lintian 2>/dev/null
#maybe stray /install dir from slackware pkgs...
rm -rf rootfs-complete/install 2>/dev/null
#slackware pkg may create this...
rm -rf rootfs-complete/etc/cron.daily 2>/dev/null

#w460 theme selection...
echo
echo "Continuing to setup `pwd`/rootfs-complete..."
echo
#141204 optional file...
DEFAULT_THEME_GTK2=''
DEFAULT_THEME_JWM=''
DEFAULT_THEME_OPENBOX=''
DEFAULT_THEME_WALLPAPER=''
DEFAULT_THEME_DESK_ICONS=''
DEFAULT_THEME_ROX_TEXT_FOREGROUND='black'
DEFAULT_THEME_ROX_TEXT_SHADOW='thin' #none, thin, or thick
DEFAULT_THEME_ROX_TEXT_FONT='DejaVu Sans 10'
DEFAULT_THEME_XORG_TEXT_DPI=  #141206
if [ -f default-theme ];then
. ../default-theme
fi
#w478 now have a gui for theme selection...
eval "`../support/choose_themes`"
#...returns BACKGROUNDIMAGE, DESKICONS, GTKTHEME, JWMTHEME.

if [ "$EXIT" = "OK" ];then
#BACKGROUNDIMAGE="zzzzzzzz"
#echo "Here are the available background images (ref: usr/share/backgrounds):"
#ls -1 rootfs-complete/usr/share/backgrounds
#echo
#while [ ! -f rootfs-complete/usr/share/backgrounds/$BACKGROUNDIMAGE ];do
# echo "Type the full filename of the one you want as default"
# echo -n "(ENTER only will select the first): "
# read BACKGROUNDIMAGE
# [ "$BACKGROUNDIMAGE" = "" ] && BACKGROUNDIMAGE="`ls -1 rootfs-complete/usr/share/backgrounds | head -n 1`"
#done
 case $BACKGROUNDIMAGE in #110831
  *.png|*.PNG)
   mv -f rootfs-complete/usr/share/backgrounds/$BACKGROUNDIMAGE rootfs-complete/usr/share/backgrounds/default.png
   sed -i -e 's%default\.jpg%default.png%' rootfs-complete/root/Choices/ROX-Filer/PuppyPin
   #i don't know what uses this, but change it anyway...
   sed -i -e 's%default\.jpg%default.png%' rootfs-complete/etc/Puppybackgroundpicture
  ;;
  *.svg)
   mv -f rootfs-complete/usr/share/backgrounds/$BACKGROUNDIMAGE rootfs-complete/usr/share/backgrounds/default.svg
   sed -i -e 's%default\.jpg%default.svg%' rootfs-complete/root/Choices/ROX-Filer/PuppyPin
   sed -i -e 's%default\.jpg%default.svg%' rootfs-complete/etc/Puppybackgroundpicture
  ;;
  *)
   mv -f rootfs-complete/usr/share/backgrounds/$BACKGROUNDIMAGE rootfs-complete/usr/share/backgrounds/default.jpg
  ;;
 esac



##w477 choose gtk theme...
#echo
#echo "Here are the available GTK themes (ref: usr/share/themes):"
#ls -1 rootfs-complete/usr/share/themes/*/gtk-2.0 | grep '^rootfs-complete/usr/share/themes' | cut -f 5 -d '/'
#echo
#GTKTHEME="zzzzzzzzzz"
#while [ ! -d "rootfs-complete/usr/share/themes/$GTKTHEME" ];do
# echo "Type the full filename of the one you want as default"
# echo -n "(ENTER only will select the first): "
# read GTKTHEME
# [ "$GTKTHEME" = "" ] && GTKTHEME="`ls -1 rootfs-complete/usr/share/themes/*/gtk-2.0 | grep '^rootfs-complete/usr/share/themes' | cut -f 5 -d '/' | head -n 1`"
#done
#130329 append extra line, needed for Qt4 theme matching. (see also /etc/profile.d/pup_gtk and /root/.config/Trolltech.conf)...
echo "# -- THEME AUTO-WRITTEN DO NOT EDIT
include \"/usr/share/themes/${GTKTHEME}/gtk-2.0/gtkrc\"

include \"/root/.gtkrc.mine\"

# -- THEME AUTO-WRITTEN DO NOT EDIT
gtk-theme-name=\"${GTKTHEME}\"" > rootfs-complete/root/.gtkrc-2.0

#130401 01micko...
# gtk3 theme support thunor http://murga-linux.com/puppy/viewtopic.php?p=695722#695722
#[ -d rootfs-complete/root/.config/gtk-3.0 ] || mkdir -p rootfs-complete/root/.config/gtk-3.0
pathGTK3THEME="`find rootfs-complete/usr/share/themes/${GTKTHEME} -type d -name gtk-3.0`"
if [ "$pathGTK3THEME" ];then #XDG_CONFIG_HOME=/root/.config
# [ -f ${pathGTK3THEME}/settings.ini ] && cp -af ${pathGTK3THEME}/settings.ini rootfs-complete/root/.config/gtk-3.0/ || echo -e "[Settings] \ngtk-theme-name = \"${GTKTHEME}\" \ngtk-fallback-icon-theme = \"gnome\"" > rootfs-complete/root/.config/gtk-3.0/settings.ini
# #[ -f ${pathGTK3THEME}/gtk.css ] && cp -af ${pathGTK3THEME}/gtk.css rootfs-complete$XDG_CONFIG_HOME/gtk-3.0/ ||\# doesn't work
# echo "" > rootfs-complete/root/.config/gtk-3.0/gtk.css
  #130404 link to actual theme...
  ln -snf "$pathGTK3THEME" rootfs-complete/root/.config/gtk-3.0
fi

#echo
#DESKICONS="zzzzzzzzz"
#echo "Here are the available desktop icon themes (ref: usr/local/lib/X11/themes):"
#find rootfs-complete/usr/local/lib/X11/themes -maxdepth 1 -mindepth 1 -type d | rev | cut -f 1 -d '/' | rev
#while [ ! -d "rootfs-complete/usr/local/lib/X11/themes/$DESKICONS" ];do
# echo "Type the full filename of the one you want as default"
# echo -n "(ENTER only will select the first): "
# read DESKICONS
# [ "$DESKICONS" = "" ] && DESKICONS="`find rootfs-complete/usr/local/lib/X11/themes -maxdepth 1 -mindepth 1 -type d | rev | cut -f 1 -d '/' | rev | head -n 1`"
#done
echo -n "$DESKICONS" > rootfs-complete/etc/desktop_icon_theme

 if [ -d rootfs-complete/root/.jwm/themes ];then
  cp -f rootfs-complete/root/.jwm/themes/${JWMTHEME}-jwmrc rootfs-complete/root/.jwm/jwmrc-theme
  cp -f rootfs-complete/root/.jwm/themes/${JWMTHEME}-colors rootfs-complete/root/.jwm/jwm_colors 2>/dev/null
 fi

 if [ -f rootfs-complete/root/.config/openbox/rc.xml ];then #20100406
  obPATTERN="s%Natura%${OBTHEME}%"
  sed -i -e "$obPATTERN" rootfs-complete/root/.config/openbox/rc.xml
 fi
 sync
fi #end theme chooser ok.

#110807 don't ask...
##110803 workaround for 'pngoverlay' needs X runnning, also avoid cpu overhead at first startup...
##/usr/sbin/icon_switcher_cli is called from /etc/rc.d/rc.update.
#echo
#echo "There are currently issues with building the default desktop icons in"
#echo "usr/local/lib/X11/pixmaps at first bootup of Puppy (PUPMODE=5)."
#echo "Recommend do it now, which will also save cpu time at first bootup."
#echo "Press any printable character then ENTER to defer building desktop icons."
#echo -n "Recommend press ENTER key only: "
#read DOITNOW
#if [ "$DOITNOW" = "" ];then

#110826 no longer calling icon_switcher_cli from rc.update, don't need this...
# #modify start of rootfs-complete/usr/sbin/icon_switcher_cli...
# sed -i -e 's%^#DONEINWOOF .*%[ "$PUPMODE" = "5" ] \&\& exit%' rootfs-complete/usr/sbin/icon_switcher_cli

 #have copied code from icon_switcher_cli to here, modified...
 NEWTHEME="$DESKICONS" #see above
 for ONESVG in `find rootfs-complete/usr/local/lib/X11/themes/${NEWTHEME}/ -maxdepth 1 -name \*.svg | tr '\n' ' '` #140103 removed '-type f'
 do
  ONEBASENAME="`basename $ONESVG .svg`"
  rm -f rootfs-complete/usr/local/lib/X11/pixmaps/${ONEBASENAME}48.png
  rsvg-convert -w 48 -h 48 -o rootfs-complete/usr/local/lib/X11/pixmaps/${ONEBASENAME}48.png $ONESVG
  rm -f rootfs-complete/usr/local/lib/X11/pixmaps/${ONEBASENAME}24.png 2>/dev/null #140102 sent to null, shouldn't exist here
  #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 rootfs-complete/usr/local/lib/X11/pixmaps/${ONEBASENAME}24.png $ONESVG #140102 removed
 done
 for ONEPNG in `find rootfs-complete/usr/local/lib/X11/themes/${NEWTHEME}/ -maxdepth 1 -type f -name \*.png | tr '\n' ' '`
 do
  ONEBASENAME="`basename $ONEPNG .png`"
  cp -f $ONEPNG rootfs-complete/usr/local/lib/X11/pixmaps/
  touch -m rootfs-complete/usr/local/lib/X11/pixmaps/${ONEBASENAME}.png #v3.98
  xONEBASENAME="`basename $ONEBASENAME 48`"
  #if [ "$xONEBASENAME" != "$ONEBASENAME" ];then #scale to 24 pixels... #140102 removed
   #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 > rootfs-complete/usr/local/lib/X11/pixmaps/${xONEBASENAME}24.png
   #touch -m rootfs-complete/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 [ -e rootfs-complete/usr/sbin/pngoverlay -a -e rootfs-complete/usr/local/lib/X11/pixmaps/closebox_outline48.png ];then #overlay app created by vovchik (see source /usr/sbin/pngoverlay.bac)
   rm -f rootfs-complete/usr/local/lib/X11/pixmaps/*_mntd*.png
   #cp -f rootfs-complete/usr/sbin/pngoverlay rootfs-complete/usr/local/lib/X11/pixmaps/ #dumb, but needs to be in same folder.
   #111123 get pngoverlay from host, in case of cross-build...
   cp -f /usr/sbin/pngoverlay rootfs-complete/usr/local/lib/X11/pixmaps/ #dumb, but needs to be in same folder.
   cd rootfs-complete/usr/local/lib/X11/pixmaps
   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 rootfs-complete/usr/local/lib/X11/pixmaps/pngoverlay
   cd ../../../../../../
  fi
 fi
#fi

#110912 modify jwm to use menu icon 'start-button.png' if present...
#note, similar code also in /usr/sbin/icon_switcher (desktop icon switcher script).
if [ -f rootfs-complete/root/.jwmrc-tray ];then
 if [ -f rootfs-complete/usr/local/lib/X11/${NEWTHEME}/start-button.png ];then #default theme has menu icon.
  if [ -f rootfs-complete/usr/local/lib/X11/pixmaps/start-button.png ];then #precaution.
   if [ "`grep 'start-button\.png' rootfs-complete/root/.jwmrc-tray`" = "" ];then
    #the default is to use 'mini-dog.xpm' with text "Menu"...
    echo "rootfs-complete/root/.jwmrc-tray modified to use start-button.png menu icon"
    echo "(which is in rootfs-complete/usr/local/lib/X11/pixmaps/)"
    PTN1='s%label="Menu" icon="mini-dog\.xpm"%icon="start-button.png"%'
    sed -i -e "$PTN1" rootfs-complete/root/.jwmrc-tray
   fi
  fi
 fi
fi


#v423 moved down. remove 'default' image from cull list...
#w482 cutdown the background images, to 8 images, remove largest...
BIGBACKIMAGES="`ls -S -1 rootfs-complete/usr/share/backgrounds | head -n -8 | grep -v 'default' | tr '\n' ' '`"
for ONEBIGIMAGE in $BIGBACKIMAGES
do
 rm -f rootfs-complete/usr/share/backgrounds/$ONEBIGIMAGE
done

#v424 rox desktop text is default white with black shadow, not suited all backgrounds...
echo
echo "The ROX-Filer desktop text defaults to white with black shadow, but this is"
echo "not best for some light backgound images. ENTER only to keep, but if you"
echo -n "want black text press any printable key then ENTER: "
read wantblacktext
if [ "$wantblacktext" != "" ];then
 echo "...ok, black text"
 grep -v 'pinboard_fg_colour' rootfs-complete/root/.config/rox.sourceforge.net/ROX-Filer/Options > /tmp/3builddistro_rox_options
 grep -v 'pinboard_shadow_labels' /tmp/3builddistro_rox_options > rootfs-complete/root/.config/rox.sourceforge.net/ROX-Filer/Options
 grep -v '^</Options>' rootfs-complete/root/.config/rox.sourceforge.net/ROX-Filer/Options > /tmp/3builddistro_rox_options
 mv -f /tmp/3builddistro_rox_options rootfs-complete/root/.config/rox.sourceforge.net/ROX-Filer/Options
 echo '  <Option name="pinboard_fg_colour">#000000000000</Option>' >> rootfs-complete/root/.config/rox.sourceforge.net/ROX-Filer/Options
 echo '  <Option name="pinboard_shadow_labels">0</Option>' >> rootfs-complete/root/.config/rox.sourceforge.net/ROX-Filer/Options
 echo '</Options>' >> rootfs-complete/root/.config/rox.sourceforge.net/ROX-Filer/Options
fi
#111010 set desktop font...
echo
echo "Also, ROX-Filer defaults to 'DejaVu Sans 10' font for the desktop. To accept"
echo "that just press ENTER. If you would prefer bold text 'DejaVu Sans Bold 10',"
echo "hit 'b' key then ENTER, or type in a full font specification (ex: Mono 12)"
echo -n ": "
read wantfont
if [ "$wantfont" != "" ];then
 grep -v 'label_font' rootfs-complete/root/.config/rox.sourceforge.net/ROX-Filer/Options > /tmp/3builddistro_rox_options
 grep -v '^</Options>' /tmp/3builddistro_rox_options > rootfs-complete/root/.config/rox.sourceforge.net/ROX-Filer/Options
 if [ "$wantfont" = "b" ];then
  echo '...ok, bold'
  echo '  <Option name="label_font">DejaVu Sans Bold 10</Option>' >> rootfs-complete/root/.config/rox.sourceforge.net/ROX-Filer/Options
 else
  echo "...ok, $wantfont"
  echo "  <Option name=\"label_font\">${wantfont}</Option>" >> rootfs-complete/root/.config/rox.sourceforge.net/ROX-Filer/Options
 fi
 echo '</Options>' >> rootfs-complete/root/.config/rox.sourceforge.net/ROX-Filer/Options
fi

#w464 maybe get rid of old mksquashfs... v424 fix
#w091019 running puppy with older glibc, mksquashfs may not work, so must chroot...
if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then #111123
 if [ -e rootfs-complete/usr/sbin/mksquashfs ];then
  if [ "`chroot rootfs-complete /usr/sbin/mksquashfs -version | grep 'mksquashfs version 4'`" = "" ];then
   mv -f rootfs-complete/usr/sbin/mksquashfs rootfs-complete/usr/sbin/mksquashfs3
  else
   [ ! -e rootfs-complete/usr/sbin/mksquashfs4 ] && mv -f rootfs-complete/usr/sbin/mksquashfs rootfs-complete/usr/sbin/mksquashfs4
  fi
  if [ "$SQUASHFSMAJOR" = "4" ];then #100607
   [ -e rootfs-complete/usr/sbin/mksquashfs4 ] && ln -s mksquashfs4 rootfs-complete/usr/sbin/mksquashfs
  else
   ln -s mksquashfs3 rootfs-complete/usr/sbin/mksquashfs
  fi
 fi
 #ditto for unsquashfs... v424 fix w091019 chroot fix...
 if [ -e rootfs-complete/usr/sbin/unsquashfs ];then
  if [ "`chroot rootfs-complete /usr/sbin/unsquashfs -version | grep 'unsquashfs version 4'`" = "" ];then
   mv -f rootfs-complete/usr/sbin/unsquashfs rootfs-complete/usr/sbin/unsquashfs3
  else
   [ ! -e rootfs-complete/usr/sbin/unsquashfs4 ] && mv -f rootfs-complete/usr/sbin/unsquashfs rootfs-complete/usr/sbin/unsquashfs4
  fi
  if [ "$SQUASHFSMAJOR" = "4" ];then #100607
   [ -e rootfs-complete/usr/sbin/unsquashfs4 ] && ln -s unsquashfs4 rootfs-complete/usr/sbin/unsquashfs
  else
   ln -s unsquashfs3 rootfs-complete/usr/sbin/unsquashfs
  fi
 fi
else
 #cross-build, assume recent kernel and has squashfs v4...
 [ ! -e rootfs-complete/usr/sbin/mksquashfs4 ] && mv -f rootfs-complete/usr/sbin/mksquashfs rootfs-complete/usr/sbin/mksquashfs4
 [ -e rootfs-complete/usr/sbin/mksquashfs ] && [ ! -h rootfs-complete/usr/sbin/mksquashfs ] && mv -f rootfs-complete/usr/sbin/mksquashfs rootfs-complete/usr/sbin/mksquashfs3 #120502
 [ -e rootfs-complete/usr/sbin/mksquashfs4 ] && ln -s mksquashfs4 rootfs-complete/usr/sbin/mksquashfs
 [ ! -e rootfs-complete/usr/sbin/unsquashfs4 ] && mv -f rootfs-complete/usr/sbin/unsquashfs rootfs-complete/usr/sbin/unsquashfs4
 [ -e rootfs-complete/usr/sbin/unsquashfs ] && [ ! -h rootfs-complete/usr/sbin/unsquashfs ] && mv -f rootfs-complete/usr/sbin/unsquashfs rootfs-complete/usr/sbin/unsquashfs3 #120502
 [ -e rootfs-complete/usr/sbin/unsquashfs4 ] && ln -s unsquashfs4 rootfs-complete/usr/sbin/unsquashfs
fi

#w469 delete /lib/modules/$KERNELVER/modules.* to save space...
#/etc/rc.d/rc.sysinit has code to run depmod.
#w481 ask delete modules.*...
echo
echo "Would you like to delete all the 'modules.*' files in lib/modules/${KERNELVER}?"
echo "This means that 'depmod' has to be run at first boot, but that is usually ok."
echo "These files occupy 600KB - 2MB uncompressed."
echo -n "Press ENTER only to delete them: "
read nodelmods
if [ "$nodelmods" = "" ];then
 echo "...deleting modules.* files."
 [ -d rootfs-complete/lib/modules/$KERNELVER ] && rm -f rootfs-complete/lib/modules/$KERNELVER/modules.* 2>/dev/null
 [ -d zdrv/lib/modules/$KERNELVER ] && rm -f zdrv/lib/modules/$KERNELVER/modules.* 2>/dev/null
fi

#100628 my 2.6.34 pet has these...
mkdir -p rootfs-complete/lib/modules/$KERNELVER #101023 not there if separate zdrv built.
[ -f rootfs-complete/etc/modules/modules.builtin ] && mv -f rootfs-complete/etc/modules/modules.builtin rootfs-complete/lib/modules/$KERNELVER/
[ -f rootfs-complete/etc/modules/modules.order ] && mv -f rootfs-complete/etc/modules/modules.order rootfs-complete/lib/modules/$KERNELVER/

if [ "$MAKEZDRV" != "" ];then #w481 100903
 #v425 big analog modules may have got moved out to the zdrv...
 if [ -d big-modem-modules ];then
  mkdir -p zdrv
  cp -a -f big-modem-modules/rootfs-complete/* zdrv/
  mkdir -p zdrv/lib/modules/all-firmware
  cp -a -f big-modem-firmware/* zdrv/lib/modules/all-firmware/
  sync
 fi
 if [ "$MAKEZDRV" = "all-modules" ];then #100903
  mkdir -p zdrv/lib
  mkdir -p zdrv/etc
  mv -f rootfs-complete/lib/firmware zdrv/lib/
  mv -f rootfs-complete/etc/modules zdrv/etc/
 fi
 echo
 echo "Creating ${ZDRVSFS}..."
 [ -f ${ZDRVSFS} ] && rm -f ${ZDRVSFS} #100911
 ../support/${MKSQUASHFS} zdrv ${ZDRVSFS} ${COMPCHOICE} #100911 110713
 sync
 chmod 644 ${ZDRVSFS} #100903 100911
fi

#w470 create a table for dialog/Xdialog: /usr/share/i18n/dialog_table
#'chooselocale' is called from /etc/rc.d/rc.country at first boot and
#pre-creating this table speeds things up. 111123
if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then #111123
 chroot rootfs-complete /usr/sbin/chooselocale composeonly
fi

[ -f rootfs-complete/pet.specs ] && rm -f rootfs-complete/pet.specs #w478

#w478 save some space if medit and gtksourceview both installed...
if [ -d rootfs-complete/usr/share/gtksourceview-2.0 ];then
 if [ -d rootfs-complete/usr/share/moo/language-specs ];then
  echo
  echo "An optimisation can be done to share some files between medit and"
  echo -n "gtksourceview. Press ENTER only to do this: "
  read optsourceview
  if [ "$optsourceview" = "" ];then
   cp -f rootfs-complete/usr/share/gtksourceview-2.0/language-specs/convert.py rootfs-complete/usr/share/moo/language-specs/ 2>/dev/null
   cp -f rootfs-complete/usr/share/gtksourceview-2.0/language-specs/language2.rng rootfs-complete/usr/share/moo/language-specs/ 2>/dev/null
   cp -f rootfs-complete/usr/share/gtksourceview-2.0/language-specs/language.rng rootfs-complete/usr/share/moo/language-specs/ 2>/dev/null
   cp -f rootfs-complete/usr/share/gtksourceview-2.0/language-specs/language.dtd rootfs-complete/usr/share/moo/language-specs/ 2>/dev/null
   cp -f rootfs-complete/usr/share/gtksourceview-2.0/language-specs/styles.rng rootfs-complete/usr/share/moo/language-specs/ 2>/dev/null
   rm -r -f rootfs-complete/usr/share/gtksourceview-2.0/language-specs
   rm -r -f rootfs-complete/usr/share/gtksourceview-2.0/styles
   ln -s /usr/share/moo/language-specs rootfs-complete/usr/share/gtksourceview-2.0/language-specs
   ln -s /usr/share/moo/language-specs rootfs-complete/usr/share/gtksourceview-2.0/styles
   echo "...usr/share/gtksourceview-2.0 now has links to usr/share/moo."
  fi
 fi
fi

#091123 t2 build get rid of some stray stuff...
[ -d rootfs-complete/usr/share/devhelp ] && rm -rf rootfs-complete/usr/share/devhelp
[ -d rootfs-complete/usr/share/man ] && rm -rf rootfs-complete/usr/share/man
[ -d rootfs-complete/usr/share/omf ] && rm -rf rootfs-complete/usr/share/omf

##091206 cleanup some icons if quirky build...
#c1DFP="`echo -n "$DISTRO_FILE_PREFIX" | cut -c 1`" #100516
##if [ "$DISTRO_FILE_PREFIX" = "quirky" ];then
#if [ "$c1DFP" = "q" ];then #100516
# ln -snf quirky-grey48x48.png rootfs-complete/usr/share/doc/puppylogo48.png
# ln -snf quirky-grey96x96.png rootfs-complete/usr/share/doc/puppylogo96.png
#else #111003
# rm -f rootfs-complete/usr/share/doc/quirky-grey48x48.png
# rm -f rootfs-complete/usr/share/doc/quirky-grey96x96.png
# case $DISTRO_FILE_PREFIX in
#  racy)
#   ln -snf racy48.png rootfs-complete/usr/share/doc/puppylogo48.png
#   ln -snf racy96.png rootfs-complete/usr/share/doc/puppylogo96.png
#  ;;
#  *)
#   rm -f rootfs-complete/usr/share/doc/racy48.png #111003
#   rm -f rootfs-complete/usr/share/doc/racy96.png #111003
#  ;;
# esac
#fi
#111006 generic logo mechanism for all pups...
if [ -f rootfs-complete/usr/share/doc/puplogos/${DISTRO_FILE_PREFIX}48.png ];then
 mv -f rootfs-complete/usr/share/doc/puplogos/${DISTRO_FILE_PREFIX}48.png rootfs-complete/usr/share/doc/${DISTRO_FILE_PREFIX}48.png
 ln -snf ${DISTRO_FILE_PREFIX}48.png rootfs-complete/usr/share/doc/puppylogo48.png
fi
if [ -f rootfs-complete/usr/share/doc/puplogos/${DISTRO_FILE_PREFIX}96.png ];then
 mv -f rootfs-complete/usr/share/doc/puplogos/${DISTRO_FILE_PREFIX}96.png rootfs-complete/usr/share/doc/${DISTRO_FILE_PREFIX}96.png
 ln -snf ${DISTRO_FILE_PREFIX}96.png rootfs-complete/usr/share/doc/puppylogo96.png
fi
rm -rf rootfs-complete/usr/share/doc/puplogos

#100208 hack so net-setup.sh can find ifplugstatus (pup 4.3.2 needs this)...
if [ ! -e rootfs-complete/sbin/ifplugstatus ];then
 if [ -e rootfs-complete/sbin/ifplugstatus-0.18 ];then
  ln -s ifplugstatus-0.18 rootfs-complete/sbin/ifplugstatus
 else
  if [ -e rootfs-complete/sbin/ifplugstatus-0.28 ];then
   ln -s ifplugstatus-0.28 rootfs-complete/sbin/ifplugstatus
  fi
 fi
fi

#100524 fix cups for samba, got this code from /usr/sbin/cups_shell...
#fixes from rcrsn51 for samba printing...
[ -f rootfs-complete/etc/cups/snmp.conf ] && [ "`stat --format=%U%G rootfs-complete/etc/cups/snmp.conf | grep 'UNKNOWN'`" != "" ] && chown root:nobody rootfs-complete/etc/cups/snmp.conf
#100706 no, this goes against all the docs, restore to a symlink...
#if [ ! -e rootfs-complete/usr/lib/cups/backend/smb ];then
# [ -f rootfs-complete/opt/samba/bin/smbspool ] && cp -a rootfs-complete/opt/samba/bin/smbspool rootfs-complete/usr/lib/cups/backend/smb
# [ -f rootfs-complete/usr/bin/smbspool ] && cp -a rootfs-complete/usr/bin/smbspool rootfs-complete/usr/lib/cups/backend/smb
#fi
if [ ! -e rootfs-complete/usr/lib${lsuffix}/cups/backend/smb ];then
 [ -f rootfs-complete/opt/samba/bin/smbspool ] && ln -s /opt/samba/bin/smbspool rootfs-complete/usr/lib${lsuffix}/cups/backend/smb
 [ -f rootfs-complete/usr/bin/smbspool ] && ln -s /usr/bin/smbspool rootfs-complete/usr/lib${lsuffix}/cups/backend/smb
fi
[ -f rootfs-complete/etc/opt/samba/smb.conf ] && chmod 755 rootfs-complete/etc/opt/samba/smb.conf #need world-readable.
[ -f rootfs-complete/etc/samba/smb.conf ] && chmod 755 rootfs-complete/etc/samba/smb.conf #need world-readable.

#110907 rcrsn51: network printing, need this... 111027 revert, must have actual pdftops from cups pkg...
#if [ ! -e rootfs-complete/usr/lib/cups/filter/pdftops ];then
# [ -f rootfs-complete/usr/bin/pdftops ] && ln -s /usr/bin/pdftops rootfs-complete/usr/lib/cups/filter/pdftops
#fi

#111026 davids45: /tmp needs 777 permissions. i do have this in rootfs-skeleton, but lost in sandbox3/rootfs-complete
# (http://murga-linux.com/puppy/viewtopic.php?t=72258&start=90)
#130203 01micko: make that 1777 ref: http://murga-linux.com/puppy/viewtopic.php?p=677630#677630
chmod 1777 rootfs-complete/tmp
chmod 777 rootfs-complete/var

#110911 fix if any alternate drivers still at old location...
mkdir -p rootfs-complete/usr/lib${lsuffix}/x/drivers-alternate
if [ -d rootfs-complete/usr/lib${lsuffix}/xorg/modules/drivers-alternate ];then
 cp -a -f rootfs-complete/usr/lib${lsuffix}/xorg/modules/drivers-alternate/* rootfs-complete/usr/lib${lsuffix}/x/drivers-alternate/
 rm -rf rootfs-complete/usr/lib${lsuffix}/xorg/modules/drivers-alternate 2>/dev/null
fi

#100319 move more xorg drivers to 'drivers-alternate' so won't be chosen by xorg...
if [ -d rootfs-complete/usr/lib${lsuffix}/xorg/modules/drivers ];then #well, always exists.
 XORGBADDRIVERS=''
 if [ -f rootfs-complete/usr/lib${lsuffix}/xorg/modules/drivers/nvidia_drv.so -o -f rootfs-complete/usr/lib${lsuffix}/xorg/modules/drivers/nouveau_drv.so ];then #100527 100629
  [ -f rootfs-complete/usr/lib${lsuffix}/xorg/modules/drivers/nv_drv.so ] && XORGBADDRIVERS='nv' #120922 note, some builds don't have nv, or it may already be in drivers-alternate dir.
 fi
 [ -f rootfs-complete/usr/lib${lsuffix}/xorg/modules/drivers/modesetting_drv.so ] && XORGBADDRIVERS="${XORGBADDRIVERS} modesetting" #130330
 echo
 echo "Certain Xorg drivers have a bad reputation. Here you can choose to move them"
 echo "to /usr/lib/x/drivers-alternate so Xorg will not use them, instead"
 echo "will use the generic 'vesa' driver (or other more appropriate driver)."
 echo "However the Xorg Wizard has a mechanism to move any drivers in "
 echo "'drivers-alternate' back to 'drivers' directory."
 echo "Here is the list of drivers currently in 'drivers-alternate':"
 XORGBADDRIVERS="$XORGBADDRIVERS `ls -1 rootfs-complete/usr/lib${lsuffix}/x/drivers-alternate | grep '_drv' | cut -f 1 -d '_' | tr '\n' ' '`"
 echo " $XORGBADDRIVERS"
 echo "Press ENTER only to accept this, or"
 echo "type a space-delimited list of drivers"
 echo -n " that you want to be in 'drivers-alternate': "
 read NEWXORGBADDRVRS
 [ "$NEWXORGBADDRVRS" = "" ] && NEWXORGBADDRVRS="$XORGBADDRIVERS"
 for ONEBADX in $NEWXORGBADDRVRS
 do
  #some builds have i810_drv.so symlink to intel_drv.so...
  if [ -h rootfs-complete/usr/lib${lsuffix}/xorg/modules/drivers/${ONEBADX}_drv.so ];then
   continue
  fi
  [ -f rootfs-complete/usr/lib${lsuffix}/xorg/modules/drivers/${ONEBADX}_drv.so ] && mv -f rootfs-complete/usr/lib${lsuffix}/xorg/modules/drivers/${ONEBADX}_drv.so rootfs-complete/usr/lib${lsuffix}/x/drivers-alternate/
 done
 for ONECHK in `ls -1 rootfs-complete/usr/lib${lsuffix}/x/drivers-alternate | grep '_drv' | cut -f 1 -d '_' | tr '\n' ' '` #110911
 do
  oPTN=" ${ONECHK} "
  if [ "`echo -n " ${NEWXORGBADDRVRS} " | grep "$oPTN"`" = "" ];then
   if [ ! -d $ONECHK ];then
    mv -f rootfs-complete/usr/lib${lsuffix}/x/drivers-alternate/${ONECHK}_drv.so rootfs-complete/usr/lib${lsuffix}/xorg/modules/drivers/
   else
    echo "SORRY: cannot move '${ONECHK}_drv.so' from drivers-alternate back to"
    echo "main drivers, it must stay as an alternate driver only."
   fi
  fi
 done
fi

#101012 if 'depmod-FULL' exists, rename it to 'depmod'... 101024
if [ "$USINGKMOD" = "no" ];then #130418
 if [ ! -h rootfs-complete/sbin/depmod-FULL ];then #if hyperlink then assume to depmod.
  if [ -f rootfs-complete/sbin/depmod-FULL ];then
   mv -f rootfs-complete/sbin/depmod rootfs-complete/sbin/depmod-BB-NOTUSED
   mv -f rootfs-complete/sbin/depmod-FULL rootfs-complete/sbin/depmod
  fi
  ln -s depmod rootfs-complete/sbin/depmod-FULL
 fi
else
 [ -e rootfs-complete/bin/kmod ] && ln -snf ../bin/kmod rootfs-complete/sbin/depmod #130430 path fix.
fi

#101015 make sure our modified 'alsaconf' is used (just in case got overwritten)...
cp -a -f ../rootfs-skeleton/usr/sbin/alsaconf rootfs-complete/usr/sbin/

#w481 extra stripping...
if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then #120502 strip will not work for cross-build.
 echo
 echo "Would you like to strip all binary executables and shared library files?"
 echo "These are usually already stripped, although some packages may have the shared"
 echo "library files stripped with the '--strip-debug' option only, and extra stripping"
 echo "should be okay. It won't do any harm answering yes here."
 if [ "`which strip`" = "" ];then
  echo
  echo "OH, NO GOOD, you don't have the 'strip' executable. The 'devx' sfs has it."
  echo "PLEASE DECLINE THIS OFFER..."
  echo
 fi
 echo -n "ENTER only to strip, any other printable char to decline: "
 read NOSTRIP
 if [ "$NOSTRIP" = "" ];then
  find rootfs-complete/bin rootfs-complete/sbin rootfs-complete/usr/bin rootfs-complete/sbin -type f |
  while read ONEFILE
  do
   ONEBASE="`basename "$ONEFILE"`"
 #  if [ "`file "$ONEFILE" | grep 'ELF' | grep 'executable' | grep 'dynamically linked'`" != "" ];then #v424 fix.
   [ "$ONEBASE" = "e3" ] && continue #v431 cannot be stripped.
   [ "$ONEBASE" = "QtWeb" ] && continue #130721 cannot be stripped.
   if [ "`file "$ONEFILE" | grep 'ELF' | grep 'executable'`" != "" ];then #v431
    echo -n "$ONEBASE "
    strip --strip-unneeded "$ONEFILE"
   fi
  done
  echo
  find rootfs-complete/usr/lib rootfs-complete/usr/lib64 -type f -name \*.so* |
  while read ONEFILE
  do
   ONEBASE="`basename "$ONEFILE"`"
   #v431 libgdk-x11-2.0.so* compiled in pup4 with '--enable-debug', fixes insert-key mozilla crash.
   # it may be unwise to strip its debug symbols... no, it's ok...
   #[ "`echo "$ONEBASE" | grep 'libgdk'`" != "" ] && continue #v431
   if [ "`file "$ONEFILE" | grep 'ELF' | grep 'shared object'`" != "" ];then
    echo -n "$ONEBASE "
    strip --strip-unneeded "$ONEFILE"
   fi
  done
  echo
 fi
fi

#100527 build a .pet with lists of all builtin files...
echo
echo "Now building sandbox3/0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}.pet,"
echo "which is a PET package with lists of all packages and files builtin to the SFS..."
rm -rf 0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION} 2>/dev/null
mkdir 0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}
mkdir /tmp/0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}
for ONEGENDIR in `find ../packages-${DISTRO_FILE_PREFIX} -maxdepth 1 -type d | tr '\n' ' '`
do
 ONEGENNAME="`basename $ONEGENDIR`"
# [ "`echo "$ONEGENNAME" | grep -E '_DEV|_DOC|_NLS|_NULL'`" != "" ] && continue #not really needed.
 ogPATTERN="|${ONEGENNAME}|"
 [ "`echo "$PKGS_SPECS_TABLE" | grep '^yes' | grep "$ogPATTERN"`" == "" ] && continue
 echo -n "$ONEGENNAME "
 ogPATTERN="s%^\\.\\./packages-${DISTRO_FILE_PREFIX}/${ONEGENNAME}/%/%"
 find $ONEGENDIR | sed -e "$ogPATTERN" | sort > /tmp/0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}/${ONEGENNAME}.files
 sync
 #store the information more efficiently...
 mkdir -p 0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}/root/.packages/builtin_files
 PREVPATH=''; PREVPATH0=''
 cat /tmp/0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}/${ONEGENNAME}.files |
 while read ONELINE
 do
  [ "`echo "$ONELINE" | cut -c 1`" == "." ] && continue #catch ex: ../packages-qrky/abiword
  if [ -d "rootfs-complete${ONELINE}" ];then
   #120925 ignore debian/ubuntu multiarch circular symlink (these are create by 2createpackages)...
   if [ -h "rootfs-complete${ONELINE}" ];then
    if [ "$(readlink "rootfs-complete${ONELINE}")" = "./" ];then
     PREVPATH0="$(dirname "$ONELINE")" #121010 fix, missing some files.
     [ "$PREVPATH0" != "$PREVPATH" ] && echo "$PREVPATH0" >> 0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}/root/.packages/builtin_files/${ONEGENNAME} #121010
     continue
    fi
   fi
   PREVPATH="$ONELINE"
   echo "$ONELINE" >> 0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}/root/.packages/builtin_files/${ONEGENNAME}
  else
   NEWPATH="`dirname "$ONELINE"`"
   [ "$NEWPATH" == "/" ] && continue #ignore top-level files.
   NEWFILE="`basename "$ONELINE"`"
   if [ -e "rootfs-complete${ONELINE}" ];then #sanity check.
    if [ "$PREVPATH" == "$NEWPATH" -o "$PREVPATH0" == "$NEWPATH" ];then #sanity check. 121010
     echo " ${NEWFILE}" >> 0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}/root/.packages/builtin_files/${ONEGENNAME}
    fi
   fi
  fi
 done
done

# do the same for rootfs-packages
if [ -f /tmp/rootfs-packages.specs ];then
	while read line ; do
		PKGL=`echo $line | cut -d '|'  -f 2`
		echo -n "${PKGL} "
		ogPATTERN="s%^\\.\\./rootfs-packages/${PKGL}/%/%"
		find ../rootfs-packages/$PKGL | sed -e "$ogPATTERN" | sort > /tmp/0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}/${PKGL}.files
		sync
		mkdir -p 0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}/root/.packages/builtin_files
		PREVPATH=''; PREVPATH0=''
		cat /tmp/0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}/${PKGL}.files |
		while read ONELINE
			 do
				[ "`echo "$ONELINE" | cut -c 1`" == "." ] && continue #catch ex: ../packages-qrky/abiword
				if [ -d "rootfs-complete${ONELINE}" ];then
					PREVPATH="$ONELINE"
					echo "$ONELINE" >> 0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}/root/.packages/builtin_files/${PKGL}
				else
					NEWPATH="`dirname "$ONELINE"`"
					[ "$NEWPATH" == "/" ] && continue #ignore top-level files.
					NEWFILE="`basename "$ONELINE"`"
					if [ -e "rootfs-complete${ONELINE}" ];then #sanity check.
						if [ "$PREVPATH" == "$NEWPATH" -o "$PREVPATH0" == "$NEWPATH" ];then #sanity check. 121010
							echo " ${NEWFILE}" >> 0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}/root/.packages/builtin_files/${PKGL}
						fi
					fi
				fi
		done
	done < /tmp/rootfs-packages.specs
	rm -f /tmp/rootfs-packages.specs
fi
echo
echo "0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}|0builtin_files_${DISTRO_FILE_PREFIX}|${DISTRO_VERSION}||BuildingBlock|||0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}.pet||Lists of files built-in to the SFS file||||
" > 0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}/pet.specs
rm -f 0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}.pet 2>/dev/null

dir2tgz 0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}
tgz2pet 0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}.tar.gz
rm -rf /tmp/0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}
echo
echo "installing pkg lists into rootfs-complete/root/.packages/builtin_files..."
cp -a -f 0builtin_files_${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}/root/.packages/builtin_files rootfs-complete/root/.packages/builtin_files
echo '...done'

#100613 create font cache for fontconfig (in /var/cache/fontconfig)...
echo
echo "Creating Fontconfig cache files..."
rm -rf rootfs-complete/var/cache/fontconfig
mkdir -p rootfs-complete/var/cache/fontconfig
#111123 ***NOTICE*** cross-build, this will have to be executed at first boot...
if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then #111123
 chroot rootfs-complete /usr/bin/fc-cache -s -v
fi

if [ ! -e rootfs-complete/usr/bin/pango-querymodules ];then #110517 debian squeeze hack
 #cp -f devx/usr/bin/pango-querymodules rootfs-complete/usr/bin/
 cp -f ../packages-${DISTRO_FILE_PREFIX}/pango_DEV/usr/bin/pango-querymodules rootfs-complete/usr/bin/
fi

#100622 slackware 13.1: this file is zero bytes...
#111123 ***NOTICE*** cross-build, this will have to be executed at first boot...
if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then #111123
 PANGOMODULES="`find rootfs-complete/etc/pango -type f -name pango.modules | sed -e 's%rootfs-complete%%'`"
 [ "$PANGOMODULES" = "" ] && PANGOMODULES='/etc/pango/pango.modules'
 if [ ! -s rootfs-complete${PANGOMODULES} ];then
  echo "/usr/bin/pango-querymodules > ${PANGOMODULES}"  > rootfs-complete/zz
  chroot rootfs-complete /bin/ash zz
  rm -f rootfs-complete/zz
 fi
fi

#110622 hack for mageia 1 (otherwise can't find pango.modules)...
if [ -d rootfs-complete/etc/pango/i386 ];then
 cp -a -f rootfs-complete/etc/pango/pango.modules rootfs-complete/etc/pango/i386/
 [ -f rootfs-complete/etc/pango/pangox.aliases ] && cp -a -f rootfs-complete/etc/pango/pangox.aliases rootfs-complete/etc/pango/i386/
fi

#110622 /etc/rc.d/rc.update executes:  gdk-pixbuf-query-loaders > /etc/gtk-2.0/gdk-pixbuf.loaders
#however, mageia1 requires above file to be at /usr/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache
#create a symlink...
VERDIR=''
if [ -d rootfs-complete/usr/lib/gdk-pixbuf-2.0 -o -d rootfs-complete/usr/lib64/gdk-pixbuf-2.0 ];then
 [ ! -f rootfs-complete/etc/gtk-2.0/gdk-pixbuf.loaders ] && touch rootfs-complete/etc/gtk-2.0/gdk-pixbuf.loaders
 PIXBUFLOADERSDIR="`find rootfs-complete/usr/lib/gdk-pixbuf-2.0 -maxdepth 2 -mindepth 2 -type d -name loaders | head -n 1`"
 PIXBUFLOADERSDIR64="`find rootfs-complete/usr/lib64/gdk-pixbuf-2.0 -maxdepth 2 -mindepth 2 -type d -name loaders | head -n 1`"
 if [ "$PIXBUFLOADERSDIR" ];then
  PIXBUFCACHEDIR="`dirname $PIXBUFLOADERSDIR`"
  VERDIR="`basename $PIXBUFCACHEDIR`" #2.10.0
  ln -snf ../../../../etc/gtk-2.0/gdk-pixbuf.loaders ${PIXBUFCACHEDIR}/loaders.cache #110903 change -s to -snf
  #there may be some loaders in wrong place...
  [ -d rootfs-complete/usr/lib/gtk-2.0/loaders ] && mv -f rootfs-complete/usr/lib/gtk-2.0/loaders/*.so ${PIXBUFLOADERSDIR}/ 2>/dev/null
  if [ -d rootfs-complete/usr/lib/gtk-2.0/${VERDIR}/loaders ];then
   mv -f rootfs-complete/usr/lib/gtk-2.0/${VERDIR}/loaders/*.so ${PIXBUFLOADERSDIR}/ 2>/dev/null
   rmdir rootfs-complete/usr/lib/gtk-2.0/${VERDIR}/loaders #111114
   ln -s ../../gdk-pixbuf-2.0/${VERDIR}/loaders rootfs-complete/usr/lib/gtk-2.0/${VERDIR}/loaders #111114
  else #111117
   ln -s ../../gdk-pixbuf-2.0/${VERDIR}/loaders rootfs-complete/usr/lib/gtk-2.0/${VERDIR}/loaders
  fi
 fi
 if [ "$PIXBUFLOADERSDIR64" ];then #140108 for 64 bit
  PIXBUFCACHEDIR64="`dirname $PIXBUFLOADERSDIR64`"
  VERDIR="`basename $PIXBUFCACHEDIR64`" #2.10.0
  ln -snf ../../../../etc/gtk-2.0/gdk-pixbuf.loaders ${PIXBUFCACHEDIR64}/loaders.cache #110903 change -s to -snf
  #there may be some loaders in wrong place...
  [ -d rootfs-complete/usr/lib64/gtk-2.0/loaders ] && mv -f rootfs-complete/usr/lib/gtk-2.0/loaders/*.so ${PIXBUFLOADERSDIR}/ 2>/dev/null
  if [ -d rootfs-complete/usr/lib64/gtk-2.0/${VERDIR}/loaders ];then
   mv -f rootfs-complete/usr/lib64/gtk-2.0/${VERDIR}/loaders/*.so ${PIXBUFLOADERSDIR64}/ 2>/dev/null
   rmdir rootfs-complete/usr/lib64/gtk-2.0/${VERDIR}/loaders #111114
   ln -s ../../gdk-pixbuf-2.0/${VERDIR}/loaders rootfs-complete/usr/lib64/gtk-2.0/${VERDIR}/loaders #111114
  else #111117
   ln -s ../../gdk-pixbuf-2.0/${VERDIR}/loaders rootfs-complete/usr/lib64/gtk-2.0/${VERDIR}/loaders
  fi
 fi 
fi

#120605 shinobar ref: http://murga-linux.com/puppy/viewtopic.php?t=76961&start=195
if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then
 chroot rootfs-complete /usr/bin/gtk-query-immodules-2.0 > rootfs-complete/etc/gtk-2.0/gtk.immodules
else
 touch rootfs-complete/etc/gtk-2.0/gtk.immodules
fi
[ "$VERDIR" ] && ln -snf ../../../../etc/gtk-2.0/gtk.immodules rootfs-complete/usr/lib/gtk-2.0/${VERDIR}/gtk.immodules

#100627 recent versions of xorg server, turn modesetting on... 110822 improve...
mkdir -p rootfs-complete/etc/modprobe.d
#110621 need to chroot...
if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then #111123
 XORGINFO="`chroot rootfs-complete /usr/bin/Xorg -version 2>&1`"
 XORGVER="`echo "$XORGINFO" | grep '^X\.Org' | rev | cut -f 1 -d ' ' | rev`"
else
 #cross-build, assume recent xorg...
 XORGVER='1.7.7'
fi
[ "$XORGVER" = "" ] && XORGVER="`echo "$XORGINFO" | grep '^X Window System Version' | rev | cut -f 1 -d ' ' | rev`" #xorg 7.3
[ "$XORGVER" = "" ] && XORGVER="1.3.0" #precaution. version used in original Xorg 7.3.
if vercmp "$XORGVER" gt "1.7.6";then

 #120616 no, don't do this...
 ##120516 check that these drivers exist...
 #EXTRADRVPTH=''
 #[ -d rootfs-complete/usr/X11R7/lib/dri ] && EXTRADRVPTH='rootfs-complete/usr/X11R7/lib/dri'
 #[ -d rootfs-complete/usr/lib/dri ] && EXTRADRVPTH="${EXTRADRVPTH} rootfs-complete/usr/lib/dri"
 #[ ! "$EXTRADRVPTH" ] && EXTRADRVPTH='/usr/lib'
 #DRILIST=''
 #for AXDRI in i915_dri.so radeon_dri.so nouveau_dri.so
 #do
 # if find ${EXTRADRVPTH} -mindepth 1 -maxdepth 1 -name ${AXDRI}; then
 #  ADRI="`echo -n "$AXDRI" | cut -f 1 -d '_'`"
 #  DRILIST="${DRILIST}${ADRI} "
 # fi
 #done
 #120616 do this...
 DRILIST='i915 radeon nouveau'
 [ "$SDFLAG" ] && DRILIST='' #assume not using kms at all when boot from sd card (arm arch).
 if [ "$DRILIST" != "" ];then
  echo
  echo "Certain Xorg drivers require KMS (Kernel ModeSetting)"
  echo "A value of '1' means on, '0' means off."
  echo "Press ENTER key only to accept the default, any other char to flip:"
  for XDRIVER in $DRILIST #i915 radeon nouveau
  do
   KMSFLG='1'
   #[ "$XDRIVER" = "radeon" ] && [ "$DISTRO_BINARY_COMPAT" = "slackware" ] && KMSFLG='0' #01micko.
   #[ "$XDRIVER" = "radeon" ] && KMSFLG='0' #110918 seems need this off. 120526 try default on. 120605 turn off. 120903 on again.
   echo -n "${XDRIVER} [${KMSFLG}]: "
   read KMSREPLY
   if [ "$KMSREPLY" ];then #110906 fix syntax error.
    #not the default...
    case $KMSFLG in
     0) echo "options ${XDRIVER} modeset=1"  > rootfs-complete/etc/modprobe.d/${XDRIVER}.conf ;;
     1) echo "options ${XDRIVER} modeset=0"  > rootfs-complete/etc/modprobe.d/${XDRIVER}.conf ;;
    esac
   else
    echo "options ${XDRIVER} modeset=${KMSFLG}"  > rootfs-complete/etc/modprobe.d/${XDRIVER}.conf
   fi
  done
 fi
fi

#140619 ui_ziggy choice
echo
echo "Do you want the Classic gui or the Ziggy gui for PPM?"
echo "hit \"z\" [enter] for Ziggy, Enter only for Classic."
read ZIGGY
if [ "$ZIGGY" = "z" ];then
	ZIGSET="UI=\"Ziggy\""
	CLASET="UI=\"Classic\""
	sed -i "s%$CLASET%$ZIGSET%" rootfs-complete/usr/local/petget/configure.sh
	sed -i "s%$CLASET%$ZIGSET%" rootfs-complete/usr/local/petget/pkg_chooser.sh
	echo "PPM gui set to \"Ziggy\""
	else echo "Leaving PPM gui set to \"Classic\""
fi
echo

# fix CUPS thanks to jamesbond, shinobar
# re http://www.murga-linux.com/puppy/viewtopic.php?p=784181#784181
echo "Fixing permissions on CUPS libraries.."
chmod 0755 rootfs-complete/usr/lib/cups/backend
chmod 0755 rootfs-complete/usr/lib/cups/filters
chmod 500 rootfs-complete/usr/lib/cups/backend/*
echo

#110213 fix if .utf8 entries missing...
if [ "`grep 'nb_NO\.utf8' rootfs-complete/usr/share/X11/locale/locale.alias`" = "" ];then
 echo 'nb_NO.utf8:					nb_NO.UTF-8' >> rootfs-complete/usr/share/X11/locale/locale.alias
fi
if [ "`grep 'nn_NO\.utf8' rootfs-complete/usr/share/X11/locale/locale.alias`" = "" ];then
 echo 'nn_NO.utf8:					nn_NO.UTF-8' >> rootfs-complete/usr/share/X11/locale/locale.alias
fi
if [ "`grep 'ru_UA\.utf8' rootfs-complete/usr/share/X11/locale/locale.alias`" = "" ];then
 echo 'ru_UA.utf8:					ru_UA.UTF-8' >> rootfs-complete/usr/share/X11/locale/locale.alias
fi
if [ "`grep 'ru_RU\.utf8' rootfs-complete/usr/share/X11/locale/locale.alias`" = "" ];then
 echo 'ru_RU.utf8:					ru_RU.UTF-8' >> rootfs-complete/usr/share/X11/locale/locale.alias
fi

#110329 some python files, move to 'devx'...
#121022 but only if python is supposed to be in devx...
if [ "$(echo "$PKGS_SPECS_TABLE" | grep '^yes|python|' | grep 'exe>dev')" != "" ];then
 PYTHONDIR=''
 [ -d rootfs-complete/usr/lib${lsuffix}/python2.5 ] && PYTHONDIR='python2.5'
 [ -d rootfs-complete/usr/lib${lsuffix}/python2.6 ] && PYTHONDIR='python2.6'
 [ -d rootfs-complete/usr/lib${lsuffix}/python2.7 ] && PYTHONDIR='python2.7'
 if [ "$PYTHONDIR" ];then
  mkdir -p devx/usr/lib${lsuffix}/${PYTHONDIR}
  cp -a -f --remove-destination rootfs-complete/usr/lib${lsuffix}/${PYTHONDIR}/* devx/usr/lib${lsuffix}/${PYTHONDIR}/
  rm -rf rootfs-complete/usr/lib${lsuffix}/${PYTHONDIR}
 fi
fi

#110412 move all .bac, .pot and .pupdev files to 'devx'...
find rootfs-complete/usr -type f -name '*.pot' -o -name '*.pupdev' -o -name '*.bac' |
while read ONEPD
do
 DESTDIR="`dirname "$ONEPD" | sed -e 's%rootfs\-complete%%'`"
 mkdir -p "devx${DESTDIR}"
 mv -f "$ONEPD" "devx${DESTDIR}/"
done
#120224 move rootfs-complete/usr/share/doc/nls to 'devx'...
mkdir -p devx/usr/share/doc/nls
cp -a -f rootfs-complete/usr/share/doc/nls/* devx/usr/share/doc/nls/
rm -r -f rootfs-complete/usr/share/doc/nls

#110417 this may be helpful for more apps than Thunar...
#this will make Thunar use ROX-Filer's icons for files...
if [ -d rootfs-complete/usr/local/apps/ROX-Filer/ROX/MIME ];then
 if [ ! -e rootfs-complete/usr/share/icons/hicolor/48x48/mimetypes ];then
  ln -s ../../../../local/apps/ROX-Filer/ROX/MIME rootfs-complete/usr/share/icons/hicolor/48x48/mimetypes
 fi
fi

#110421 get version compare utility...
cp -f ../boot/initrd-tree0/bin/vercmp rootfs-complete/bin/

#110429 fix just in case...
busybox chown -h -R spot:spot rootfs-complete/root/spot

#110429 this is difficult. may need a fix, think want glibc scsi headers in usr/include/scsi...
mkdir -p devx/usr/include
if [ -d ../packages-${DISTRO_FILE_PREFIX}/linux_headers_DEV/usr/include/scsi ];then
 cp -a ../packages-${DISTRO_FILE_PREFIX}/linux_headers_DEV/usr/include/scsi devx/usr/include/scsi-KERNEL
fi
if [ -d ../packages-${DISTRO_FILE_PREFIX}/linux-header_DEV/usr/include/scsi ];then
 cp -a ../packages-${DISTRO_FILE_PREFIX}/linux-header_DEV/usr/include/scsi devx/usr/include/scsi-KERNEL
fi
if [ -d ../packages-${DISTRO_FILE_PREFIX}/glibc_DEV/usr/include/scsi ];then
 cp -a ../packages-${DISTRO_FILE_PREFIX}/glibc_DEV/usr/include/scsi devx/usr/include/scsi-GLIBC
fi
#also see further down, when building devx.

##110517 kms may need to be turned on... ALREADY DONE, SEE 110621 ABOVE
#XINTELMINOR=`grep '^xserver-xorg-video-intel' rootfs-complete/root/.packages/woof-installed-packages | cut -f 3 -d '|' | cut -f 2 -d '.'` #110517 ex: xserver-xorg-video-intel_2.13.0-6_i386.deb extract 13.
#[ ! $XINTELMINOR ] && XINTELMINOR=`grep '^xf86-video-intel' rootfs-complete/root/.packages/woof-installed-packages | cut -f 3 -d '|' | cut -f 2 -d '.'`
#[ ! $XINTELMINOR ] && XINTELMINOR=0
#if [ $XINTELMINOR -gt 9 ];then
# echo 'options i915 modeset=1' > rootfs-complete/etc/modprobe.d/i915.conf
# [ "`grep '^xserver-xorg-video-nouveau' rootfs-complete/root/.packages/woof-installed-packages`" != "" ] && echo 'options nouveau modeset=1' > rootfs-complete/etc/modprobe.d/nvidia.conf
# [ "`grep '^xf86-video-nouveau' rootfs-complete/root/.packages/woof-installed-packages`" != "" ] && echo 'options nouveau modeset=1' > rootfs-complete/etc/modprobe.d/nvidia.conf
# [ "`grep '^xserver-xorg-video-radeon' rootfs-complete/root/.packages/woof-installed-packages`" != "" ] && echo 'options radeon modeset=1' > rootfs-complete/etc/modprobe.d/radeon.conf
# [ "`grep '^xf86-video-radeon' rootfs-complete/root/.packages/woof-installed-packages`" != "" ] && echo 'options radeon modeset=1' > rootfs-complete/etc/modprobe.d/radeon.conf
#fi

#110523 override files from rootfs-skeleton/root/Choices/MIME-types, if rox pkg has older files...
cp -a -f ../rootfs-skeleton/root/Choices/MIME-types/* rootfs-complete/root/Choices/MIME-types/
#110620 do same for mime icons...
cp -a -f --remove-destination ../rootfs-skeleton/usr/local/apps/ROX-Filer/ROX/MIME/* rootfs-complete/usr/local/apps/ROX-Filer/ROX/MIME/

#130320 these days, i think give util-linux the preference. note, should really fix in e2fsprogs template.
##110701 possible lib clash between e2fsprogs and util-linux (i might use old util-linux pet). hack...
#if [ -e ../packages-${DISTRO_FILE_PREFIX}/e2fsprogs/lib/libblkid.so.1 ];then
# if [ -e ../packages-${DISTRO_FILE_PREFIX}/util-linux/lib/libblkid.so.1 ];then
#  #give e2fsprogs libs preference...
#  cp -a -f --remove-destination ../packages-${DISTRO_FILE_PREFIX}/e2fsprogs/lib/libblkid.so* rootfs-complete/lib/
#  cp -a -f --remove-destination ../packages-${DISTRO_FILE_PREFIX}/e2fsprogs/lib/libuuid.so* rootfs-complete/lib/
#  rm -f ../packages-${DISTRO_FILE_PREFIX}/util-linux_DEV/lib/lib*
# fi
#fi
#130320 i don't think any of this is required, as util-linux is later in the pkg-list so will override anyway.
# so, comment this out also!...
#if [ -e ../packages-${DISTRO_FILE_PREFIX}/e2fsprogs/lib/libblkid.so.1 ];then
# if [ -e ../packages-${DISTRO_FILE_PREFIX}/util-linux/lib/libblkid.so.1 ];then
#  #give util-linux libs preference...
#  cp -a -f --remove-destination ../packages-${DISTRO_FILE_PREFIX}/util-linux/lib/libblkid.so* rootfs-complete/lib/
#  cp -a -f --remove-destination ../packages-${DISTRO_FILE_PREFIX}/util-linux/lib/libuuid.so* rootfs-complete/lib/
#  cp -a -f --remove-destination ../packages-${DISTRO_FILE_PREFIX}/util-linux/usr/lib/libblkid.so* rootfs-complete/lib/ 2>/dev/null
#  cp -a -f --remove-destination ../packages-${DISTRO_FILE_PREFIX}/util-linux/usr/lib/libuuid.so* rootfs-complete/lib/ 2>/dev/null
#  #so that the 'devx' will build ok...
#  rm -f ../packages-${DISTRO_FILE_PREFIX}/e2fsprogs_DEV/lib/libblkid.so* 2>/dev/null
#  rm -f ../packages-${DISTRO_FILE_PREFIX}/e2fsprogs_DEV/lib/libuuid.so* 2>/dev/null
#  rm -f ../packages-${DISTRO_FILE_PREFIX}/e2fsprogs_DEV/usr/lib/libblkid.so* 2>/dev/null
#  rm -f ../packages-${DISTRO_FILE_PREFIX}/e2fsprogs_DEV/usr/lib/libuuid.so* 2>/dev/null
#  rm -f ../packages-${DISTRO_FILE_PREFIX}/e2fsprogs_DEV/usr/lib/pkgconfig/blkid.pc
#  rm -f ../packages-${DISTRO_FILE_PREFIX}/e2fsprogs_DEV/usr/lib/pkgconfig/uuid.pc
# fi
#fi

#130111 fixed in 2createpackages...
##110726 mageia does something weird, puts all these in wrong place (see also devx further down)...
#if [ -d rootfs-complete/usr/bin/multiarch-i386-linux ];then
# cp -a -f --remove-destination rootfs-complete/usr/bin/multiarch-i386-linux/* devx/usr/bin/
# rm -rf rootfs-complete/usr/bin/multiarch-i386-linux
#fi

#110806 if yad installed, no zenity, make sure zenity is symlink to yad...
YAD="`find ./rootfs-complete/bin ./rootfs-complete/sbin ./rootfs-complete/usr/bin ./rootfs-complete/usr/sbin ./rootfs-complete/usr/local/bin -type f -name yad`"
if [ "$YAD" ];then
 ZENITY="`find ./rootfs-complete/bin ./rootfs-complete/sbin ./rootfs-complete/usr/bin ./rootfs-complete/usr/sbin ./rootfs-complete/usr/local/bin -name zenity`"
 if [ ! "$ZENITY"  ];then
  YADDIR="`dirname $YAD`"
  ln -s yad ${YADDIR}/zenity
 fi
fi

#110904 no, now done in pet pkg 'zz_wary52_fixup'...
##110901 i have experimented with building a wary "5.2pre" without libxcb.
##however, many existing apps want it, even though don't use it.
##this is a trick that satisfies them...
#if [ "`grep '^libxcb' rootfs-complete/root/.packages/woof-installed-packages`" = "" ];then
# if [ -e rootfs-complete/usr/X11R7/lib/libX11.so.6 ];then
#  ln -s libX11.so.6 rootfs-complete/usr/X11R7/lib/libxcb-xlib.so.0
#  ln -s libX11.so.6 rootfs-complete/usr/X11R7/lib/libxcb.so.1
# fi
#fi

#110901 temp hack, if only have gtkdialog4 pkg...
if [ ! -e rootfs-complete/usr/sbin/gtkdialog3 ];then
 if [ ! -e rootfs-complete/usr/bin/gtkdialog3 ];then
  [ -e rootfs-complete/usr/sbin/gtkdialog4 ] && ln -s gtkdialog4 rootfs-complete/usr/sbin/gtkdialog3
  [ -e rootfs-complete/usr/bin/gtkdialog4 ] && ln -s gtkdialog4 rootfs-complete/usr/bin/gtkdialog3
 fi
fi
#110910 make sure gtkdialog links to latest...
if [ ! -e rootfs-complete/usr/sbin/gtkdialog ];then
 [ -e rootfs-complete/usr/sbin/gtkdialog3 ] && ln -s gtkdialog3 rootfs-complete/usr/sbin/gtkdialog
 [ -e rootfs-complete/usr/sbin/gtkdialog4 ] && ln -snf gtkdialog4 rootfs-complete/usr/sbin/gtkdialog
fi

#110822 some customisations provided by 01micko for Slacko...
#if [ "$DISTRO_BINARY_COMPAT" = "slackware" ];then
if [ "$DISTRO_FILE_PREFIX" = "slacko" -o "$DISTRO_FILE_PREFIX" = "slacko64" ];then #130321 #140107
 if [ -f ../support/slacko/woof_base_hack  ];then
  echo "Running Slacko customization script ../support/slacko/woof_base_hack..."
  ../support/slacko/woof_base_hack
 fi
fi

#111123 put this into target, want to know if cross-build at 1st boot (see rc.update)...
[ -f ../WOOFMERGEVARS ] && cp -f ../WOOFMERGEVARS rootfs-complete/etc/rc.d/

#120522 precise puppy, seamonkey was crashing. needed /usr/share/glib-2.0/schemas/apps.gecko-mediaplayer.preferences,
# but also needs this compile operation...
# ***NOTICE*** cross-build, this will have to be executed at first boot...
if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then
 if [ -d rootfs-complete/usr/share/glib-2.0/schemas ];then
  [ -e rootfs-complete/usr/bin/glib-compile-schemas ] && chroot rootfs-complete /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
 fi
 #120523 seems need this too... 140121 ${lsuffix}
 if [ -d rootfs-complete/usr/lib${lsuffix}/gio/modules ];then
  [ -e rootfs-complete/usr/bin/gio-querymodules ] && chroot rootfs-complete /usr/bin/gio-querymodules /usr/lib${lsuffix}/gio/modules
 fi
fi

#120618 raspi debian squeeze, links as main browser, helpsurfer html viewer, gtkmoz missing...
#120620 problem, helpsurfer cannot display /usr/share/doc/index.html, so do not point gtkmoz at basichtmlviewer...
if [ ! -e rootfs-complete/usr/local/bin/gtkmoz ];then
 ln -s defaultbrowser rootfs-complete/usr/local/bin/gtkmoz
fi

#120704 some constraints in a small system...
if [ "$SDFLAG" != "" ];then
 #ask if want to load swap partition/file...
 echo
 echo "Puppy will normally load a swap partition or swap file if it/they exist,
however you can choose to ignore them and not load them at bootup."
 echo -n "Press ENTER key to load swap at bootup, any key otherwise: "
 read wantswap
 if [ "$wantswap" != "" ];then
  echo "BOOT_DISABLESWAP='yes'" >> rootfs-complete/etc/rc.d/BOOTCONSTRAINED #read by /etc/rc.d/rc.sysinit
  echo "...BOOT_DISABLESWAP='yes' in /etc/rc.d/BOOTCONSTRAINED, read by rc.sysinit."
 fi
 #this also reduces writes to flash card... um, relatime seems to be the default anyway... 120707 change to noatime...
 echo "BOOT_ATIME='noatime'" >> rootfs-complete/etc/rc.d/BOOTCONSTRAINED #rc.sysinit will remount / with 'relatime' (constrains updating access time). 120707 change to noatime.
 #this file in rootfs-skeleton, overwrite...
 echo 'none          /proc        proc     defaults,noatime               0 0
none          /sys         sysfs    defaults,noatime               0 0
none          /dev/pts     devpts   gid=2,mode=620,noatime         0 0
shmfs         /dev/shm     tmpfs    defaults,noatime      0 0' > rootfs-complete/etc/fstab
 echo "BOOT_DIRTYWRITE='1500'" >> rootfs-complete/etc/rc.d/BOOTCONSTRAINED #defer writing to disk. refer: http://www.lesswatts.org/tips/disks.php
 echo "BOOT_SCHEDULER='deadline'" >> rootfs-complete/etc/rc.d/BOOTCONSTRAINED #my kernel defaults to 'cfq', change to 'deadline'. refer: http://tombuntu.com/index.php/2008/09/04/four-tweaks-for-using-linux-with-solid-state-drives/
 #echo "BOOT_QUICKSETUP='quickarm'" >> rootfs-complete/etc/rc.d/BOOTCONSTRAINED #120708 leaves out X config frame. 120714 removed.
 echo "BOOT_UDEVDCHILDREN='1'" >> rootfs-complete/etc/rc.d/BOOTCONSTRAINED #120709 is this a good idea? append --children-max=1 to udevd in rc.sysinit.
 echo "BOOT_BOARD='${BOOT_BOARD}'" >> rootfs-complete/etc/rc.d/BOOTCONSTRAINED #120714 read by quicksetup.
 echo "BOOT_DISABLEXORGWIZARD='yes'" >> rootfs-complete/etc/rc.d/BOOTCONSTRAINED #120723 read in xwin, also xorgwizard. also see below.
 echo "BOOT_DISABLEALSACONF='yes'" >> rootfs-complete/etc/rc.d/BOOTCONSTRAINED #120724 'alsaconf' does not work on arm boards.
fi

#120723 code moved down, add BOOT_DISABLEXORGWIZARD test...
#110413 variable DISTRO_XORG_AUTO in /etc/DISTRO_SPECS, see also /usr/bin/xwin
autoflag='no'
if [ "$BOOT_DISABLEXORGWIZARD" != "yes" ];then
 if [ "$DISTRO_XORG_AUTO" ];then
  autoflag="$DISTRO_XORG_AUTO"
 else
  if vercmp "$XORGVER" ge "1.7.0";then #110621
   echo
   echo "Do you want Xorg to start automatically at first boot (or at 'pfix=ram'"
   echo "kernel boot param) or run Xorg Wizard? The latter has been the case for"
   echo "earlier puppies. Automatic startup of X usually works, though in some"
   echo "cases may choose the wrong monitor resolution or driver -- which can be"
   echo "fixed by running Xorg Wizard afterward."
   echo -n "Press ENTER only for automatic Xorg: "
   read xorgauto
   [ "$xorgauto" = "" ] && autoflag='yes'
  fi
 fi
fi
if [ "`grep '^DISTRO_XORG_AUTO' rootfs-complete/etc/DISTRO_SPECS`" = "" ];then #110621
 echo "DISTRO_XORG_AUTO='${autoflag}'" >> rootfs-complete/etc/DISTRO_SPECS
else
 REGEXauto="s%^DISTRO_XORG_AUTO.*%DISTRO_XORG_AUTO='${autoflag}'%"
 sed -i -e "$REGEXauto" rootfs-complete/etc/DISTRO_SPECS
fi

#121123 want this info in ppm...
echo '#multiarch distros, such as Ubuntu, will have this. ex: /usr/lib/i386-linux-gnu, so DISTRO_ARCHDIR=i386-linux-gnu' >> rootfs-complete/etc/DISTRO_SPECS
echo "DISTRO_ARCHDIR='${ARCHDIR}'" >> rootfs-complete/etc/DISTRO_SPECS
echo "DISTRO_ARCHDIR='${ARCHDIR}'" >> initrd-tree/DISTRO_SPECS
#130114 revert...
##130112 just in case 2createpackages did not set this variable...
#[ "$DISTRO_ARCHDIR_SYMLINKS" = "" ] && echo "DISTRO_ARCHDIR_SYMLINKS='${xDISTRO_ARCHDIR_SYMLINKS}'" >> rootfs-complete/etc/DISTRO_SPECS
#[ "$DISTRO_ARCHDIR_SYMLINKS" = "" ] && echo "DISTRO_ARCHDIR_SYMLINKS='${xDISTRO_ARCHDIR_SYMLINKS}'" >> initrd-tree/DISTRO_SPECS

#120709 debian squeeze has /usr/share/misc/pci.ids, usb.ids, which confuses some apps, create symlinks...
if [ ! -e rootfs-complete/usr/share/pci.ids ];then
 if [ -e rootfs-complete/usr/share/misc/pci.ids ];then
  ln -s misc/pci.ids rootfs-complete/usr/share/pci.ids
 fi
fi
if [ ! -e rootfs-complete/usr/share/usb.ids ];then
 if [ -e rootfs-complete/usr/share/misc/usb.ids ];then
  ln -s misc/usb.ids rootfs-complete/usr/share/usb.ids
 fi
fi

#120830 my hunspell-en-us pet has pinstall.sh that creates symlinks of en_US.aff and en_US.dic
#from seamonkey and firefox. however, need to do this thoroughly...
#note: see also woof rootfs-skeleton/usr/share/doc/langpack-template (used by momanager). #140121 ${lsuffix}
if [ -d rootfs-complete/usr/share/hunspell ];then
 for ONEHUN in `find rootfs-complete/usr/share/hunspell -mindepth 1 -maxdepth 1 -type f -name '*.dic' -o -name '*.aff' | tr '\n' ' '`
 do
  HUNBASE="`basename $ONEHUN`"
  DICTDIRS="`find rootfs-complete/usr/lib${lsuffix} -mindepth 2 -maxdepth 2 -type d -name dictionaries | tr '\n' ' '`"
  for ONEDICTDIR in $DICTDIRS
  do
   [ ! -e ${ONEDICTDIR}/${HUNBASE} ] && ln -s ../../../share/hunspell/${HUNBASE} ${ONEDICTDIR}/${HUNBASE}
  done
 done
fi

#121022 may need this (see below similar code if python in devx)...
if [ "$(echo "$PKGS_SPECS_TABLE" | grep '^yes|python|' | grep 'exe>dev')" = "" ];then
 [ -e rootfs-complete/usr/bin/python2.5 ] && ln -s python2.5 rootfs-complete/usr/bin/python 2>/dev/null
 [ -e rootfs-complete/usr/bin/python2.6 ] && ln -s python2.6 rootfs-complete/usr/bin/python 2>/dev/null
 [ -e rootfs-complete/usr/bin/python2.7 ] && ln -s python2.7 rootfs-complete/usr/bin/python 2>/dev/null
fi

#121203 bacon hug gui apps need some .so libs, for example libgtk-x11-2.0.so,
#(refer /usr/sbin/welcome1stboot.bac), however these are only in the devx.
#so need to create them...
#note, i think this problem only in wary/racy, as gtk template creates these symlinks.
if [ ! -e rootfs-complete/usr/lib${lsuffix}/libgtk-x11-2.0.so ];then
 FNDGTKLIB="$(find rootfs-complete/usr/lib${lsuffix}/ -mindepth 1 -maxdepth 1 -name 'libgtk-x11-2.0.so.[0-9]' | head -n 1 | rev | cut -f 1 -d '/' | rev)"
 [ "$FNDGTKLIB" ] && ln -s $FNDGTKLIB rootfs-complete/usr/lib${lsuffix}/libgtk-x11-2.0.so #2> /dev/null
fi
#i'll do the same for libgdk-x11-2.0.so ...
if [ ! -e rootfs-complete/usr/lib${lsuffix}/libgdk-x11-2.0.so ];then
 FNDGDKLIB="$(find rootfs-complete/usr/lib${lsuffix}/ -mindepth 1 -maxdepth 1 -name 'libgdk-x11-2.0.so.[0-9]' | head -n 1 | rev | cut -f 1 -d '/' | rev)"
 [ "$FNDGTKLIB" ] && ln -s $FNDGTKLIB rootfs-complete/usr/lib${lsuffix}/libgdk-x11-2.0.so #2> /dev/null
fi

#121212 puinstall.sh was not getting removed...
[ -f rootfs-complete/puninstall.sh ] && rm -f rootfs-complete/puninstall.sh

#130326 now permanently 96... 130327 reintroduce a choice...
##130129 /root/.Xresources has dpi set to 78 in all prior pups. In future, might prefer 96...
#141206 now have DEFAULT_THEME_XORG_TEXT_DPI (usually =96).
if [ "$DEFAULT_THEME_XORG_TEXT_DPI" != "" ];then #141206
 dpisizeflag="$DEFAULT_THEME_XORG_TEXT_DPI"
else
 DPILINE="$(grep '^Xft\.dpi:' rootfs-complete/root/.Xresources)"
 DPIVALUE=$(echo -n "$DPILINE" | tr '\t' ' ' | tr -s ' ' | cut -f 2 -d ' ')
 echo
 echo "rootfs-complete/root/.Xresources has this line:"
 echo "$DPILINE"
 echo "which sets the displayed size of fonts on the screen (but not bitmap fonts)."
 DPINEXT1=`expr $DPIVALUE + 6`
 DPINEXT2=`expr $DPINEXT1 + 6`
 echo "To accept this size, just press ENTER key."
 echo "However, if you would like fonts to display bigger on the screen, type in
a value now. The value should be in steps of 6, for example ${DPINEXT1} or ${DPINEXT2}.
The default size is ${DPIVALUE}, you might try the next-up, ${DPINEXT1}."
 echo -n "Press ENTER key for '${DPIVALUE}' dpi, or type a value: "
 read dpisizeflag
fi
if [ "$dpisizeflag" ];then #130512 scsijon: fix size...
 fsPTN='s%^Xft\.dpi:.*%Xft.dpi: '"${dpisizeflag}%"
 sed -i -e "$fsPTN" rootfs-complete/root/.Xresources
 echo "...ok, dpi now ${dpisizeflag}"
fi

#130226 wary: /usr/bin/X was missing...
if [ ! -e rootfs-complete/usr/bin/X ];then
 if [ -e rootfs-complete/usr/bin/Xorg -o -h rootfs-complete/usr/bin/Xorg ];then
  ln -s Xorg rootfs-complete/usr/bin/X
 fi
fi

#130601 optional, run internet apps as user 'spot'... #140121 ${lsuffix}
echo
echo "Puppy is normally run as the 'administrator' (root) user, though there is
also 'fido' which is not currently very mature.
The structure of Puppy is such that we consider root to be safe (with a full
disclaimer of any responsibility if anything does go wrong), but there is a
compromise, to run as root but to run Internet apps as user 'spot'."
if [ -e rootfs-complete/usr/lib${lsuffix}/seamonkey ];then
 echo
 echo "Type any character then ENTER if you want SeaMonkey to run as spot,
or just ENTER not to. Note, in a running 'Puppy Menu->System->Login & Security
Manager' can be used to enable or disable running as spot."
 echo
 echo -n "ENTER only to not run SeaMonkey as spot: "
 read SPOTAPPS
 if [ "$SPOTAPPS" ];then
  ../support/setup-spot 'seamonkey=true' #130604
  echo '...ok, SeaMonkey will run as spot.'
 else
  echo 'seamonkey=false' > rootfs-complete/root/.spot-status #130604 so that an unticked checkbox will display in loginmanager.
  echo '...ok, SeaMonkey will not run as spot.'
 fi
fi
if [ -e rootfs-complete/usr/lib${lsuffix}/firefox* ];then
 echo
 echo "Type any character then ENTER if you want firefox to run as spot,
or just ENTER not to. Note, in a running Puppy 'Menu->System->Login & Security
Manager' can be used to enable or disable running as spot."
 echo
 echo -n "ENTER only to not run firefox as spot: "
 read SPOTAPPS
 if [ "$SPOTAPPS" ];then
  ../support/setup-spot "firefox=true"
  echo "...ok, firefox will run as spot."
 else
  echo "firefox=false" >> rootfs-complete/root/.spot-status #so that an unticked checkbox will display in loginmanager.
  echo "...ok, firefox will not run as spot."
 fi
fi
#130720 add QtWeb... 130723 add opera... 130821 add firefox... 132211 firefox didn't work, separate, see above
for AINTERNETAPP in QtWeb opera
do
 [ -e rootfs-complete/usr/bin/${AINTERNETAPP} ] || continue
 echo
 echo "Type any character then ENTER if you want ${AINTERNETAPP} to run as spot,
or just ENTER not to. Note, in a running Puppy 'Menu->System->Login & Security
Manager' can be used to enable or disable running as spot."
 echo
 echo -n "ENTER only to not run ${AINTERNETAPP} as spot: "
 read SPOTAPPS
 if [ "$SPOTAPPS" ];then
  ../support/setup-spot "${AINTERNETAPP}=true"
  echo "...ok, ${AINTERNETAPP} will run as spot."
 else
  echo "${AINTERNETAPP}=false" >> rootfs-complete/root/.spot-status #so that an unticked checkbox will display in loginmanager.
  echo "...ok, ${AINTERNETAPP} will not run as spot."
 fi
done
#setup-spot is also called by the Login & Security Manager in running Puppy...
cp -f ../support/setup-spot rootfs-complete/usr/sbin/

#130618 code above sets DEVTMPFSFLG...
#130612 if kernel has CONFIG_DEVTMPFS=y (and CONFIG_DEVTMPFS_MOUNT=y) then empty /dev...
if [ $DEVTMPFSFLG -ne 0 ];then
 rm -rf rootfs-complete/dev/* #see also 'init' script in initrd.
 sed -i -e 's%^DEVTMPFSFLG=.*%DEVTMPFSFLG=2%' rootfs-complete/etc/rc.d/rc.sysinit
fi

#130725 want Menu -> Help to use secondary browser... 130726 fix...
#code to detect dual browsers, taken out of /usr/sbin/delayedrun...
ALTBROWSER=''
#the pinstall.sh script in qtweb and opera pets create separate desktop icon if there is another major browser...
[ "`grep 'QtWeb' rootfs-complete/root/Choices/ROX-Filer/PuppyPin`" != "" ] && ALTBROWSER='QtWeb'
[ "`grep 'opera' rootfs-complete/root/Choices/ROX-Filer/PuppyPin`" != "" ] && ALTBROWSER='opera'
if [ "$ALTBROWSER" != "" ];then
 MAINBROWSER="`grep '^exec' rootfs-complete/usr/local/bin/defaultbrowser | tr '\t' ' ' | tr -s ' ' | cut -f 2 -d ' '`"
 if [ "$MAINBROWSER" != "$ALTBROWSER" ];then
  echo '#!/bin/sh' > rootfs-complete/usr/local/bin/defaulthtmlviewer
  echo "exec ${ALTBROWSER} \"\$@\"" >> rootfs-complete/usr/local/bin/defaulthtmlviewer
  chmod 755 rootfs-complete/usr/local/bin/defaulthtmlviewer
 fi
fi

rm -rf rootfs-complete/tmp/* #121123 some above chroot operations may have left something behind in here.

BUILD_SFS='yes'
if [ "$SDFLAG" != "" ]; then
 echo
 echo "Since you are building a SD card image, it is not necessary"
 echo "to build the main sfs file.  Press ENTER only to skip,"
 echo "any other character then ENTER to build it anyway."
 read buildanyway
 if [ "$buildanyway" = '' ]; then
  BUILD_SFS='no'
 fi
fi
if [ "$BUILD_SFS" = 'yes' ]; then
###########
#build the rootfs-complete sfs...
echo
echo "Now building the main f.s., ${PUPPYSFS}..."
sync
rm -f build/${PUPPYSFS} 2>/dev/null
../support/${MKSQUASHFS} rootfs-complete build/${PUPPYSFS} ${COMPCHOICE} #100911 110713
sync
echo -n "$IDSTRING" >> build/${PUPPYSFS} #100911 16-byte id-string appended to file.
sync
###########
fi # if BUILD_SFS

#if separate 'zdrv' exists, copy that into live-cd also...
if [ -f ${ZDRVSFS} ];then #100911
 echo -n "$IDSTRING" >> ${ZDRVSFS} #100911 16-byte id-string appended to file.
 echo
 echo "Press ENTER only to copy ${ZDRVSFS} into build/,"
 echo -n "so that it becomes part of the live-CD. Any other char to leave it out: "
 read insertzdrv
 if [ "$insertzdrv" = "" ];then
  echo "Copying ${ZDRVSFS} into build/" #100911
  cp -a ${ZDRVSFS} build/ #100911
 fi
 sync
fi

##create isolinux.cfg...
#echo "Creating build/isolinux.cfg..."
#echo "default puppy
#display boot.msg
#prompt 1
#label puppy
#kernel vmlinuz
#append initrd=initrd.gz pmedia=cd
#timeout 50" > build/isolinux.cfg

WOOF_OUTPUT="woof-output-${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}${SCSIFLAG}"
[ -d ../$WOOF_OUTPUT ] || mkdir -p ../$WOOF_OUTPUT

#build live-cd .iso file...
echo "Now building ${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}${SCSIFLAG}.iso"
rm -f ../${WOOF_OUTPUT}/${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}${SCSIFLAG}.iso 2>/dev/null
if [ -f rootfs-complete/usr/lib/syslinux/isolinux.bin ];then
 cp -a rootfs-complete/usr/lib/syslinux/isolinux.bin build/
else
 cp -a ../boot/isolinux.bin build/
fi

case $DISTRO_FILE_PREFIX in #091203
 wary) #101026
  BOOTLOGO="puppy"
  BOOTLABEL="puppy"
  #REMOVELINE1='pfix=copy' #100916
  REMOVELINE1=''
  VERPREFIX="wary "
  [ "`echo "$DISTRO_NAME" | grep "Racy"`" != "" ] && VERPREFIX="racy " #110917
 ;;
 racy) #110917
  BOOTLOGO="racy-splash" #110928
  BOOTLABEL="puppy"
  REMOVELINE1=''
  VERPREFIX="racy "
 ;;
 sqzd) #110517 squeeze debian
  BOOTLOGO="puppy"
  BOOTLABEL="puppy"
  REMOVELINE1=''
  VERPREFIX="squeezed "
 ;;
 drake|drak) #110721
  BOOTLOGO="puppy"
  BOOTLABEL="puppy"
  REMOVELINE1=''
  VERPREFIX="drake "
 ;;
 q*) #quirky, qurky, qrky
  BOOTLOGO="quirky"
  BOOTLABEL="puppy" #"quirky"
  #REMOVELINE1='pfix=copy'
  VERPREFIX="puppy " #100319
  [ "$DISTRO_FILE_PREFIX" = "qret" ] && VERPREFIX="retro " #100514 quirky-retro.
 ;;
 *)
  BOOTLOGO="puppy"
  BOOTLABEL="puppy"
  #REMOVELINE1='pfix=copy' #100916
  VERPREFIX=""
 ;;
esac
#111006 generic mechanism, boot logo for any pup...
[ -f ../boot/boot-dialog/${DISTRO_FILE_PREFIX}-splash.gif ] && BOOTLOGO="${DISTRO_FILE_PREFIX}-splash"
blPATTERN="s%BOOTLABEL%${BOOTLABEL}%"
namePATTERN="s/DISTRO_NAME/${DISTRO_NAME}/"
verPATTERN="s/DISTRO_VERSION/${DISTRO_VERSION}/"
prefixPATTERN="s/DISTRO_FILE_PREFIX/${DISTRO_FILE_PREFIX}/"
sed -e "$namePATTERN" -e "$verPATTERN" -e "$prefixPATTERN" -e "$blPATTERN" ../boot/boot-dialog/help.msg > build/help.msg
if [ "$REMOVELINE1" != "" ];then
 grep -v "$REMOVELINE1" build/help.msg > /tmp/3builddistro-help.msg
 mv -f /tmp/3builddistro-help.msg build/help.msg
fi
#100916 second help screen...
sed -e "$namePATTERN" -e "$verPATTERN" -e "$prefixPATTERN" -e "$blPATTERN" ../boot/boot-dialog/help2.msg > build/help2.msg
if [ "$REMOVELINE1" != "" ];then
 grep -v "$REMOVELINE1" build/help2.msg > /tmp/3builddistro-help2.msg
 mv -f /tmp/3builddistro-help2.msg build/help2.msg
fi

#130515 asked above whether to disable 'pupdesk.flg' mechanism...
if [ "$PUPDESKFLG" != "" ];then
 sed -i -e 's%next bootup will force run of Video Wizard: choose alternate driver/settings%next startup type boot param "puppy pfix=nox" to enable run of Video Wizard%' build/help.msg
fi

cp -f ../boot/boot-dialog/boot.msg build/boot.msg
cat ../boot/boot-dialog/isolinux.cfg | sed -e "$blPATTERN" > build/isolinux.cfg
export RGBDEF=/usr/share/X11/rgb.txt
../boot/boot-dialog/textongif.sh ../boot/boot-dialog/${BOOTLOGO}.gif "${VERPREFIX}${RIGHTVER}" > logo.gif
../boot/boot-dialog/gif2lss logo.gif > build/logo.16

#120217 translate early-boot splash screens... (expect building with langpack)
if [ "$DEFAULTLANG" != "en_US" ];then
 DEFAULTLANG1="${DEFAULTLANG%_*}" #ex: de
 DEFAULTLANG12="${DEFAULTLANG%.*}" #ex: de_DE
 SPLASHFILE=""
 [ -f ../rootfs-skeleton/usr/share/sss/initrd_strings.${DEFAULTLANG12} ] && SPLASHFILE="../rootfs-skeleton/usr/share/sss/initrd_strings.${DEFAULTLANG12}"
 [ ! "$SPLASHFILE" ] && [ -f ../rootfs-skeleton/usr/share/sss/initrd_strings.${DEFAULTLANG1} ] && SPLASHFILE="../rootfs-skeleton/usr/share/sss/initrd_strings.${DEFAULTLANG1}"
 if [ "$SPLASHFILE" ];then
  #translate boot.msg...
  sPTN="/^\[boot.msg\]/,/^$/p" #this is a multi-line block find expression.
  CODEBLOCK="`sed -n "$sPTN" ${SPLASHFILE} | sed -e '/^#/d' -e '/%%/d' -e '/^$/d' -e '/^\[/d'`" #extracts just the relevant block of lines.
  if [ "$CODEBLOCK" ];then
   echo "$CODEBLOCK" > /tmp/3builddistro-boot-splash-translation
   sed -i -f /tmp/3builddistro-boot-splash-translation build/boot.msg
  fi
  #translate help.msg...
  sPTN="/^\[help.msg\]/,/^$/p" #this is a multi-line block find expression.
  CODEBLOCK="`sed -n "$sPTN" ${SPLASHFILE} | sed -e '/^#/d' -e '/%%/d' -e '/^$/d' -e '/^\[/d'`" #extracts just the relevant block of lines.
  if [ "$CODEBLOCK" ];then
   echo "$CODEBLOCK" > /tmp/3builddistro-boot-splash-translation
   sed -i -f /tmp/3builddistro-boot-splash-translation build/help.msg
  fi
 fi
fi

#110426 help file for Windows users
CUT2INSERT="`echo -n "$DISTRO_FILE_PREFIX" | cut -c 1,2`"
CUT3INSERT="`echo -n "$DISTRO_FILE_PREFIX" | cut -c 1,2,3`"
cut2PATTERN="s%CUT2INSERT%${CUT2INSERT}%"
cut3PATTERN="s%CUT3INSERT%${CUT3INSERT}%"
sed -e "$verPATTERN" -e "$prefixPATTERN" -e "$cut2PATTERN" -e "$cut3PATTERN" ../boot/README.HTM > build/README.HTM

sync


if [ "$SDFLAG" = "" ];then #120506
 $MKISOFS -D -R -o ../${WOOF_OUTPUT}/${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}${SCSIFLAG}.iso -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table ./build/
 sync
 # 131227 iguleder: made the generated ISO image hybrid, so it can be written to flash drives using dd
 isohybrid="$(which isohybrid)"
 [ -n "$isohybrid" ] && $isohybrid ../${WOOF_OUTPUT}/${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}${SCSIFLAG}.iso
 CUR_DIR="$PWD"
 cd ../${WOOF_OUTPUT}
 md5sum ${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}${SCSIFLAG}.iso > ${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}${SCSIFLAG}.iso.md5.txt
 cd "$CUR_DIR"
 echo
 echo "Would you like to burn it to a CD? "
 echo -n "ENTER only for yes, or any printable char then ENTER not to: "
 read writeitnow
 if [ "$writeitnow" = "" ];then
  CDR=""
  [ -f /etc/cdburnerdevice ] && CDR="/dev/`cat /etc/cdburnerdevice`"
  [ "$CDR" = "" ] && CDR='/dev/sr0'
  CDDESCR="`probedisk2 | grep '|optical|' | grep "$CDR" | cut -f 3 -d '|'`"
  echo -n 'Type "y" for multisession, else just ENTER: '
  read BURNMULTI
  if [ "$BURNMULTI" = "y" ];then
   BURNMULTI="-multi -tao -pad"
  else
   BURNMULTI="-dao"
  fi
  echo "Please insert blank CD into $CDR
(which is described as: ${CDDESCR})
-- also be sure that it is unmounted."
  echo -n "Then hit ENTER key: "
  read yayburn
# $CDRECORD $BURNMULTI -data -eject -v speed=4 padsize=300k dev=ATAPI:$CDR ${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}${SCSIFLAG}.iso
  $CDRECORD $BURNMULTI -data -eject -v speed=4 padsize=300k dev=$CDR ../${WOOF_OUTPUT}/${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}${SCSIFLAG}.iso
  sync
  eject $CDR
  echo "...done"
 fi
else #120506 sd image
 
 echo
 echo "Please choose the SD-card skeleton image file, to suit target board."
 CNT=1
 echo -n "" > /tmp/3builddistro-probeimage
 ls -1 ../sd-skeleton-images | grep 'xz$' | grep "$BOOT_BOARD" | #120714 BOOT_BOARD is raspi, mele, or odroidx.
 while read ONEIMAGE
 do
  echo "${CNT} ${ONEIMAGE}" >> /tmp/3builddistro-probeimage
  CNT=`expr $CNT + 1`
 done
 while [ 1 ];do
  echo
  echo "Type number which is your choice of SD image:"
  cat /tmp/3builddistro-probeimage
  read sdnumber
  SDIMAGE="`cat /tmp/3builddistro-probeimage | head -n $sdnumber | tail -n 1 | cut -f 2 -d ' '`"
  echo -n "You chose '${SDIMAGE}' Press ENTER if correct: "
  read sdcorrect
  [ "$sdcorrect" = "" ] && break
 done
 SDBASE="`basename ../sd-skeleton-images/${SDIMAGE} .xz`"
 echo
 echo "Please type the name that you want to give the SD image file, or press ENTER"
 SDBASEBASE="`basename $SDBASE .img | sed -e 's%-201[0-9]*%-%' -e 's%-skeleton%-%' | cut -f 1,2,3 -d '-'`"
 PUPIMG="${SDBASEBASE}-${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}.img"
 echo -n "only for the default [${PUPIMG}]: "
 read PUPIMG1
 [ "$PUPIMG1" != "" ] && PUPIMG="`basename $PUPIMG1 .img`.img"
 echo "...chosen $PUPIMG"
 echo
 
 #need to know uncompressed size of image...
 echo "Uncompressing image, please wait..."
 [ -f ../${WOOF_OUTPUT}/${PUPIMG} ] && rm -f ../${WOOF_OUTPUT}/${PUPIMG}
 unxz --stdout ../sd-skeleton-images/${SDIMAGE} > ../${WOOF_OUTPUT}/${PUPIMG}
 if [ $? -ne 0 ];then
  [ -f ../${WOOF_OUTPUT}/${PUPIMG} ] && rm -f ../${WOOF_OUTPUT}/${PUPIMG}
  echo "Uncompress fail. Aborting."
  exit 1
 fi
 sync

 #120703 allow 2nd partition to be ext2, ext3 or ext4...
 SDIMGINFO="`disktype ../${WOOF_OUTPUT}/${PUPIMG}`"
 SDFS2="`echo "$SDIMGINFO" | grep -o 'Ext[0-9] file system' | cut -f 1 -d ' ' | tr '[A-Z]' '[a-z]'`"
 case $SDFS2 in
  ext2|ext3|ext4) SDFS2="ext4" ;; #120706 hack for now. disktype misreports ext4 without journal as ext2.
  *)
   echo -n "wrong f.s. ${SDFS2} in ${SDIMAGE}, aborting. Press ENTER: "
   read exitme
   exit
  ;;
 esac
 
 #130530 better to write everything to the image file first, then write to sd afterward...
 #need to know the offsets of the filesystems...
 P1BYTES=`echo "$SDIMGINFO" | grep '^Partition 1:' | cut -f 2 -d '(' | cut -f 1 -d ' '`
 P1SECTORS=`echo "$SDIMGINFO" | grep '^Partition 1:' | cut -f 2 -d '(' | cut -f 3 -d ' '`
 P1STARTSECTORS=`echo "$SDIMGINFO" | grep '^Partition 1:' | rev | cut -f 1 -d ' ' | rev | cut -f 1 -d ')'`
 BYTESPERSECTOR=`expr $P1BYTES \/ $P1SECTORS` #normally 512.
 P1STARTBYTES=`expr $P1STARTSECTORS \* $BYTESPERSECTOR`
 
 P2BYTES=`echo "$SDIMGINFO" | grep '^Partition 2:' | cut -f 2 -d '(' | cut -f 1 -d ' '`
 P2SECTORS=`echo "$SDIMGINFO" | grep '^Partition 2:' | cut -f 2 -d '(' | cut -f 3 -d ' '`
 P2STARTSECTORS=`echo "$SDIMGINFO" | grep '^Partition 2:' | rev | cut -f 1 -d ' ' | rev | cut -f 1 -d ')'`
 P2STARTBYTES=`expr $P2STARTSECTORS \* $BYTESPERSECTOR`
 
 echo
 echo "Copying Linux kernel to SD image file..."
 mkdir -p /mnt/sdimagep1
 mkdir -p /mnt/sdimagep2
 mount-FULL -t vfat -o loop,offset=${P1STARTBYTES} ../${WOOF_OUTPUT}/${PUPIMG} /mnt/sdimagep1
 if [ $? -ne 0 ];then
  echo "Sorry, mounting vfat partition 1 (at offset ${P1STARTBYTES}) of ${PUPIMG} failed. Aborting script."
  exit 1
 fi
 #120613 restore correct kernel image name...
 case $REALKERNAME in
  uImage)     cp -f build/vmlinuz /mnt/sdimagep1/uImage ;;
  kernel.img) cp -f build/vmlinuz /mnt/sdimagep1/kernel.img ;;
  *)          cp -f build/vmlinuz /mnt/sdimagep1/ ;;
 esac
 echo -n "$REALKERNAME" > /mnt/sdimagep1/REALKERNAME #just in case need to know, in a running puppy.
 sync
 busybox umount /mnt/sdimagep1 2>/dev/null
 echo "...done"
 
 echo
 echo "Copying Puppy filesystem to SD image file, please wait..."
 mount-FULL -t ${SDFS2} -o loop,offset=${P2STARTBYTES} ../${WOOF_OUTPUT}/${PUPIMG} /mnt/sdimagep2
 if [ $? -ne 0 ];then
  echo "Sorry, mounting ${SDFS2} partition 2 (at offset ${P2STARTBYTES}) of ${PUPIMG} failed. Aborting script."
  exit 1
 fi
 cp -a rootfs-complete/* /mnt/sdimagep2/
 sync
 #120704 add to /etc/fstab...
# echo "/dev/${SDDEVICE}2     /     ${SDFS2}     defaults,noatime   0 1" >> /mnt/sdimagep2/etc/fstab #120707 change relatime to noatime.
 #not sure if the root partition is referred to as /dev/root or /dev/mmcblk0p2 on the raspi
 echo "/dev/mmcblk0p2     /     ${SDFS2}     defaults,noatime   0 1" >> /mnt/sdimagep2/etc/fstab
 sync
 echo "...done"
 busybox umount /mnt/sdimagep2 2>/dev/null
 
 IMGBYTES=`stat --format=%s ../${WOOF_OUTPUT}/$PUPIMG`
 
 echo
 IMGK=`expr $IMGBYTES \/ 1024`
 echo "The image file is ${IMGK}KB, so needs to be compressed for distribution."

 echo "If you only want to write to SD card choose 'none' which is the fastest."
 echo "You can also compress the image file yourself later."
 echo "Choose compression type:
1 xz   (smallest, slowest)
2 gz   (larger, faster)
3 none (huge, instant)"
 read compresstype
 case $compresstype in
  1) COMPRESS='xz' ;;
  2) COMPRESS='gz' ;;
  *) COMPRESS='none' ;;
 esac
 echo "...you chose $COMPRESS"

 if [ "$COMPRESS" != 'none' ]; then
 echo "Compressing, please wait..."
 [ -f ../${WOOF_OUTPUT}/${PUPIMG}.${COMPRESS} ] && rm -f ../${WOOF_OUTPUT}/${PUPIMG}.${COMPRESS}
 if [ "$COMPRESS" = 'xz' ]; then
 xz --stdout ../${WOOF_OUTPUT}/${PUPIMG} > ../${WOOF_OUTPUT}/${PUPIMG}.xz
 elif [ "$COMPRESS" = 'gz' ]; then
 gzip --stdout ../${WOOF_OUTPUT}/${PUPIMG} > ../${WOOF_OUTPUT}/${PUPIMG}.gz
 fi
 sync
 echo "...${PUPIMG}.${COMPRESS} created."
 COMPRIMGBYTES=`stat --format=%s ../${WOOF_OUTPUT}/${PUPIMG}.${COMPRESS}`
 echo
 echo "The image is now ${PUPIMG}.${COMPRESS} and is ${COMPRIMGBYTES}bytes."
 COMPRIMGK=`expr $COMPRIMGBYTES \/ 1024`
 echo "(${COMPRIMGK}KB)"
 echo "Image file may be distributed to others!"
 echo
 fi # if COMPRESS

 echo
 echo "Would you like to write it to a SD card?  ENTER only for no,"
 echo -n "or any printable char then ENTER to write image to SD card: "
 read writeSD
 if [ "$writeSD" = "" ];then
  WRITE_SD="no"
 else
  WRITE_SD="yes"
 fi

 if [ "$WRITE_SD" = "yes" ];then

  echo
  echo "Please insert the SD card. Make sure that it is the same size or bigger than
 indicated on the filename of the skeleton image file that you chose."
  echo -n "Press ENTER after it is inserted: "
  read waitinsert
  sleep 2
  while [ 1 ];do
   CNT=1
   echo -n "" > /tmp/3builddistro-probedisk
   probedisk |
   while read ONEPROBE
   do
    echo "${CNT} ${ONEPROBE}" >> /tmp/3builddistro-probedisk
    CNT=`expr $CNT + 1`
   done
   echo
   echo "Type number which is your SD card:"
   cat /tmp/3builddistro-probedisk
   read sdnumber
   SDDEVICE="`cat /tmp/3builddistro-probedisk | head -n $sdnumber | tail -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '|'`"
   echo -n "You chose ${SDDEVICE} Press ENTER if correct: "
   read sdcorrect
   [ "$sdcorrect" = "" ] && break
  done
  echo
  echo "Sanity check: ${PUPIMG}
 is to be written to ${SDDEVICE}."
  echo -n "Press ENTER to continue: "
  read yepgo

  SDCARDINFO="`disktype ${SDDEVICE}`"
  #120506b check that sd card big enough...
  SDCARDBYTES=`echo "$SDCARDINFO" | grep '^Block device' | cut -f 2 -d '(' | cut -f 1 -d ' '` #ex: 4023386112
  if [ $IMGBYTES -gt $SDCARDBYTES ];then
   echo
   echo "Sorry, the image file is ${IMGBYTES}bytes, however the
SD card is only ${SDCARDBYTES}bytes. Cannot continue."
   exit 1
  fi

  echo
  echo "Writing image file ${PUPIMG} to SD card ${SDDEVICE}..."
  dd if=../${WOOF_OUTPUT}/${PUPIMG} of=${SDDEVICE} bs=4M #120704 added bs=4M
  if [ $? -ne 0 ];then
   echo "Sorry, operation failure. Aborting script."
   exit 1
  fi
  sync
 fi # if WRITE_SD

 if [ "$COMPRESS" != 'none' ]; then
 rm -f ../${WOOF_OUTPUT}/${PUPIMG}
 fi

#130530 this is old code...
# echo "Writing skeleton image to ${SDDEVICE}, please wait very patiently..."
# dd if=${SDBASE} of=${SDDEVICE} bs=4M #120704 added bs=4M
# if [ $? -ne 0 ];then
#  echo "Sorry, operation failure. Aborting script."
#  exit 1
# fi
# sync
# rm -f $SDBASE
# 
# FIXEXTPTN=''
# if [ "$SDFS2" = "ext2" ];then #120704 120706 see hack above, bypass.
#  echo
#  echo "The 2nd partition is 'ext2', but you may bump it to 'ext4' if you wish."
#  echo -n "ENTER only to keep ext2, any char for ext4: "
#  read bumpit
#  if [ "$bumpit" != "" ];then
#   echo
#   echo "Creating ext4 f.s. on partition ${SDDEVICE}2, without a journal..."
#   #mke2fs -t ext4 -O ^has_journal -L puppy -m 0 -b 4096 ${SDDEVICE}2
#   ##120705 add '-E stride=2,stripe-width=1024' refer: http://blogofterje.wordpress.com/2012/01/14/optimizing-fs-on-sd-card/
#   #mke2fs -t ext4 -O ^has_journal -L puppy -m 0 -b 4096 -E stride=2,stripe-width=1024 ${SDDEVICE}2
#   #120706 mavrothal reports may not suit many cards, so revert...
#   mke2fs -t ext4 -O ^has_journal -L puppy -m 0 -b 4096 ${SDDEVICE}2
#   sync
#   FIXEXTPTN='s%ext2%ext4%' #see below.
#   SDFS2='ext4'
#   echo "...done"
#  fi
# fi
# 
# mkdir -p /mnt/sdimagep1
# mkdir -p /mnt/sdimagep2
# dd if=${SDDEVICE}1 of=/dev/null bs=1024 count=1 #trying to fix weird bug.
# dd if=${SDDEVICE}2 of=/dev/null bs=1024 count=1 #trying to fix weird bug.
# while [ "a" = "a" ];do
#  mount -t vfat ${SDDEVICE}1 /mnt/sdimagep1
#  MNTSTAT1=$?
#  mount -t ${SDFS2} ${SDDEVICE}2 /mnt/sdimagep2
#  MNTSTAT2=$?
#  if [ $MNTSTAT1 -ne 0 -o $MNTSTAT2 -ne 0 ];then
#   echo
#   echo "Something is wrong. There should be two partitions on the SD card,"
#   echo "vfat and ${SDFS2}. There was an error mounting them. Aborting script."
#   echo "Attempted operations:"
#   echo "mount -t vfat ${SDDEVICE}1 /mnt/sdimagep1    STATUS: ${MNTSTAT1}"
#   echo "mount -t ${SDFS2} ${SDDEVICE}2 /mnt/sdimagep2    STATUS: ${MNTSTAT2}"
#   umount /mnt/sdimagep1 2>/dev/null
#   umount /mnt/sdimagep2 2>/dev/null
#   echo "You might try replugging the card."
#   echo -n "ENTER to quit, any other key to retry: "
#   read weirdbug
#   [ "$weirdbug" != "" ] && continue
#   exit 1
#  fi
#  break
# done
# [ "$FIXEXTPTN" ] && [ -f /mnt/sdimagep1/cmdline.txt ] && sed -i -e "${FIXEXTPTN}" /mnt/sdimagep1/cmdline.txt #120704
# echo
# echo "Copying Linux kernel to SD card..."
# #120613 restore correct kernel image name...
# case $REALKERNAME in
#  uImage)     cp -f build/vmlinuz /mnt/sdimagep1/uImage ;;
#  kernel.img) cp -f build/vmlinuz /mnt/sdimagep1/kernel.img ;;
#  *)          cp -f build/vmlinuz /mnt/sdimagep1/ ;;
# esac
# echo -n "$REALKERNAME" > /mnt/sdimagep1/REALKERNAME #just in case need to know, in a running puppy.
# sync
# echo "...done"
# echo "Copying Puppy filesystem to SD card, please wait..."
# cp -a rootfs-complete/* /mnt/sdimagep2/
# sync
# #120704 add to /etc/fstab...
# echo "/dev/${SDDEVICE}2     /     ${SDFS2}     defaults,noatime   0 1" >> /mnt/sdimagep2/etc/fstab #120707 change relatime to noatime.
# sync
# echo "...done"
# umount /mnt/sdimagep1 2>/dev/null
# umount /mnt/sdimagep2 2>/dev/null
# 
# echo
# echo "Please type the name that you want to give the SD image file, or press ENTER"
# SDBASEBASE="`basename $SDBASE .img | sed -e 's%-201[0-9]*%-%' -e 's%-skeleton%-%' | cut -f 1,2,3 -d '-'`"
# PUPIMG="${SDBASEBASE}-${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}.img"
# echo -n "only for the default [${PUPIMG}]: "
# read PUPIMG1
# [ "$PUPIMG1" != "" ] && PUPIMG="$PUPIMG1"
# echo "...chosen $PUPIMG"
# echo
# echo "Now creating an image file of the SD card, please wait patiently..."
# IMGK=`expr $IMGBYTES \/ 1024` #assume size divisible by 1024!!!!***WARNING***
# dd if=$SDDEVICE of=$PUPIMG bs=1024 count=$IMGK
# sync
# echo "...done. The image file is named '${PUPIMG}'"
# echo
# echo "However, it is ${IMGK}KB, so needs to be compressed for distribution."
# echo "Compressing, please wait..."
# rm -f ${PUPIMG}.xz
# xz $PUPIMG
# sync


 if [ "$WRITE_SD" = "yes" ];then
 #update desktop drive icons. note, this is also done in /usr/sbin/bootflash, puppyinstaller, gparted_shell...
 #/tmp/pup_event_frontend_block_request is used in /sbin/pup_event_frontend_d to refresh drv...
 THEDRIVE="`echo -n "$SDDEVICE" | cut -f 3 -d '/'`"
 echo "$THEDRIVE" > /tmp/pup_event_frontend_block_request

 #120510...
 echo "If the SD card currently plugged in is bigger than the image, for example
you have a 4GB card and used a 1GB image, optionally now you may increase the
${SDFS2} partition to fill the remaining space -- this is for your own use."
 echo -n "ENTER only to decline: "
 read makebig
 if [ "$makebig" != "" ];then
  if [ "`which gparted`" = "" ];then
   echo "ERROR, gparted not installed!!!"
  else
   gparted $SDDEVICE
   sync
   echo
   echo "Checking the ${SDFS2} filesystem..."
   fsck.${SDFS2} -p ${SDDEVICE}2
   sync
   echo "$THEDRIVE" > /tmp/pup_event_frontend_block_request
  fi
 fi
 fi # if WRITE_SD

fi

cd $WKGDIR

#now do the devx...
echo
echo -n "Hit ENTER to build the 'devx' SFS file, any other key to exit: "
read dodevx
[ "$dodevx" != "" ] && exit
echo
echo "Building ${DEVXSFS}..."

#rm -rf sandbox3/devx
#mkdir sandbox3/devx
echo " building sandbox3/devx ..."
rm -f /tmp/3builddistro_removed_alt_dev #101013
ALLGENNAMESD="`echo "$PKGS_SPECS_TABLE" | grep '^yes' | cut -f 2 -d '|' | sed -e 's%$%_DEV%'`"
rm -f ./${WOOF_OUTPUT}/${DEVXSFS} 2>/dev/null #100911
for ONEDEV in `ls -1 packages-${DISTRO_FILE_PREFIX} | grep '_DEV$' | tr '\n' ' '`
do

 #110727 no longer using this...
 ##101013 preferred pkg for an older kernel (see PKGS_MANAGEMENT file)...
 ONEPKG="`echo -n "$ONEDEV" | sed -e 's%_DEV%%'`"
 #for ONE_PPO in $PKG_PREFER_OLDER
 #do
 # one_KERNEL=`echo -n "$ONE_PPO" | cut -f 1 -d '|' | cut -f 1 -d '-' | cut -f 1 -d '_' | cut -f 3 -d '.'` #ex 34 (from 2.6.34)
 # two_PKG="`echo -n "$ONE_PPO" | cut -f 2 -d '|'`"
 # three_PKG="`echo -n "$ONE_PPO" | cut -f 3 -d '|'`"
 # if [ $SUB_KERNELVER -le $one_KERNEL ];then
 #  if [ "$two_PKG" = "$ONEPKG" ];then
 #   #if PKGS_SPECS_TABLE has 'yes' for older pkg, bypass this one...
 #   if [ "`echo "$PKGLIST" | tr ' ' '\n' | grep "^${three_PKG}$"`" != "" ];then
 #    echo "$ONEDEV" >> /tmp/3builddistro_removed_alt_dev
 #    continue 2
 #   fi
 #  fi
 # else
 #  if [ "$three_PKG" = "$ONEPKG" ];then
 #   #if PKGS_SPECS_TABLE has 'yes' for newer pkg, bypass this one...
 #   if [ "`echo "$PKGLIST" | tr ' ' '\n' | grep "^${two_PKG}$"`" != "" ];then
 #    echo "$ONEDEV" >> /tmp/3builddistro_removed_alt_dev
 #    continue 2
 #   fi
 #  fi
 # fi
 #done
 
 devPATTERN='^'"$ONEDEV"
 if [ "`echo "$ALLGENNAMESD" | grep "$devPATTERN"`" != "" ];then
  cp -a --remove-destination packages-${DISTRO_FILE_PREFIX}/${ONEDEV}/* sandbox3/devx/
  sync #110727 running drake pup 0.3 with k2.6.39-3 new laptop, mysterious loss of dirs/files. try sync.
 fi
done
sync

#earlier above i moved all invalid symlinks into the devx module, yeah but i
#think should delete them if they really point nowhere...
echo " Deleting really invalid symlinks in devx..."
mkdir layer_top
LAYERFS="aufs"
[ "`lsmod | grep '^unionfs'`" != "" ] && LAYERFS="unionfs"
if [ "$LAYERFS" = "aufs" ];then
 mount -t aufs -o udba=reval,diropq=w,dirs=sandbox3/devx=rw:sandbox3/rootfs-complete=ro layerfs layer_top
else
 mount -t unionfs -o dirs=sandbox3/devx=rw:sandbox3/rootfs-complete=ro layerfs layer_top
fi
for ONESYMLINK in `find layer_top/ -type l`
do
 [ "`echo -n "$ONESYMLINK" | grep '/dev/'`" != "" ] && continue
 [ "`echo -n "$ONESYMLINK" | grep '/proc/'`" != "" ] && continue
 xONESYMLINK="`echo -n "$ONESYMLINK" | sed -e 's/^layer_top//'`"
 #111123 cross-build, cannot do chroot...
 if [ "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then #111123
  if [ "`chroot layer_top readlink -e $xONESYMLINK`" = "" ];then
   echo " $ONESYMLINK"
   rm -f $ONESYMLINK
  fi
 else
  #do it without chroot...
  LINKTO="`readlink layer_top${xONESYMLINK}`"
  [ -e layer_top${LINKTO} ] && continue #absolute path
  UPONE="`dirname "$xONESYMLINK"`"
  [ -e layer_top${UPONE}/${LINKTO} ] && continue #relative path
  echo " $ONESYMLINK"
  rm -f $ONESYMLINK
 fi
done
sync
umount layer_top
rmdir layer_top

###HACK###
if [ "$(echo "$PKGS_SPECS_TABLE" | grep '^yes|python|' | grep 'exe>dev')" != "" ];then #121022
 [ -e sandbox3/devx/usr/bin/python2.5 ] && ln -s python2.5 sandbox3/devx/usr/bin/python 2>/dev/null
 [ -e sandbox3/devx/usr/bin/python2.6 ] && ln -s python2.6 sandbox3/devx/usr/bin/python 2>/dev/null
 [ -e sandbox3/devx/usr/bin/python2.7 ] && ln -s python2.7 sandbox3/devx/usr/bin/python 2>/dev/null
fi
 rm -f sandbox3/devx/usr/lib${lsuffix}/mozilla/plugins/* #100407 maybe .a, .la files in here.
###END HACK###

#v424 no, i have needed docs in usr/share/doc...
#w003 there's docs here, move them out...
rm -rf packages-${DISTRO_FILE_PREFIX}/devx_DOC 2>/dev/null
mkdir -p packages-${DISTRO_FILE_PREFIX}/devx_DOC/usr/share
#mv sandbox3/devx/usr/share/doc packages-${DISTRO_FILE_PREFIX}/devx_DOC/usr/share/ 2>/dev/null
mv sandbox3/devx/usr/share/doc-base packages-${DISTRO_FILE_PREFIX}/devx_DOC/usr/share/ 2>/dev/null
mv sandbox3/devx/usr/share/man packages-${DISTRO_FILE_PREFIX}/devx_DOC/usr/share/ 2>/dev/null

#100831 hack, this symlink needed...
if [ -d sandbox3/devx/usr/X11R7/include/xorg ];then
 [ ! -e sandbox3/devx/usr/include/xorg ] && ln -s ../X11/include/xorg sandbox3/devx/usr/include/xorg
fi

#100622 remove overhead of this script running every bootup...
mkdir -p sandbox3/devx/etc/profile.d
rm -f sandbox3/devx/etc/profile.d/pkgconfig* 2>/dev/null
PKG_CONFIG_PATH="`find sandbox3/devx/usr/lib${lsuffix} sandbox3/devx/usr/X11R7/lib sandbox3/devx/opt sandbox3/devx/usr/local/lib${lsuffix} -type d -name pkgconfig | sed -e 's%sandbox3/devx%%' | tr '\n' ':'`"
echo "export PKG_CONFIG_PATH='${PKG_CONFIG_PATH}'" > sandbox3/devx/etc/profile.d/pkgconfig

#maybe stray /install dir from slackware pkgs...
rm -rf sandbox3/devx/install 2>/dev/null
[ -f sandbox3/devx/pet.specs ] && rm -f sandbox3/devx/pet.specs #w478
#another slackware hack 131124
[ -x sandbox3/devx/usr/bin/strings-GNU ] && mv sandbox3/devx/usr/bin/strings-GNU sandbox3/devx/usr/bin/strings
sync

#110215 BaCon...
#111203 remove offer to download and compile bacon (no good for cross-builds)...
if [ ! -f sandbox3/devx/usr/bin/bacon ];then
#if [ -f sandbox3/devx/usr/bin/bacon ];then
# BACONPKG="`grep '|bacon|' sandbox3/rootfs-complete/root/.packages/woof-installed-packages | cut -f 1 -d '|'`"
# echo
# echo "The devx has BaCon compiler package: ${BACONPKG}"
# echo "Great, but if you have an Internet connection, you may update it, however this"
# echo "does not always work as a very recent /usr/bin/bacon must be installed..."
#else
 echo
 echo "Oh, you do not have the BaCon compiler in devx."
 echo "It is recommended that variable PKGS_SPECS_TABLE in file DISTRO_PKGS_SPECS-* have this entry:"
 echo "yes|bacon||exe>dev,dev,doc>dev,nls"
 echo "It is recommended that all Puppy distros standardise on having BaCon available."
# echo "Anyway, if you have an Internet connection, BaCon can be downloaded now, however"
# echo "this does not always work as you need a very recent /usr/bin/bacon already installed..."
fi
echo -n "Press ENTER to continue: "
read keepgoing
##101217 get latest BaCon...
#if [ -f support/fetch ];then #see http://www.basic-converter.org/fetch.bac
# if [ "`which bacon`" != "" ];then
#  echo
#  echo "Press any printable key (then ENTER) to download latest BaCon BASIC compiler"
#  echo "and support files. You will need Internet access to do this."
#  echo -n "ENTER key only to decline, any other printable char to download: "
#  read dobacon
#  if [ "$dobacon" != "" ];then
#   mkdir -p sandbox3/devx/usr/share/BaCon
#   rm -f sandbox3/devx/usr/share/BaCon/* 2>/dev/null
#   cp support/fetch sandbox3/devx/usr/share/BaCon/
#   cd sandbox3/devx/usr/share/BaCon
#   ./fetch
#   sync
#   [ ! -f documentation.pdf ] && wget http://www.basic-converter.org/documentation.pdf
#   [ ! -f documentation.html ] && wget http://www.basic-converter.org/documentation.html
#   [ ! -f hug.txt ] && wget http://www.basic-converter.org/hug.txt
#   [ ! -f sqlite3.txt ] && wget http://www.basic-converter.org/sqlite3.txt
#   [ ! -f gmp.txt ] && wget http://www.basic-converter.org/gmp.txt
#   [ ! -f curses.txt ] && wget http://www.basic-converter.org/curses.txt
#   [ ! -f pthreads.txt ] && wget http://www.basic-converter.org/pthreads.txt
#   [ ! -f gd.txt ] && wget http://www.basic-converter.org/gd.txt
#   [ ! -f gtk.txt ] && wget http://www.basic-converter.org/gtk.txt
#   [ ! -f gl.txt ] && wget http://www.basic-converter.org/gl.txt
#   ##fetch has already downloaded bacon.bac, but want beta version...
#   #mv -f bacon.bac bacon-released.bac
#   #wget http://www.basic-converter.org/beta/bacon.bac
#   #[ $? -ne 0 ] && mv -f bacon-released.bac bacon.bac
#   #mv -f documentation.html documentation-released.html
#   #wget http://www.basic-converter.org/beta/documentation.html
#   #[ $? -ne 0 ] && mv -f documentation-released.html documentation.html
#   sync
#   if [ -f bacon.bac ];then
#    bacon bacon.bac
#    sync
#    if [ -f bacon ];then
#     cp -f -a bacon ../../bin/
#     echo "...done"
#    else
#     echo "ERROR: bacon did not compile!"
#    fi
#   fi
#   cd ../../../../../ #back to woof-tree project dir.
#  fi
# else
#  echo
#  echo "WARNING: 'devx' SFS not loaded, cannot install latest BaCon BASIC compiler."
#  echo "         (or current loaded devx SFS does not have BaCon compiler in it)"
# fi
#fi

#110429 see above, think want glibc scsi headers in usr/include/scsi...
if [ -d sandbox3/devx/usr/include/scsi-GLIBC ];then
 #probably have kernel scsi headers in usr/include/scsi, fix...
 cp -a -f sandbox3/devx/usr/include/scsi-GLIBC/* sandbox3/devx/usr/include/scsi/
fi

#110702 mageia1 python puts 30python.csh here...
rm -f sandbox3/devx/etc/profile.d/*.csh

#130111 fixed in 2createpackages...
##110726 mageia does something weird, puts all these in wrong place...
#if [ -d sandbox3/devx/usr/bin/multiarch-i386-linux ];then
# cp -a -f --remove-destination sandbox3/devx/usr/bin/multiarch-i386-linux/* sandbox3/devx/usr/bin/
# rm -rf sandbox3/devx/usr/bin/multiarch-i386-linux
#fi

# 151019 - delete duplicate symlinks
echo "deleting duplicate symlinks"
find sandbox3/devx -type l |
while read ONESYMLINK
do
  # if the symlink exists in sandbox3/rootfs-complete we delete it
 ESYMLINK=$(echo "$ONESYMLINK" | sed 's%devx%rootfs-complete%')
 [ -h "$ESYMLINK" ] && echo -n "deleted $ONESYMLINK " && rm -f "$ONESYMLINK"
 
done

sync


# 151019 - delete whiteouts
echo "cleaning out whiteouts"
find sandbox3/devx -name '.wh*' -delete

echo "Now creating ${DEVXSFS} ..."
./support/${MKSQUASHFS} sandbox3/devx ./${WOOF_OUTPUT}/${DEVXSFS} ${COMPCHOICE} #100911 110713
sync
chmod 644 ./${WOOF_OUTPUT}/${DEVXSFS}
CUR_DIR="$PWD"
cd ./${WOOF_OUTPUT}
echo -n "$IDSTRING" >> ${DEVXSFS} #100911 16-byte id-string appended to file.
md5sum ${DEVXSFS} > ${DEVXSFS}.md5.txt #100911
cd "$CUR_DIR"
sync
echo "...done"


echo
echo "Script finished."
echo -n "Press ENTER key to exit: "
read yeahgetout
###END###

