#!/bin/bash
#(c) copyright Barry Kauler 2006,2007 www.puppylinux.com
#script to create initrd.gz, pup_xxx.sfs and live-CD iso
#from packages.
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#v3.94 dec2007: option for barebones build.
#v3.96/7 jan/feb 2008: updates for dingo.
#v3.98 mar 2008: redesign of firmware handling in zdrv file.
#v3.98 mar 2008: if matching _DEV pkg, copy to devx-extra folder.
#v3.98 mar 2008: a quick hack, /dev/mmc* major 179, needs driver param k2.6.21.7.
#v3.99 apr 2008: prior purging of cifs.ko and xfs.ko removed.
#v4.00 ndiswrapper into pup_xxx.sfs as not found if CD removed, loading xxx.inf.
#v4.01 pickmodules.sh new 2nd param 'zdrvinit' for cutdown selection in initrd.
#v4.02 firmware in /lib/all-firmware now tarballs. now /lib/modules/all-firmware.
#v4.02 /lib/modules/firmware.dep.<kern>,DOTconfig-K<kern> moved to /etc/modules.
#v406 now can select different kernels in same version. (IDE, SCSI, LZMA, etc.)
#v406 kernels/<sub dir> name can now be named <kern version>-<text>
#v411 remove stray unipup scripts.
#v412 add ssb.ko module to initrd (needed by ohci-hcd.ko).

APATTERN='^".\+" ".\+" .\+ ".*" \\'
ERRPKG="`cat packages.txt | grep -v "$APATTERN"`"
if [ ! "$ERRPKG" = "" ];then
 echo
 echo "The following lines in packages.txt are incorrect..."
 echo "$ERRPKG"
 echo
 echo -n "ENTER to continue, any other non-space char to quit: "
 read yabbo
 [ ! "$yabbo" = "" ] && exit
 exit
fi


WKGDIR="`pwd`"
PUPPYDIR="$WKGDIR"
if [ ! -f /etc/cdburnerdevice ];then
 echo "Run 'CD/DVD Drive Wizard' first and choose burner drive!"
 exit
fi
BURNERDRIVE="/dev/`cat /etc/cdburnerdevice`" #set by CD/DVD Wizard.
CDR="$BURNERDRIVE" #not recommended, but works.
PUPPYVERSION="`cat boot/initrd-tree0/PUPPYVERSION`"
export PUPPYVERSION

dependcheck() {
 echo -n "" > /tmp/missinglibs.txt
 echo -n "" > /tmp/notmissinglibs0.txt
 echo -n "" > /tmp/notusedlibs.txt
 FNDFILES="`find $WKGDIR/rootfs-complete -type f | tr "\n" " "`"
 for ONEFILE in $FNDFILES
 do
  ISANEXEC="`file --brief $ONEFILE | grep --extended-regexp "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/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}/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 -type f -name $ONEMISSING >> /tmp/missinglibs.txt
    done
    #echo "" >> /tmp/missinglibs.txt
   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/rootfs-complete -type f | tr "\n" " "`"
 for ONEFILE in $FNDFILES
 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
}

if [ ! -d packages ];then
 echo "The current directory appears to be incorrect."
 echo "Please open a terminal window on the directory that has the packages"
 echo "and kernels subdirectories and createpuppy file (this script)."
 echo "Exiting..."
 exit
fi
echo
echo "WARNING: Current Linux system must be running same kernel as used in Puppy."
echo
echo "This script will create a complete Puppy filesystem, in rootfs-complete"
echo "directory. This may be about 200M. A temporary copy of this is made,"
echo "needing another 200M, plus temp files, so you may need about 500M"
echo "free space in the partition that is running this script."
echo "WARNING: Do NOT use a msdos or vfat partition!!!!!"
echo "Executing df command (available space shown in Kbytes):"
df
echo
echo "...if the current partition is inadequate, type CTRL-C to"
echo -n "   abort this script, otherwise just press ENTER key to continue: "
read mmnn

#v2.16rc... v2.20 now using initramfs...
#echo
#echo "Do you want to build Puppy with a conventional 'initrd', that is, a ramdisk"
#echo "/dev/ram0 to hold the initial filesystem? If so, just press ENTER key."
#echo "Or, a cpio 'initramfs'? This has the advantage that the boot parameters"
#echo "'root=/dev/ram0 [ramdisk_size=<value>]' are not needed, which is an advantage"
#echo "for a large 'initrd.gz'. It also has more efficient usage of RAM."
#echo "Note, for now it is recommended that you choose the 'initrd', unless you have"
#echo "a special reason for wanting the cpio initramfs." 
#echo -n "Just hit ENTER (recommended) or any char then ENTER: "
#read INITTYPE
#if [ "$INITTYPE" = "" ];then
# INITTYPE="initrd"
# ln -sf makeext2initrd boot/makeinitrdgz.sh
#else
 INITTYPE="initramfs"
 ln -sf makecpioinitrd boot/makeinitrdgz.sh
#fi

rm -f /tmp/fbvideomode.txt
echo
echo "Wait..."

#v3.94 pre-process packages.txt for 'barebones' build...
if [ -f pkgs-barebones.txt ];then
 echo
 echo "Barebones is defined in pkgs-barebones.txt, edit before running createpuppy."
 echo "If choose barebones, the orig packages.txt is saved as /tmp/ORIG-packages.txt"
 echo "and restored to packages.txt at end of this script (so do not abort script)."
 echo -n "Do you want a 'standard' puppy, or 'barebones' (ENTER for standard): "
 read FBAREBONES
 if [ "$FBAREBONES" != "" ];then
  mv -f packages.txt /tmp/ORIG-packages.txt
  cat /tmp/ORIG-packages.txt | sed -e 's/\\$/\\\\/' |
  while read ONELINE
  do
   ONENAMEONLY="`echo -n "$ONELINE" | cut -f 4 -d '"' | cut -f 1 -d ' '`" #'geany
   obPATTERN='^-'"$ONENAMEONLY"'$'
   nbPATTERN='^+'"$ONENAMEONLY"'$'
   if [ "`cat pkgs-barebones.txt | grep "$obPATTERN"`" != "" ];then
    echo "$ONELINE" | sed -e 's/" on "/" off "/' >> packages.txt
   else
    if [ "`cat pkgs-barebones.txt | grep "$nbPATTERN"`" != "" ];then
     echo "$ONELINE" | sed -e 's/" off "/" on "/' >> packages.txt
    else
     echo "$ONELINE" >> packages.txt
    fi
   fi
  done
 fi
fi


if [ ! -f packages.txt ];then
 touch packages.txt
fi
KNOWNPKGS="`cat packages.txt`"


#script has to scan packages/ folder and build a list of all available pkgs:

echo "Creating /tmp/allpkgs.txt, a list of all packages in packages directory."
ALLPKGS="`ls -1 packages/`"
echo "$ALLPKGS" > /tmp/allpkgs.txt
ALLON1LINE="`echo "$ALLPKGS" | tr "\n" " "`"

echo "Correlating packages.txt and allpkgs.txt, creating /tmp/packagesfnd.txt."
echo -n "" > /tmp/packagesfnd.txt
for ONEPKG in $ALLON1LINE
do
 QONEPKGQ="\"${ONEPKG}\""
 FNDPKG="`echo "$KNOWNPKGS" | grep "$QONEPKGQ"`"
 if [ ! "$FNDPKG" = "" ];then
  echo "$FNDPKG" >> /tmp/packagesfnd.txt
 else
  #this is a new package...
  echo -e "\"${ONEPKG}\" \"${ONEPKG}: \" off \"\" \\"  >> /tmp/packagesfnd.txt
 fi
done
sync
PACKAGESFND="`cat /tmp/packagesfnd.txt`"

echo "Any packages in packages.txt not in allpkgs.txt are marked unavailable."
#delete any lines in packages.txt that are not in packages/ folder.
#...maybe change status field to "unavailable"...
sort -u packages.txt > /tmp/packages.txt
sort -u /tmp/packagesfnd.txt > /tmp/packagesfnd2.txt
DIFFLINES="`diff --text --suppress-common-lines /tmp/packagesfnd2.txt /tmp/packages.txt | grep '>' | sed -e 's/> //g'`"
sync
#now make the difflines unavailable...
#grep '"' is to try remove blank lines...
DIFFLINES="`echo "$DIFFLINES" | grep '"' | sed -e 's/\" on \"/\" unavailable \"/g' | sed -e 's/\" off \"/\" unavailable \"/g' `" #'geany fix
sync
#now append difflines...
echo "$DIFFLINES" > /tmp/difflines.txt
echo "$DIFFLINES" >> /tmp/packagesfnd.txt
sync
 #there is a problem, getting duplicate lines sometimes. a hack to fix...
 sort -u /tmp/packagesfnd.txt > /tmp/tempfile1.txt
 sync
 #mv /tmp/tempfile1.txt /tmp/packagesfnd.txt
 #v1.0.1 another problem, getting a blank first line. quick hack...
 #Darn, messed around here for sometime... get an error msg "cat: write: broken pipe"
 #so, suppressing it...
 FIRSTLINE="`cat /tmp/tempfile1.txt 2> /dev/null | head -n 1 | tr "\n" " "`"
 if [ "$FIRSTLINE" = " " ];then
  NUMLINES=`wc -l /tmp/tempfile1.txt | tr -s " " | cut -f 2 -d " "`
  NUMLINES=`expr $NUMLINES - 1`
  cat /tmp/tempfile1.txt | tail -n $NUMLINES > /tmp/packagesfnd.txt
 else
  mv /tmp/tempfile1.txt /tmp/packagesfnd.txt
 fi
 sync
PACKAGESFND="`cat /tmp/packagesfnd.txt`"
cp -f /tmp/packagesfnd.txt /tmp/packagesfndx.txt


while [ 1 ];do #BIG WHILE LOOP

echo "Starting package selection GUI. Must exit GUI before this script will continue."
#ok put up a gui...
rm -f /tmp/rettags.txt
echo '#!/bin/sh' > /tmp/pkgdialog
echo 'RETTAGS="`Xdialog --title "Choose packages for Puppy" --left --stdout --separator " "  --item-help --icon "pkggroups2.xpm" --no-cancel --buildlist "Add or remove packages that will be in Puppy.\nNote, if you want to remove any individual CORE packages, it may be okay, and\nthis script will perform a complete dependency check later.\nExample: any package in MMGTK1APPS group will need ALL PACKAGES TO BE\nINSTALLED from CONSCORE, XLIBCORE, GTK1CORE and MMCORE groups.\n\nPressing OK button will not make changes immediately but will first perform\nsome more dependency checks." 780x540+0+0 0 \' >> /tmp/pkgdialog
echo "$PACKAGESFND" >> /tmp/pkgdialog #'"geany
echo ' `"' >> /tmp/pkgdialog
echo 'if [ ! $? -eq 0 ];then' >> /tmp/pkgdialog
echo ' exit'  >> /tmp/pkgdialog
echo 'fi' >> /tmp/pkgdialog
echo 'echo "$RETTAGS" > /tmp/rettags.txt' >> /tmp/pkgdialog #'geany

sync
chmod 755 /tmp/pkgdialog
/tmp/pkgdialog

if [ ! -f /tmp/rettags.txt ];then
 echo "Something wrong, /tmp/rettags.txt does not exist. Quiting script."
 exit
fi

echo "/tmp/rettags.txt has all the tags (package names) chosen to be in Puppy."
#status field to "on", otherwise "off"...
RETTAGS="`cat /tmp/rettags.txt`"
echo -n "" > /tmp/packagesfnd.txt
for ONEPKG in $RETTAGS
do
 QONEPKGQ="\"$ONEPKG\""
 FNDPKG="`echo "$PACKAGESFND" | grep "$QONEPKGQ"`"
 if [ ! "$FNDPKG" = "" ];then
  #make sure it is on...
  echo "$FNDPKG" | sed -e 's/\" off \"/\" on \"/g' >> /tmp/packagesfnd.txt
 else
  #make sure it is off...
  echo "$FNDPKG" | sed -e 's/\" on \"/\" off \"/g' >> /tmp/packagesfnd.txt
 fi
done
sync
sort -u /tmp/packagesfnd.txt > /tmp/packagesfnd2.txt
sync

#need to add back in all the off and unavailable packages...
echo "Note, /tmp/notchosenpkgs.txt has a list of the not-chosen packages."
sort -u /tmp/packagesfndx.txt > /tmp/packagesfndx2.txt
DIFFLINES="`diff --text --suppress-common-lines /tmp/packagesfnd2.txt /tmp/packagesfndx2.txt | grep '>' | sed -e 's/> //g'`"
#now make the difflines off if any are on...
DIFFLINES="`echo "$DIFFLINES" |  grep '"' | sed -e 's/\" on \"/\" off \"/g'`"
echo "$DIFFLINES" > /tmp/notchosenpkgs.txt
#echo "$DIFFLINES" >> /tmp/packagesfnd2.txt
 #...no, pkgs may have got moved from off to on in dialog window...
 DIFFNAMES="`echo "$DIFFLINES" | tr '"' ' ' | cut -f 2 -d " " | tr "\n" " "`"
 for ONEDIFF in $DIFFNAMES
 do
  QONEDIFFQ="\"${ONEDIFF}\""
  if [ "`cat /tmp/packagesfnd2.txt | grep "$QONEDIFFQ"`" = "" ];then
   echo "$DIFFLINES" | grep "$ONEDIFF" >> /tmp/packagesfnd2.txt
  fi
 done
rm -f /tmp/packagesfnd.tst
sync
sort -u /tmp/packagesfnd2.txt > /tmp/packagesfnd.txt
PACKAGESFND="`cat /tmp/packagesfnd.txt`"


#now need to check dependencies, make sure all CORE groups are there...
goto1=""
echo
echo "Ok, now checking broad dependency groups..."
echo "Note that these groups are fairly coarse and you may want to override."
echo "For example, printer and scanner support are both in MMCORE group, but"
echo "you may want one not the other. So, do not be too bothered about breaking"
echo "up a CORE group, as this script does a comprehensive dependency check"
echo "further down."
#get all the chosen APPS, then find the CORE groups...
NEEDEDCOREGRPS="CONSCORE"
echo "$PACKAGESFND" | grep "CONSCORE" | grep '" off "' > /tmp/missingcorepkgs0.txt
YESAPPS="`echo "$PACKAGESFND" | grep '" on "' | grep 'APPS'`"
if [ ! "`echo "$YESAPPS" | grep 'XLIBAPPS'`" = "" ];then
 NEEDEDCOREGRPS="$NEEDEDCOREGRPS|XLIBCORE"
 echo "$PACKAGESFND" | grep "XLIBCORE" | grep '" off "' >> /tmp/missingcorepkgs0.txt
fi
if [ ! "`echo "$YESAPPS" | grep 'GTK1APPS'`" = "" ];then
 NEEDEDCOREGRPS="$NEEDEDCOREGRPS|XLIBCORE|GTK1CORE"
 echo "$PACKAGESFND" | grep --extended-regexp "XLIBCORE|GTK1CORE" | grep '" off "' >> /tmp/missingcorepkgs0.txt
fi
if [ ! "`echo "$YESAPPS" | grep 'GTK2APPS'`" = "" ];then
 NEEDEDCOREGRPS="$NEEDEDCOREGRPS|XLIBCORE|GTK2CORE"
 echo "$PACKAGESFND" | grep --extended-regexp "XLIBCORE|GTK2CORE" | grep '" off "' >> /tmp/missingcorepkgs0.txt
fi
if [ ! "`echo "$YESAPPS" | grep 'TCLAPPS'`" = "" ];then
 NEEDEDCOREGRPS="$NEEDEDCOREGRPS|XLIBCORE|TCLCORE"
 echo "$PACKAGESFND" | grep --extended-regexp "XLIBCORE|TCLCORE" | grep '" off "' >> /tmp/missingcorepkgs0.txt
fi
if [ ! "`echo "$YESAPPS" | grep 'QT3APPS'`" = "" ];then
 NEEDEDCOREGRPS="$NEEDEDCOREGRPS|XLIBCORE|QT3CORE"
 echo "$PACKAGESFND" | grep --extended-regexp "XLIBCORE|QT3CORE" | grep '" off "' >> /tmp/missingcorepkgs0.txt
fi
if [ ! "`echo "$YESAPPS" | grep '"MM'`" = "" ];then #'geany
 NEEDEDCOREGRPS="$NEEDEDCOREGRPS|MMCORE"
 echo "$PACKAGESFND" | grep "MMCORE" | grep '" off "' >> /tmp/missingcorepkgs0.txt
fi
sort -u /tmp/missingcorepkgs0.txt > /tmp/missingcorepkgs.txt
MISSINGCOREPKGS="`cat /tmp/missingcorepkgs.txt`"
if [ ! "$MISSINGCOREPKGS" = "" ];then
 echo
 echo "The following CORE packages have not been chosen, but may be needed:"
 echo "$MISSINGCOREPKGS"
 echo
 echo -e "Press \"c\" key then  ENTER key to go back to package-selection dialog"
 echo -ne " window, or if you really want to leave these out, just press ENTER: "
 read goto1
fi
if [ "$goto1" = "c" ];then
 continue #BIG WHILE LOOP.
fi

 echo -n 'echo "$PACKAGESFND"' > /tmp/evalgrps
 echo -ne " | grep '\" on \"' | grep 'CORE' | grep --invert-match --extended-regexp  \"" >> /tmp/evalgrps #"Geany
 echo -n "$NEEDEDCOREGRPS" >> /tmp/evalgrps
 echo '"'  >> /tmp/evalgrps
 eval "`cat /tmp/evalgrps`" > /tmp/unusedcorepkgs.txt
 UNUSEDCOREPKGS="`cat /tmp/unusedcorepkgs.txt`"
 if [ ! "$UNUSEDCOREPKGS" = "" ];then
  echo
  echo "The following CORE packages have been chosen, but are probably not needed:"
  echo "$UNUSEDCOREPKGS"
  echo
  echo -e "Press \"c\" key then  ENTER key to go back to package-selection dialog"
  echo -ne " window, or if you really want to leave these in, just press ENTER: "
  read goto1
 fi
if [ "$goto1" = "c" ];then
 continue #BIG WHILE LOOP.
fi
echo "...done."

cd $WKGDIR #v1.0.6 precaution.


echo
echo "These kernels are available:"
ls -1 kernels/
USEKERNEL="`uname -r`"
DIRKERNEL="$USEKERNEL" #v406
DEFAULTKERNEL="$USEKERNEL"
echo "Please type in which one of these kernels you want to use. "
echo "Note, puppy is currently running $USEKERNEL, and if you choose a"
echo "different kernel the build will be slightly deficient -- you need"
echo "to boot from it and then run createpuppy again so that the default"
echo "kernel is the one you want -- this second live-cd will be ok."
echo -n "If you just press ENTER key, the default is $USEKERNEL: "
read mmaadd
if [ ! "$mmaadd" = "" ];then
 #USEKERNEL="$mmaadd"
 USEKERNEL="`echo -n $mmaadd | cut -f 1 -d '-'`" #v406
 DIRKERNEL="$mmaadd" #v406
fi
echo "...okay, using $DIRKERNEL"

echo
echo "Kernel version $DIRKERNEL has these actual types to choose from:"
ls -1 kernels/${DIRKERNEL}/vmlinuz* | rev | cut -f 1 -d '/' | rev
NAMEKERNEL="`ls -1 kernels/${DIRKERNEL}/vmlinuz* | head -n 1 | rev | cut -f 1 -d '/' | rev`" #v406
echo "Please type in which one of these kernels you want to use. "
echo "If in doubt, just press ENTER key only."
echo -n "If you just press ENTER key, the default is $NAMEKERNEL: "
read mmaadd
[ "$mmaadd" != "" ] && NAMEKERNEL="$mmaadd"
echo "...okay, using $NAMEKERNEL"


echo
echo "Now building rootfs-complete, a directory with the complete Puppy filesystem..."
echo "Updating packages.txt from /tmp/packagesfnd.txt, renamed old packages.txt."
#now build rootfs-complete, final Puppy root filesystem...
echo '#!/bin/sh' > pinstall.sh
#echo 'if [ ! -d root0 ];then' >> pinstall.sh
#echo ' echo "Current directory is wrong, cannot execute pinstall.sh."' >> pinstall.sh
#echo ' exit' >> pinstall.sh
#echo 'fi' >> pinstall.sh
mv -f packages.txt packages-PREVIOUS.txt
cp -f /tmp/packagesfnd.txt packages.txt
rm -rf rootfs-complete
sync
mkdir rootfs-complete
echo -n "" > /tmp/keywords.txt
PKGLIST="`cat /tmp/packagesfnd.txt | grep '" on "'| tr '"' ' ' | cut -f 2 -d " " | tr "\n" " "`"  #'geany fix
echo "$PKGLIST" > /tmp/chosenpkgs.txt
sync

rm -f /tmp/bypassmenurebuild
cp -f /tmp/chosenpkgs.txt chosenpkgs.txt.bak
rm -f /tmp/replacementlist

rm -rf devx-extra #v3.98
mkdir devx-extra  #v3.98

for ONEPKG in $PKGLIST
do

  SUBSTPKG=""
  VVPATTERN='^"'"$ONEPKG"'" '
  PKGNAMEONLY="`grep "$VVPATTERN" packages.txt | cut -f 4 -d '"' | cut -f 1 -d ' '`" #'geany
  #precaution...
  [ "`echo -n "$PKGNAMEONLY" | grep ':'`" != "" ] && PKGNAMEONLY="`echo -n "$PKGNAMEONLY" | cut -f 1 -d '-'`"
  [ -d packages-k${USEKERNEL} ] && SUBSTPKG="`ls -1 packages-k${USEKERNEL} | grep "$PKGNAMEONLY"`"
  
  #v3.98
  PKGVERSIONONLY="`grep "$VVPATTERN" packages.txt | cut -f 4 -d '"' | grep ':' | cut -f 1 -d ':' | cut -f 2 -d ' '`" #'geany
  [ "$PKGVERSIONONLY" = "" ] && PKGVERSIONONLY="`echo -n "$ONEPKG" | cut -f 2 -d '-'`"
  
  
  if [ "$SUBSTPKG" = "" ];then
   # -f may not be enough, as if destination exists and it is a link, then cp will follow the link,
   #overwrite final target. --remove-destination just deletes any dest file/link then writes...
   
   if [ "`echo -n "$ONEPKG" | grep '_DEV'`" = "" ];then #v3.98
    cp -a --remove-destination packages/$ONEPKG/* rootfs-complete/ 2> /dev/null
   else #v3.98 divert if a _DEV pkg...
    cp -a --remove-destination packages/$ONEPKG/* devx-extra/ 2> /dev/null
   fi
   
   #v3.98 see if there is a matching _DEV pkg...
   [ -d packages/${PKGNAMEONLY}_DEV-${PKGVERSIONONLY} ] && cp -a --remove-destination packages/${PKGNAMEONLY}_DEV-${PKGVERSIONONLY}/* devx-extra/
   
  else
   cp -a --remove-destination packages-k${USEKERNEL}/${SUBSTPKG}/* rootfs-complete/ 2> /dev/null
   echo "$PKGNAMEONLY $ONEPKG $SUBSTPKG" >> /tmp/replacementlist
  fi
  sync

  if [ -f rootfs-complete/pinstall.sh ];then
   #note, do not filter #! /bin/sh (with a space)...
   #cat rootfs-complete/pinstall.sh | grep -v '#!/bin/sh' >> pinstall.sh
   #v1.0.6 the string may occur indented, allow thru...
   cat rootfs-complete/pinstall.sh | grep  --extended-regexp -v '^\#\!\/bin\/sh' >> pinstall.sh
   sync
   rm -f rootfs-complete/pinstall.sh
  fi

  #v2.15 get rid of a .specs file...
  rm -f rootfs-complete/*.specs
  
  #window and menu icons can also be in the top-level directory of the package...
  mv rootfs-complete/*24.xpm rootfs-complete/usr/local/lib/X11/pixmaps/ 2>/dev/null
  mv rootfs-complete/*32.xpm rootfs-complete/usr/local/lib/X11/pixmaps/ 2>/dev/null
  mv rootfs-complete/*32.png rootfs-complete/usr/local/lib/X11/pixmaps/ 2>/dev/null
  mv rootfs-complete/*48.xpm rootfs-complete/usr/local/lib/X11/pixmaps/ 2>/dev/null
  mv rootfs-complete/*48.png rootfs-complete/usr/local/lib/X11/pixmaps/ 2>/dev/null
  mv rootfs-complete/*.xpm rootfs-complete/usr/local/lib/X11/mini-icons/ 2>/dev/null
  #build list of keywords...
  if [ -f rootfs-complete/keyword ];then
   echo -n " " >> /tmp/keywords.txt
   cat rootfs-complete/keyword  | tr "\n" " " >> /tmp/keywords.txt
   rm rootfs-complete/keyword
  fi
  sync
done
echo 'echo "END OF pinstall.sh SCRIPT."' >> pinstall.sh
#echo 'read pinstfin' >> pinstall.sh
chmod 755 pinstall.sh


#echo
#echo "Remove a few more unnecessary framebuffer and tv/tuner-media kernel modules?...."
#echo -n "(to save space). Press ENTER only for yes (to remove): "
#read nooss
#if [ "$nooss" = "" ];then
# echo "Removing some kernel modules from rootfs-complete..."
# #rm -rf rootfs-complete/lib/modules/$USEKERNEL/block
# rm -f rootfs-complete/lib/modules/$USEKERNEL/ide/ide-tape.o
## rm -f rootfs-complete/lib/modules/$USEKERNEL/ide/ide-floppy.o  NO, ide zip drive needs this.
# cp -af rootfs-complete/lib/modules/$USEKERNEL/media/video/videodev.o /tmp/
# rm -rf rootfs-complete/lib/modules/$USEKERNEL/media
# mkdir -p rootfs-complete/lib/modules/$USEKERNEL/media/video
# cp -a /tmp/videodev.o rootfs-complete/lib/modules/$USEKERNEL/media/video/
# rm -rf rootfs-complete/lib/modules/$USEKERNEL/video
# sync
#fi

#echo "Building modules dependency files, using depmod..."
##v2.12 use System.map, so can run depmod on o.s. running different kernel...
##depmod -b $WKGDIR/rootfs-complete
#depmod -b $WKGDIR/rootfs-complete -F $WKGDIR/kernels/$DIRKERNEL/System.map $USEKERNEL
#sync
#if [ -f ./kernels/$DIRKERNEL/modules/pinstall.sh ];then
# echo "Executing modules post-install script..."
# ./kernels/$DIRKERNEL/modules/pinstall.sh
# sync
#fi

#v1.0.4
echo
echo "Generating vertical Puppy menu logo for Fvwm in"
echo "rootfs-complete/usr/local/lib/X11/pixmaps/fvwm-menu.xpm.."
#v1.0.4 copied this down...
DIGIT1="`echo "$PUPPYVERSION" | cut -b 1`"
DIGIT2="`echo "$PUPPYVERSION" | cut -b 2`"
DIGIT3="`echo "$PUPPYVERSION" | cut -b 3`"
RIGHTVER="${DIGIT1}.${DIGIT2}.${DIGIT3}"
[ "$DIGIT3" = "0" ] && RIGHTVER="${DIGIT1}.${DIGIT2}"

cat ./splash-images/blank130x24.ppm | ./splash-images/ppmlabel -background transparent -color red -size 22 -y 15 -text "puppy" -background transparent -color red -size 22 -x 1 -y 15 -text "puppy" -background transparent -color red -size 22 -x 2 -y 15 -text "puppy" -background transparent -color orange -size 8 -x 100 -y 15 -text "$RIGHTVER" | pnmrotate -noantialias 90 | ppmtoxpm | sed -e 's/#000000/None/g' > ./rootfs-complete/usr/local/lib/X11/pixmaps/fvwm-menu.xpm
sync

if [ -f rootfs-complete/usr/share/backgrounds/pupdesk1024x768.gif ];then
 echo "Generating text on"
 echo "rootfs-complete/usr/share/backgrounds/pupdesk1024x768.gif..."
 cat rootfs-complete/usr/share/backgrounds/pupdesk1024x768.gif | giftopnm | ppmlabel -background transparent -color red -size 22 -x 430 -y 570 -text "puppy" -background transparent -color red -size 22 -x 431 -y 570 -text "puppy" -background transparent -color red -size 22 -x 432 -y 570 -text "puppy" -background transparent -color red -size 22 -x 430 -y 571 -text "puppy" -background transparent -color "dark orange" -size 10 -x 532 -y 570 -text "$RIGHTVER" -background transparent -color "dark orange" -size 10 -x 533 -y 570 -text "$RIGHTVER" -background transparent -color "dark orange" -size 10 -x 532 -y 571 -text "$RIGHTVER" | pnmsmooth | ppmquant 256 | ppmtogif > /tmp/newfile.gif
 #cat /usr/share/backgrounds/pupdesk1024x768grey.gif | giftopnm | ppmlabel -background transparent -color red -size 22 -x 430 -y 570 -text "puppy" -background transparent -color red -size 22 -x 431 -y 570 -text "puppy" -background transparent -color red -size 22 -x 432 -y 570 -text "puppy" -background transparent -color red -size 22 -x 430 -y 571 -text "puppy" -background transparent -color "dark orange" -size 10 -x 532 -y 570 -text "$RIGHTVER" -background transparent -color "dark orange" -size 10 -x 533 -y 570 -text "$RIGHTVER" -background transparent -color "dark orange" -size 10 -x 532 -y 571 -text "$RIGHTVER" | pnmsmooth | ppmquant 256 | pnmtojpeg --quality 90 > newfile.jpg
 sync
 cp -f /tmp/newfile.gif rootfs-complete/usr/share/backgrounds/pupdesk1024x768.gif
 sync
fi

if [ -f rootfs-complete/usr/share/backgrounds/paw0desk1024x768.jpg ];then
 Y1=585
 Y2=586
 echo "Generating text on"
 echo "rootfs-complete/usr/share/backgrounds/pawdesk1024x768.jpg..."
cat rootfs-complete/usr/share/backgrounds/paw0desk1024x768.jpg | jpegtopnm | ppmlabel -background transparent -color red -size 22 -x 443 -y $Y1 -text "puppy" -background transparent -color red -size 22 -x 444 -y $Y1 -text "puppy" -background transparent -color red -size 22 -x 445 -y $Y1 -text "puppy" -background transparent -color red -size 22 -x 443 -y $Y2 -text "puppy" -background transparent -color "red" -size 10 -x 545 -y $Y1 -text "$RIGHTVER" -background transparent -color "red" -size 10 -x 546 -y $Y1 -text "$RIGHTVER" -background transparent -color "red" -size 10 -x 545 -y $Y2 -text "$RIGHTVER" | pnmsmooth | pnmtojpeg --quality 80 > /tmp/newfile.jpg
 sync
 cp -f /tmp/newfile.jpg rootfs-complete/usr/share/backgrounds/pawdesk1024x768.jpg
 sync
fi

if [ -f rootfs-complete/usr/share/backgrounds/pawdesk1024x768.jpg ];then
 Y1=595
 Y2=596
 echo "Generating text on"
 echo "rootfs-complete/usr/share/backgrounds/pawdesk1024x768.jpg..."
cat rootfs-complete/usr/share/backgrounds/pawdesk1024x768.jpg | jpegtopnm | ppmlabel -background transparent -color red -size 22 -x 443 -y $Y1 -text "puppy" -background transparent -color red -size 22 -x 444 -y $Y1 -text "puppy" -background transparent -color red -size 22 -x 445 -y $Y1 -text "puppy" -background transparent -color red -size 22 -x 443 -y $Y2 -text "puppy" -background transparent -color "red" -size 10 -x 545 -y $Y1 -text "$RIGHTVER" -background transparent -color "red" -size 10 -x 546 -y $Y1 -text "$RIGHTVER" -background transparent -color "red" -size 10 -x 545 -y $Y2 -text "$RIGHTVER" | pnmsmooth | pnmtojpeg --quality 80 > /tmp/newfile.jpg
 sync
 cp -f /tmp/newfile.jpg rootfs-complete/usr/share/backgrounds/pawdesk1024x768.jpg
 sync
fi
if [ -f rootfs-complete/usr/share/backgrounds/sky1.jpg ];then
 Y1=535 #585
 Y2=536 #586
 echo "Generating text on"
 echo "rootfs-complete/usr/share/backgrounds/sky1.jpg..."
cat rootfs-complete/usr/share/backgrounds/sky1.jpg | jpegtopnm | ppmlabel -background transparent -color red -size 22 -x 443 -y $Y1 -text "puppy" -background transparent -color red -size 22 -x 444 -y $Y1 -text "puppy" -background transparent -color red -size 22 -x 445 -y $Y1 -text "puppy" -background transparent -color red -size 22 -x 443 -y $Y2 -text "puppy" -background transparent -color "red" -size 10 -x 545 -y $Y1 -text "$RIGHTVER" -background transparent -color "red" -size 10 -x 546 -y $Y1 -text "$RIGHTVER" -background transparent -color "red" -size 10 -x 545 -y $Y2 -text "$RIGHTVER" | pnmsmooth | pnmtojpeg --quality 80 > /tmp/newfile.jpg
 sync
 cp -f /tmp/newfile.jpg rootfs-complete/usr/share/backgrounds/sky1.jpg
 sync
fi
#if [ -f rootfs-complete/usr/share/backgrounds/pawrings3.jpg ];then
# Y1=715 #595
# Y2=716 #596
# echo "Generating text on"
# echo "rootfs-complete/usr/share/backgrounds/pawrings3.jpg..."
#cat rootfs-complete/usr/share/backgrounds/pawrings3.jpg | jpegtopnm | ppmlabel -background transparent -color red -size 22 -x 443 -y $Y1 -text "puppy" -background transparent -color red -size 22 -x 444 -y $Y1 -text "puppy" -background transparent -color red -size 22 -x 445 -y $Y1 -text "puppy" -background transparent -color red -size 22 -x 443 -y $Y2 -text "puppy" -background transparent -color "red" -size 10 -x 545 -y $Y1 -text "$RIGHTVER" -background transparent -color "red" -size 10 -x 546 -y $Y1 -text "$RIGHTVER" -background transparent -color "red" -size 10 -x 545 -y $Y2 -text "$RIGHTVER" | pnmsmooth | pnmtojpeg --quality 80 > /tmp/newfile.jpg
# sync
# cp -f /tmp/newfile.jpg rootfs-complete/usr/share/backgrounds/pawrings3.jpg
# sync
#fi

echo
echo "GOOD, the complete root filesystem is now built, in rootfs-complete."


echo
echo "This script can now optionally do a thorough dependency check. "
echo "This is currently experimental so not the default. It also"
echo "takes awhile to complete."
echo -e "Press \"c\" key then ENTER to do thorough check,"
echo -ne "else just press ENTER key to bypass check: "
read mmnn
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 go back to package-selection dialog"
  echo -ne "window, or if you really want to ignore problem, just press ENTER: "
  read goto1
 fi
 if [ "$goto1" = "c" ];then
  continue #BIG WHILE LOOP.
 fi
 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 go back to package-selection dialog"
  echo -ne "window, or if you really want to ignore problem, just press ENTER: "
  read goto1
 fi
 if [ "$goto1" = "c" ];then
  continue #BIG WHILE LOOP.
 fi
fi
echo "...done."

break
done #END BIG WHILE LOOP


echo
echo "Each package can have file pinstall.sh, which is a post-install script, however"
echo "these have not yet been executed. Instead, they are appended to create one big"
echo "pinstall.sh script, which will now be executed."
echo "This script can do stuff like configure a particular web browser as the default,"
echo "specify a taskbar icon for an application, or create a menu entry."
echo
echo -n "Press ENTER key to continue: "
read mmnn
echo
echo "Executing pinstall.sh..."
cd $WKGDIR/rootfs-complete
SAVEWKGDIR="$WKGDIR"
$WKGDIR/pinstall.sh
#echo "...done."
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"

#v3.98 hack to fix k2.6.21.7, /dev/mmc major 179 param explicitly required when
#load driver. K2.6.24 has standardised on 179 and the driver-param not used.
KERNMAJOR=`echo -n "$USEKERNEL" | cut -f 3 -d '.'`
if [ $KERNMAJOR -lt 24 ];then
 cat ./etc/modprobe.conf | sed -e 's%mmc_block ;%mmc_block major=179 ;%g' > /tmp/modprobe.conf.mmc
 sync
 cp -f /tmp/modprobe.conf.mmc ./etc/modprobe.conf
fi

#v1.0.1
#this is for the new pupget package manager...
echo
echo "Copying packages.txt to rootfs-complete/root/.packages/ ..."
#cp ../packages.txt ./root/.packages/
#v2.0.0 note, no longer require /root/.packages/unleashedpackages.txt.
#v2.15 screen out '_DEV' packages...
if [ -f /tmp/replacementlist ];then #diff kernel version,some pkgs changed.

 cp ../packages.txt /tmp/packages.txt.reps
 
 #v3.00 need to fix...
 #cat /tmp/replacementlistfinal |
 #while read ONEREP
 #do
 # ORIGPKG="`echo -n "$ONEREP" | cut -f 1 -d ' '`"
 # REPPKG="`echo -n "$ONEREP" | cut -f 2 -d ' '`"
 # #APATTERN="s/${ORIGPKG}/${REPPKG}/"
 # #cat /tmp/packages.txt.reps | sed -e "$APATTERN" > /tmp/packages.txt.reps.1
 # #sync
 # #mv -f /tmp/packages.txt.reps.1 /tmp/packages.txt.reps
 # OPATTERN='^"'"$ORIGPKG"'"'
 # grep "$OPATTERN" /tmp/packages.txt.reps | sed -e 's/" on "/" off "/' > /tmp/packages.txt.reps.1
 # sync
 # RPATTERN='^"'"$REPPKG"'"'
 # grep "$RPATTERN" /tmp/packages.txt.reps.1 | sed -e 's/" off "/" on "/' > /tmp/packages.txt.reps
 # sync  
 #done
 
 cat /tmp/packages.txt.reps | grep -v '_DEV' > ./root/.packages/packages.txt
else
 cat ../packages.txt | grep -v '_DEV' > ./root/.packages/packages.txt
fi
sync

#v2.14 have a new fixmenus script...
echo
echo "Constructing configuration files for JWM, Fvwm95, IceWM..."
chroot ${WKGDIR}/rootfs-complete /usr/sbin/fixmenus
sync
echo -n "...done. Press ENTER to continue: "
read isitbad

#v2.14 generate help index...
chroot ${WKGDIR}/rootfs-complete /usr/sbin/indexgen.sh


cd $WKGDIR

#v3.02...
echo "/lib" > rootfs-complete/etc/ld.so.conf
echo "/usr/lib" >> rootfs-complete/etc/ld.so.conf
echo "/usr/X11R7/lib" >> rootfs-complete/etc/ld.so.conf
[ -d /opt/gnome2/lib ] && echo "/opt/gnome2/lib" >> rootfs-complete/etc/ld.so.conf
[ -d /opt/qt4/lib ] && echo "/opt/qt4/lib" >> rootfs-complete/etc/ld.so.conf
[ -d /opt/mozilla.org/lib ] && echo "/opt/mozilla.org/lib" >> rootfs-complete/etc/ld.so.conf
[ -d /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...
cp -a ldconfig rootfs-complete/
chroot rootfs-complete ./ldconfig
rm -f rootfs-complete/ldconfig

#v3.02...
if [ ! -e /usr/bin/mozilla ];then
 if [ -d rootfs-complete/opt/mozilla.org/lib ];then
  MOZHOMEDIR="`ls -1 rootfs-complete/opt/mozilla.org/lib | grep -E 'seamonkey|firefox' | head -n 1`"
  ln -s $MOZHOMEDIR rootfs-complete/opt/mozilla.org/lib/mozilla
  MOZNAMEONLY="`echo "$MOZHOMEDIR" | cut -f 1 -d '-'`"
  [ -e rootfs-complete/opt/mozilla.org/bin/$MOZNAMEONLY ] && ln -s $MOZNAMEONLY rootfs-complete/opt/mozilla.org/bin/mozilla
 fi
fi

echo
echo "One thing that the post-install script is supposed to do is decide what is"
echo "the main web browser, and write that to /tmp/rightbrwsr.txt"
echo "We need that info, as the live-CD iso filename has the browser name in it."
RIGHTBRWSR="`cat /tmp/rightbrwsr.txt`"
echo "...ok, it is $RIGHTBRWSR."

#v3.02 remove...
##some pkgs, like librsvg, put something into /usr/lib/mozilla/plugins...
#if [ -d rootfs-complete/usr/lib/mozilla ];then
# if [ ! -L rootfs-complete/usr/lib/mozilla ];then
#  if [ -e rootfs-complete/usr/lib/$RIGHTBRWSR ];then
#   case $RIGHTBRWSR in
#    seamonkey)
#     cp -af rootfs-complete/usr/lib/mozilla/plugins/* rootfs-complete/usr/lib/seamonkey/plugins/
#     sync
#     rm -rf rootfs-complete/usr/lib/mozilla
#     ln -s seamonkey rootfs-complete/usr/lib/mozilla
#    ;;
#    firefox)
#     cp -af rootfs-complete/usr/lib/mozilla/plugins/* rootfs-complete/usr/lib/firefox/plugins/
#     sync
#     rm -rf rootfs-complete/usr/lib/mozilla
#     ln -s firefox rootfs-complete/usr/lib/mozilla
#    ;;
#   esac
#  fi
# fi
#fi

RAMDISKSIZE=`cat $PUPPYDIR/rootfs-complete/etc/ramdisksize`

##do not want to have a root0 directory in pup 2.0.0...
#rm -rf rootfs-complete/root 2>/dev/null #precaution.
#mv -f rootfs-complete/root0 rootfs-complete/root
#sync

#update puppyversion...
echo -n "$PUPPYVERSION" > rootfs-complete/etc/puppyversion

#v411 remove stray files...
rm -f rootfs-complete/etc/rc.d/rc.sysinit-UNIPUP
rm -f rootfs-complete/etc/rc.d/rc.shutdown-UNIPUP

#v2.02 sanity check and fixup for desktop icons...
echo "Sanity check for ROX desktop icons..."
echo -n "" > /tmp/PuppyPinfixed
cat rootfs-complete/root/Choices/ROX-Filer/PuppyPin |
while read HEAD1ST
do
 BADLNK=""
 LNKEXEC="`echo -n "$HEAD1ST" | grep '<icon' | cut -f 2 -d '>' | cut -f 1 -d '<'`"
 if [ "`echo -n "$LNKEXEC" | grep '/usr/local/apps'`" = "" ];then
  if [ ! "$LNKEXEC" = "" ];then
   if [ ! "`echo -n "$LNKEXEC" | grep 'default'`" = "" ];then
    LNKEXEC="`cat rootfs-complete${LNKEXEC} | tail -n 1 | tr -s ' ' | cut -f 2 -d ' '`"
   fi
   LNKEXEC="`basename $LNKEXEC`"
   BADLNK="yes"
   FNDEXEC="`find ./rootfs-complete/ -type f -mount -name $LNKEXEC | grep --extended-regexp '/usr/bin|/usr/sbin|/usr/local/bin'`"
   [ ! "$FNDEXEC" = "" ] && BADLNK=""
   #but, v2.10 /usr/local/bin/defaultspreadsheet has 'gnumeric'
   #which is a link to gnumeric-1.6.3...
   FNDEXEC="`find ./rootfs-complete/ -type l -mount -name $LNKEXEC | grep --extended-regexp '/usr/bin|/usr/sbin|/usr/local/bin'`"
   [ ! "$FNDEXEC" = "" ] && BADLNK=""
  fi
 fi
 [ "$BADLNK" = "" ] && echo "$HEAD1ST" >> /tmp/PuppyPinfixed        
done
sync
cp -f /tmp/PuppyPinfixed rootfs-complete/root/Choices/ROX-Filer/PuppyPin


echo -n "Press ENTER to build files that will be in the iso: "
read yabbo
echo
cd $PUPPYDIR



rm -f isolinux-builds/pup_$PUPPYVERSION.sfs
rm -f isolinux-builds/pup_*.sfs 2>/dev/null
rm -f isolinux-builds/vmlinuz
rm -f isolinux-builds/initrd.gz
rm -f isolinux-builds/zdrv_*.sfs 2>/dev/null
sync

##v2.11
VMMODS=""
SQFSLZ=""


echo "Copying kernels/$DIRKERNEL/$NAMEKERNEL to isolinux-builds/vmlinuz..."
cp -f kernels/$DIRKERNEL/$NAMEKERNEL isolinux-builds/vmlinuz #v406
sync

#v2.12 now have complete set of modules...
echo "Creating modules-complete/..."
rm -rf modules-complete
cp -a kernels/$DIRKERNEL/all-modules modules-complete #v406
#mkdir -p modules-complete/etc/modules
#cp -a kernels/$USEKERNEL/firmware.dep.$USEKERNEL modules-complete/etc/modules/

#v3.98 just copy firmware dirs as-is... note, see 'modprobe' script...
cp -a kernels/$DIRKERNEL/all-firmware modules-complete/lib/modules/ #v406
sync

#v4.02 change firmware into tarballs...
cd modules-complete/lib/modules/all-firmware
for ONEFIRMDIR in `ls -1`
do
 tar -c -f ${ONEFIRMDIR}.tar ${ONEFIRMDIR}/
 gzip ${ONEFIRMDIR}.tar
 sync
 rm -rf ${WKGDIR}/modules-complete/lib/modules/all-firmware/${ONEFIRMDIR}
done
cd $WKGDIR

#NOTE: removing some exotic modules...
echo
echo "Removing some exotic modules unlikely to be needed..."
#some broad categories that are esoteric, don't need... v3.99 cifs and xfs restored...
#v4.01  'drivers/hwmon',  'drivers/clocksource' restored. v406 fs/udf
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/nfsd fs/romfs fs/sysv '
EXOTICNET=' net/appletalk net/ipv6 net/ipx net/llc net/sunrpc net/wanrouter '
#v408 EXOTICSOUND=' sound/oss '
EXOTICSOUND='' #v408
EXOTICDRIVERS=' drivers/block/paride drivers/block/aoe drivers/mtd drivers/telephony drivers/video '
mv -f modules-complete/lib/modules/$USEKERNEL/kernel/drivers/video/output.ko /tmp/ #quick hack. acpi video.ko needs this.
for ONEDIR in $EXOTICFS $EXOTICNET $EXOTICSOUND $EXOTICDRIVERS
do
 [ -d modules-complete/lib/modules/$USEKERNEL/kernel/$ONEDIR ] && rm -rf modules-complete/lib/modules/$USEKERNEL/kernel/$ONEDIR
done
sync
mkdir -p modules-complete/lib/modules/$USEKERNEL/kernel/drivers/video
mv -f /tmp/output.ko modules-complete/lib/modules/$USEKERNEL/kernel/drivers/video/
echo "...ok, some removed."

echo "Do you want to remove all the old true-SCSI drivers? True-SCSI drives are"
echo -n "quite rare. Just press ENTER to remove, any other printable char to keep: "
read scsi2go
if [ "$scsi2go" = "" ];then
 echo "Removing true-SCSI drivers from modules-complete/lib/modules/$USEKERNEL/kernel/drivers/scsi..."
 mkdir -p /tmp/scsikeep
 mv -f modules-complete/lib/modules/$USEKERNEL/kernel/drivers/scsi/imm.ko /tmp/scsikeep/
 mv -f modules-complete/lib/modules/$USEKERNEL/kernel/drivers/scsi/ppa.ko /tmp/scsikeep/
 mv -f modules-complete/lib/modules/$USEKERNEL/kernel/drivers/scsi/raid_class.ko /tmp/scsikeep/
 mv -f modules-complete/lib/modules/$USEKERNEL/kernel/drivers/scsi/sg.ko /tmp/scsikeep/
 mv -f modules-complete/lib/modules/$USEKERNEL/kernel/drivers/scsi/scsi_wait_scan.ko /tmp/scsikeep/
 rm -rf modules-complete/lib/modules/$USEKERNEL/kernel/drivers/scsi
 mkdir -p modules-complete/lib/modules/$USEKERNEL/kernel/drivers/scsi
 cp -a /tmp/scsikeep/* modules-complete/lib/modules/$USEKERNEL/kernel/drivers/scsi/
 rm -rf /tmp/scsikeep
fi
sync
depmod -b $WKGDIR/modules-complete -F $WKGDIR/kernels/$DIRKERNEL/System.map $USEKERNEL
sync

#v4.00 v406
cp -a kernels/$DIRKERNEL/DOTconfig-K${USEKERNEL} rootfs-complete/etc/modules/
cp -a kernels/$DIRKERNEL/firmware.dep.${USEKERNEL} rootfs-complete/etc/modules/

##this symlink is needed...
#ln -snf $USEKERNEL rootfs-complete/lib/modules/2.6

#v2.14 put the glibc_locales pkg into zdrv... v2.20 no...
#echo "Copying locales into modules-complete..."
#LOCALESPKG="`cat rootfs-complete/root/.packages/packages.txt | grep '"glibc_locales-' | head -n 1 | cut -f 2 -d '"'`"
# if [ ! "$LOCALESPKG" = "" ];then
#  cp -af packages/$LOCALESPKG/* modules-complete/
# fi

rm -rf boot/initrd-tree
cp -a boot/initrd-tree0 boot/initrd-tree
sync

myzdrv=""
echo

echo "Would you like the complete set of kernel modules to be in the initrd?"
echo "Or, would you prefer them to be a separate file, named zdrv_${PUPPYVERSION}.sfs?"
echo "Press ENTER key only for the zdrv file, "
echo "(note, ENTER key only will also offer further choice to put the"
echo " modules in the pup_${PUPPYVERSION}.sfs rather than create a zdrv file)"
echo -n "or any printable char then ENTER only for all modules to be in the initrd: "
read FLAGZDRV

if [ "$FLAGZDRV" = "" ];then
 #chose not to put all the zdrv components into the initrd, but still do need a small
 #number of modules required for drive recognition...
 echo
 echo "Moving some modules to initrd (req'd for drive recognition): "
 #v3.95 added scsi_wait_scan.ko...
 #v407 blkcipher not in 2.6.25 kernel, now crypto_blkcipher...
 # ditto, aes now aes_generic. v412 added ssb.ko.
 NEEDEDINITRDMODS=' aufs.ko cdrom.ko fuse.ko ide-cd.ko ide-floppy.ko nls_cp437.ko nls_iso8859-1.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 '
 for ONENEEDED in $NEEDEDINITRDMODS
 do
  echo -n "$ONENEEDED "
  FNDONE="`find modules-complete/lib/modules -type f -name $ONENEEDED | sed -e 's/modules\-complete//'`"
  if [ "$FNDONE" != "" ];then
   FNDDIR="`dirname $FNDONE`"
   mkdir -p boot/initrd-tree$FNDDIR
   cp -a modules-complete${FNDONE} boot/initrd-tree${FNDONE}
   gzip boot/initrd-tree${FNDONE}
   rm -f modules-complete${FNDONE} #v4.02 avoid duplication.
  fi
 done
 sync
 boot/depmod -b $WKGDIR/boot/initrd-tree -F $WKGDIR/kernels/$DIRKERNEL/System.map $USEKERNEL
 sync
 echo
fi

if [ "$FLAGZDRV" != "" ];then
 #v2.20 copy modules-complete into initrd...
 echo
 echo "Press a printable char then ENTER to reduce modules to a minimum selection."
 echo "This will run 'pickmodules.sh $USEKERNEL zdrvinitcut' which selects only"
 echo "a small subset of modules to copy into the initrd. "
 echo -n "Or, press ENTER only for all modules: "
 read zdrvsubset
 if [ "$zdrvsubset" = "" ];then
  echo "Copying all of modules-complete to initrd..."
  cp -af modules-complete/* boot/initrd-tree/
 else
  ./pickmodules.sh $USEKERNEL zdrvinitcut
  echo "Copying cutdown selecion to boot/initrd-tree..."
  cp -a modules-complete/lib/modules/all-firmware boot/initrd-tree/lib/modules/
  #cp -a modules-complete/lib/modules/firmware.dep.$USEKERNEL boot/initrd-tree/etc/modules/
  for ONECHOSENMOD in `cat /tmp/zdrvcutdown.list`
  do
   CHOSENPATH="`dirname $ONECHOSENMOD`"
   mkdir -p boot/initrd-tree/lib/modules/$USEKERNEL/$CHOSENPATH
   cp -af modules-complete/lib/modules/$USEKERNEL/$ONECHOSENMOD boot/initrd-tree/lib/modules/$USEKERNEL/$CHOSENPATH/
  done
 fi
 sync
 find boot/initrd-tree/lib/modules -type f -name *.ko |
 while read ONEKO
 do
  gzip $ONEKO
  sync
 done
 #cp -af boot/depmod boot/initrd-tree/bin/
 #chroot boot/initrd-tree /bin/depmod -a
 boot/depmod -b $WKGDIR/boot/initrd-tree -F $WKGDIR/kernels/$DIRKERNEL/System.map $USEKERNEL
 sync
 #rm -f boot/initrd-tree/bin/depmod
 
else
 #offer choice to have zdrv file in iso or a cutdown set of modules...
 echo
 echo "If you want the modules to be in pup_$PUPPYVERSION.sfs, and zdrv_$PUPPYVERSION.sfs"
 echo "not created at all, then type <ENTER> key only."
 echo -n "Type any printable char then ENTER for default zdrv: "
 read rev_myzdrv
 myzdrv=""
 [ "$rev_myzdrv" = "" ] && myzdrv="yes"
 echo
 if [ "$myzdrv" = "" ];then
  echo "Hit ENTER key to create zdrv_$PUPPYVERSION.sfs in isolinux-builds/, for"
  echo "inclusion in the live-CD iso file. Any other character key then ENTER"
  echo -n "not to include zdrv file: "
  read myzdrv
  if [ "$myzdrv" = "" ];then
   #v2.16 cutdown zdrv...
   echo "You can build a smaller zdrv, using pickmodules.sh to select a subset"
   echo -n "of drivers. Hit ENTER only for the full zdrv, any char for subset: "
   read xmyzdrv
   if [ "$xmyzdrv" != "" ];then
    #modules-complete already has the full set of drivers.
    ./pickmodules.sh $USEKERNEL zdrvcut #returns /tmp/zdrvcutdown.list
    find modules-complete -name *.ko | sed -e 's/^modules-complete//g' |
    while read ONEKO
    do
     NAMEKO="`basename $ONEKO`"
     xNAMEONLY="`basename $ONEKO .ko`"
     KPATTERN="/$NAMEKO"
     xPATTERN="^$xNAMEONLY " #v3.02
     ePATTERN=" ${xNAMEONLY}"'$' #v3.94
     if [ "`grep "$KPATTERN" /tmp/zdrvcutdown.list`" = "" ];then
      rm -f modules-complete${ONEKO}
     fi
    done
    #NO, when compressed in sfs, this only adds 600K to size of zdrv file...
    ##do not want keyboard locales stuff in cutdown zdrv...
    #rm -rf modules-complete/usr/lib/locale
   fi
   sync
   echo "Creating zdrv_$PUPPYVERSION.sfs in isolinux-builds/..."
   #$PUPPYDIR/mksquashfs modules-complete/lib/modules  isolinux-builds/zdrv_$PUPPYVERSION.sfs
   $PUPPYDIR/mksquashfs modules-complete  isolinux-builds/zdrv_$PUPPYVERSION.sfs
  fi
 else
  echo
  echo "Press ENTER key only to place all modules into pup_$PUPPYVERSION.sfs."
  echo "Press any printable char then ENTER to place a usable subset of"
  echo "modules (about 10MB compressed) into pup_$PUPPYVERSION.sfs."
  echo -n "Waiting: "
  read forsubset
  if [ "$forsubset" = "" ];then
   echo "Copying all modules from modules-complete to rootfs-complete..."
   cp -af modules-complete/* rootfs-complete/
   sync
  else
   echo "The cutdown choice of modules is currently fixed and you will need"
   echo "to edit pickmodules.sh to change it."
   echo -n "Hit ENTER key to run pickmodules.sh: "
   read mypick
   ./pickmodules.sh $USEKERNEL
  fi
 fi
 sync
fi

#v4.02 remove...
#if [ "$myzdrv" = "" -a "$FLAGZDRV" = "" ];then
# #chose not to put all the zdrv components into the initrd, and also
# #chose not to put a large subset of the modules into the pup_xxx.sfs file, but do
# #put in a small number that are always going to be needed (into pup_xxx.sfs)...
# NEEDEDDRIVERSCHAR=' lp '
# #v4.00 added ndiswrapper...
# NEEDEDDRIVERSNET=' bsd_comp plip ppp_async ppp_deflate ppp_generic ppp_mppe ppp_synctty pppoe pppox slhc slip ndiswrapper '
# NEEDEDDRIVERSPARPORT=' parport parport_pc '
# NEEDEDDRIVERSSCSI=' sg '
# NEEDEDDRIVERSUSB=' cdc-acm usblp '
# NEEDEDFS=' smbfs '
# NEEDEDLIB=' crc-ccitt zlib_deflate '
# NEEDEDNETIPV4=' ipip tunnel4 '
# NEEDEDDIRS=' kernel/net/ipv4/netfilter kernel/net/netfilter '
# ALLNEEDED="$NEEDEDDRIVERSCHAR $NEEDEDDRIVERSNET $NEEDEDDRIVERSPARPORT $NEEDEDDRIVERSSCSI $NEEDEDDRIVERSUSB $NEEDEDFS $NEEDEDLIB $NEEDEDNETIPV4"
# for ONENEEDED in $ALLNEEDED
# do
#  ONENEEDED="${ONENEEDED}.ko"
#  FNDONE="`find modules-complete/lib/modules -type f -name $ONENEEDED | sed -e 's/modules\-complete//'`"
#  if [ "$FNDONE" != "" ];then
#   FNDDIR="`dirname $FNDONE`"
#   mkdir -p rootfs-complete$FNDDIR
#   #cp -a modules-complete${FNDONE} rootfs-complete${FNDONE}
#   #v3.91 avoid duplication...
#   mv -f modules-complete${FNDONE} rootfs-complete${FNDONE}
#  fi
# done
# PREDIR="/lib/modules/$USEKERNEL/"
# for ONEDIR in $NEEDEDDIRS
# do
#  UPONE="`dirname $ONEDIR`"
#  mkdir -p rootfs-complete${PREDIR}${UPONE}
#  cp -a modules-complete${PREDIR}${ONEDIR} rootfs-complete${PREDIR}${UPONE}/
#  #v3.91 avoid duplication...
#  sync
#  rm -rf modules-complete${PREDIR}${ONEDIR}
# done
# sync 
# depmod -b $WKGDIR/rootfs-complete -F $WKGDIR/kernels/$DIRKERNEL/System.map $USEKERNEL
# sync 
#fi

echo
echo "The file rootfs-complete/etc/networkmodules is a list of network"
echo "drivers that is read by the Network Wizard (usr/sbin/net-setup.sh)"
echo -n "Press ENTER key to create this file: "
read gonetlist
./updatenetmoduleslist.sh

##v2.17...
#echo
#echo "Do you want to compress pup_$PUPPYVERSION.sfs with LZMA?"
#echo -n "Just ENTER for yes (recommended) or any other char for GZIP: "
#read FLAGLZMA
FLAGLZMA='gzip' #v2.20

echo "Converting rootfs-complete/ folder to squashfs isolinux-builds/pup_$PUPPYVERSION.sfs..."
if [ "$FLAGLZMA" = "" ];then #v2.17
 echo "(using LZMA)"
 $PUPPYDIR/mksquashfs-lzma rootfs-complete isolinux-builds/pup_$PUPPYVERSION.sfs
else
 echo "(using GZIP)"
 #despite the docs, this option doesn't work...
 #$PUPPYDIR/mksquashfs -nolzma rootfs-complete isolinux-builds/pup_$PUPPYVERSION.sfs
 $PUPPYDIR/mksquashfs rootfs-complete isolinux-builds/pup_$PUPPYVERSION.sfs
fi
sync

chmod 744 isolinux-builds/pup_$PUPPYVERSION.sfs
#chmod 744 isolinux-builds/zdrv_$PUPPYVERSION.sfs


#v2.15 now build initrd.gz...
cd boot
mv -f initrd.gz initrd-previous.gz
echo
echo "Would you like to create a 'humongous' initrd?"
echo "Normally you will reply no here (just press ENTER key only)."
echo "Some special uses of Puppy, such as network booting, may require"
echo "Puppy to be in just two files, vmlinuz (the kernel) and initrd.gz"
echo "(with pup_$PUPPYVERSION.sfs and maybe zdrv_$PUPPYVERSION.sfs inside it)."
echo "Such a large initrd.gz file requires a lot of physical RAM, usually"
echo "at least 256MB, so not suitable for older PCs."
echo
echo "Press ENTER key only for a normal initrd.gz (recommended), or"
echo -n "press any character then ENTER key for 'humongous' initrd.gz: "
read HUMONGOUS
echo
echo "Creating boot/initrd.gz..."
RDCOMPR='gzip'
[ "`echo "$NAMEKERNEL" | grep '\-RDLZMA'`" != "" ] && RDCOMPR='lzma'
if [ "$HUMONGOUS" = "" ];then
 ./makeinitrdgz.sh $RDCOMPR #v406
else
 mv -f ../isolinux-builds/pup_$PUPPYVERSION.sfs ./initrd-tree/
 sync
 [ -f ../isolinux-builds/zdrv_$PUPPYVERSION.sfs ] && mv -f ../isolinux-builds/zdrv_$PUPPYVERSION.sfs ./initrd-tree/
 sync
 ./makeinitrdgz.sh $RDCOMPR #v406
 sync
 #rm -f ./initrd-tree/pup_$PUPPYVERSION.sfs
fi
sync
cd $PUPPYDIR
echo "Copying boot/initrd.gz to isolinux-builds/..."
cp boot/initrd.gz isolinux-builds/
sync



#v2.15 now create the isolinux.cfg file... v2.16rc modified...
echo "Creating isolinux-builds/isolinux.cfg..."
CFGBASE='default puppy
display boot.msg
prompt 1
label puppy
kernel vmlinuz'
echo "$CFGBASE" > isolinux-builds/isolinux.cfg
if [ "$INITTYPE" = "initrd" ];then
 INITRDSIZEK="`cat /tmp/initrdsize.txt`"
 if [ "$HUMONGOUS" = "" ];then
  #normal situation....
  echo "append root=/dev/ram0 initrd=initrd.gz pmedia=cd" >> isolinux-builds/isolinux.cfg
 else
  echo "append root=/dev/ram0 initrd=initrd.gz pmedia=cd ramdisk_size=$INITRDSIZEK" >> isolinux-builds/isolinux.cfg
 fi
else
 #cpio archive, initramfs.
 echo "append initrd=initrd.gz pmedia=cd" >> isolinux-builds/isolinux.cfg
fi
echo 'timeout 50' >> isolinux-builds/isolinux.cfg
sync

#404
cp -af boot.msg.puppy isolinux-builds/boot.msg

#v3.94
[ -f /tmp/ORIG-packages.txt ] && mv -f /tmp/ORIG-packages.txt packages.txt


echo
echo "Now to build the iso file..."
echo "Note, if possible, choose to burn to dvd-r here, as it will create an iso"
echo "file that is best for booting from both cd & dvd, both normal & multisession."
echo "(but, you MUST have a blank dvd-r ready, as iso created by burning to dvd first)"

echo -n "Press ENTER to create iso for booting from CD, any other for DVD: "
read CDDVD

if [ "$CDDVD" = "" ];then
 rm -f puppy-$RIGHTVER-$RIGHTBRWSR.iso
 sync
 echo -n "Press ENTER to build ISO file: "
 read mmnn
 #-J removed, Joliet causes trouble saving sessions...
 mkisofs -D -R -o puppy-$RIGHTVER-$RIGHTBRWSR.iso -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table ./isolinux-builds/
 sync
 echo
 echo "...done."
 echo "puppy-$RIGHTVER-$RIGHTBRWSR$RIGHTINSIDE.iso created."

    echo
    echo "Can now burn to CD-R. (CTRL-C if you want to quit script)"
    echo -n 'Type "y" for multisession, else just ENTER: '
    read BURNMULTI
    if [ "$BURNMULTI" = "y" ];then
     BURNMULTI="-multi -tao -pad" #v2.02 added -tao -pad
    else
     BURNMULTI="-dao" #v2.02 added -dao
    fi
    echo "Please insert blank CD into $CDR
(which is described as: ${CDDESCR})
-- also be sure that it is unmounted.
then hit ENTER key: "
    read yayburn
    cdrecord $BURNMULTI -data -eject -v speed=4 dev=ATAPI:$CDR puppy-$RIGHTVER-$RIGHTBRWSR.iso
    sync
fi


echo -n "Press ENTER to create iso for booting from DVD, any other to quit: "
read CDDVD

if [ "$CDDVD" = "" ];then
    echo
    echo "You have chosen DVD."
    echo "This works in a roundabout fashion. You have to burn the files direct"
    echo "to DVD-R right now, then the iso is copied from the DVD."
    echo
    echo "Please insert blank DVD-R single-layer into $CDR
(which is described as: ${CDDESCR})
-- also be sure that it is unmounted.
(it must be a DVD-R single-layer, nothing else)
then hit ENTER key: "
    read yayburn
    #growisofs -speed=4 -Z ${CDR}=puppy-${RIGHTVER}-${RIGHTBRWSR}.iso
    echo
    echo "A bit of a hack in the script here. for dvd, using mkisofs directly gives"
    echo "error msgs at bootup and shutdown. now will"
    echo "run growisofs to burn the files direct to dvd. "
    #-J removed, Joliet causes trouble saving sessions...
    growisofs -speed=4 -Z $CDR -R -D -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table ./isolinux-builds/
    echo
    echo "Okay, burnt to DVD."
    sync
    echo
    echo "Using dd to create puppy-$RIGHTVER-$RIGHTBRWSR-dvd.iso from the DVD..."
    echo "(note, Graveman duplicate dvd->iso does not work right, something wrong iso)"
    rm -f puppy-$RIGHTVER-$RIGHTBRWSR-dvd.iso
    sync
    echo
    echo -n "Make sure tray is closed, then press ENTER: " #3.95
    read waitontray
    dd if=$CDR of=puppy-$RIGHTVER-$RIGHTBRWSR-dvd.iso bs=32k seek=0
    sync
    echo "...done"

    echo
    echo "Just checking integrity..."
    mount -t iso9660 $CDR /mnt/data
    md5sum /mnt/data/vmlinuz > /tmp/dvd-vmlinuz.chk
    md5sum /mnt/data/pup_$PUPPYVERSION.sfs > /tmp/dvd-pup_$PUPPYVERSION.sfs.chk
    md5sum /mnt/data/initrd.gz > /tmp/dvd-initrd.gz.chk
    sync
    md5sum ./isolinux-builds/vmlinuz > /tmp/vmlinuz.chk
    md5sum ./isolinux-builds/pup_$PUPPYVERSION.sfs > /tmp/pup_$PUPPYVERSION.sfs.chk
    md5sum ./isolinux-builds/initrd.gz > /tmp/initrd.gz.chk
    sync
    umount /mnt/data
    CHKVMLINUZ="`cat /tmp/vmlinuz.chk | cut -f 1 -d ' '`"
    CHKSFS="`cat /tmp/pup_$PUPPYVERSION.sfs.chk | cut -f 1 -d ' '`"
    CHKINITRD="`cat /tmp/initrd.gz.chk | cut -f 1 -d ' '`"
    [ ! "`cat /tmp/dvd-vmlinuz.chk | cut -f 1 -d ' '`" = "$CHKVMLINUZ" ] && echo "AAARGH! FAIL!"
    [ ! "`cat /tmp/dvd-pup_$PUPPYVERSION.sfs.chk | cut -f 1 -d ' '`" = "$CHKSFS" ] && echo "AAARGH! FAIL!"
    [ ! "`cat /tmp/dvd-initrd.gz.chk | cut -f 1 -d ' '`" = "$CHKINITRD" ] && echo "AAARGH! FAIL!"
    echo -n "Press ENTER to continue: "
    read booboo
fi





cd $WKGDIR

####END####
