#!/bin/bash
#(c) copyright Barry Kauler 2006,2007 www.puppylinux.com
#script to create initrd.gz, pup_xxx.sfs, zdrv_xxx.sfs and live-CD iso
#from packages.

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-tree/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...
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..."


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 "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

for ONEPKG in $PKGLIST
do
  #v1.0.3
  # -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...
  sync
  cp-FULL -a --remove-destination packages/$ONEPKG/* rootfs-complete/ 2> /dev/null
  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


#v1.0.3
echo
echo "These kernels are available:"
ls -1 kernels/
#USEKERNEL="`ls -1 kernels/ | tail -n 1`"
USEKERNEL="`uname -r`"
echo "Please type in which one of these kernels you want to use. "
echo "Note: initrd.gz has the modules for one kernel only, so if"
echo "       alternative kernels are to be offered then the modules"
echo "       must be compatible."
echo -n "If you just press ENTER key, the default is $USEKERNEL: "
read mmaadd
if [ ! "$mmaadd" = "" ];then
 USEKERNEL="$mmaadd"
fi
echo "...okay, using $USEKERNEL"

echo
echo "Building rootfs-complete/lib/modules kernel modules directory..."
#now build the kernel modules...
KRNLMODULES="`ls -1 kernels/$USEKERNEL/modules/`"
for ONEPKG in $KRNLMODULES
do
 if [ -d ./kernels/$USEKERNEL/modules/$ONEPKG ];then
  cp -a kernels/$USEKERNEL/modules/$ONEPKG/* rootfs-complete/
 fi
 sync
done

#echo
#echo "As we now have alsa modules, do you want to remove the oss modules?"
#echo -n "(to save space). Press ENTER only for yes (to remove): "
#read nooss
#if [ "$nooss" = "" ];then
# echo "Removing rootfs-complete/lib/modules/$USEKERNEL/sound/oss..."
# rm -rf rootfs-complete/lib/modules/$USEKERNEL/sound/oss
# echo "Emptying rootfs-complete/lib/libhardware/soundcard.db..."
# echo "" > rootfs-complete/lib/libhardware/soundcard.db
#fi
#sync

#make sure no modules are compressed...
CURRWKG="`pwd`"
echo
echo "Gunzipping kernel modules..."
find rootfs-complete/lib/modules/$USEKERNEL -type f -name *.gz |
while read ONEMODULE
do
 ONEPATH="`dirname $ONEMODULE`"
 ONEBASE="`basename $ONEMODULE`"
 cd $ONEPATH
 gunzip $ONEBASE
 sync
 cd $CURRWKG
done
cd $CURRWKG

#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/$USEKERNEL/System.map $USEKERNEL
sync
if [ -f ./kernels/$USEKERNEL/modules/pinstall.sh ];then
 echo "Executing modules post-install script..."
 ./kernels/$USEKERNEL/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}"
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

#v2.10 remove this...
##v1.0.2
##need to generate /etc/modprobe.conf from /etc/modules.conf
##this section assumes that Puppy is running and running a 2.6 kernel...
##...ha ha. otherwise, generate-modprobe.conf won't work...
#if [ -e $WKGDIR/rootfs-complete/lib/modules/2.6 ];then
# echo
# echo "Generating rootfs-complete/etc/modprobe.conf..."
# rm -f $WKGDIR/rootfs-complete/etc/modprobe.conf 2> /dev/null
# cat $WKGDIR/rootfs-complete/etc/modules.conf | grep -v '^#' > /tmp/modules.conf.tmp
# sync
# cp -f /tmp/modules.conf.tmp $WKGDIR/rootfs-complete/etc/modules.conf
# chroot $WKGDIR/rootfs-complete /sbin/generate-modprobe.conf /etc/modprobe.conf
# sync
# #cp -f $WKGDIR/rootfs-complete/etc/modprobe.conf  $WKGDIR/rootfs-complete/etc/modprobe.conf0
# ##...modprobe.conf0 will be the reference file, not changed.
# sync
#fi


#echo
#echo -e "Each package can have an optional file called \"keyword\" that has keywords"
#echo "and these have been concatenated into a space delimited list /tmp/keywords.txt"
#echo "used by the window manager post-install script to build menus."
#
#echo "Each window manager is supposed to have a fully-populated menu in its config"
#echo "file, with each entry commented-out. The post-install script for each w.m. is"
#echo "supposed to read /tmp/keywords.txt and uncomment matching lines in config file."
#echo -e "For example, keyword for gcombust-0.1.55 package is \"Gcombust\". A w.m."
#echo "post-install script should find lines with that in-context, for example in JWM"
#echo -e "it would be \"label=\"Gcombust\" and that line must be uncommented."

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"

#v2.14 fixmenus not needed...
##v1.0.5
##a problem if jwm or fvwm95 pkg not chosen. still do need the .jwmrc or .fvwm95
##config files in puppy...
#if [ ! -f $WKGDIR/rootfs-complete/root0/.fvwm95rc ];then
# echo
# echo "Fvwm95 has not been chosen, however as it will be a pupget pkg and can be"
# echo "installed later, .fvwm95rc, the config file, still has to be processed."
# FVWMPKG="`cat /tmp/packagesfnd.txt | grep '"fvwm95-' | head -n 1 | cut -f 2 -d '"'`"
# cp $WKGDIR/packages/$FVWMPKG/root0/.fvwm95rc $WKGDIR/rootfs-complete/root0/
# KEYWORDS="`cat /tmp/keywords.txt`"
# sync
# echo "Processing keywords for rootfs-complete/root0/.fvwm95rc..."
# for ONEKEY in $KEYWORDS
# do
#  /usr/sbin/fixmenus $WKGDIR/rootfs-complete/root0 $ONEKEY fvwm95
# done
# sync
#fi
#if [ ! -f $WKGDIR/rootfs-complete/root0/.jwmrc ];then
# echo
# echo "JWM has not been chosen, however as it will be a pupget pkg and can be"
# echo "installed later, .jwmrc, the config file, still has to be processed."
# JWMPKG="`cat /tmp/packagesfnd.txt | grep '"jwm-' | head -n 1 | cut -f 2 -d '"'`"
# cp $WKGDIR/packages/$JWMPKG/root0/.jwmrc $WKGDIR/rootfs-complete/root0/
# KEYWORDS="`cat /tmp/keywords.txt`"
# sync
# echo "Processing keywords for rootfs-complete/root0/.jwmrc..."
# for ONEKEY in $KEYWORDS
# do
#  /usr/sbin/fixmenus $WKGDIR/rootfs-complete/root0 $ONEKEY jwm
# done
# sync
#fi
##v1.0.6...
#if [ ! -f $WKGDIR/rootfs-complete/root0/.icewm/menu ];then
# echo
# echo "iceWM is not an Unleashed package, but menu file still has to be processed."
# mv $WKGDIR/rootfs-complete/root0/.icewm/raw-menu $WKGDIR/rootfs-complete/root0/.icewm/menu
# KEYWORDS="`cat /tmp/keywords.txt`"
# sync
# echo "Processing keywords for rootfs-complete/root0/.icewm/menu..."
# for ONEKEY in $KEYWORDS
# do
#  /usr/sbin/fixmenus $WKGDIR/rootfs-complete/root0 $ONEKEY icewm
# done
# sync
#fi
##v2.13
##ROX-Filer, "Desktop background color" menu item does not work...
#if [ -d $WKGDIR/rootfs-complete/root0/.config/rox.sourceforge.net ];then
# #remove entry, all w.m.s...
# /usr/sbin/fixmenus $WKGDIR/rootfs-complete/root0 -Desktop
#else
# /usr/sbin/fixmenus $WKGDIR/rootfs-complete/root0 +Desktop
#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...
cat ../packages.txt | grep -v '_DEV' > ./root/.packages/packages.txt
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

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."

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

#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 iso files from rootfs-complete: "
read yabbo
echo
cd $PUPPYDIR

#TODO optimisation to avoid duplication of files in initrd and pup_xxx.sfs...

##move all the modules to initrd...
#echo "Moving all modules to initrd..."
#rm -rf boot/initrd-tree/lib/modules
#sync
#mv -f rootfs-complete/lib/modules boot/initrd-tree/lib/
#sync
##this will be valid when puppy is running...
#ln -s /initrd/lib/modules rootfs-complete/lib/modules


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

##v2.11
VMMODS=""
SQFSLZ=""
#if [ -f rootfs-complete/usr/bin/mksquashfs-lzma ];then
# VMMODS='-SQUASHFS_LZMA'
# SQFSLZ='-lzma'
# if [ ! -f kernels/$USEKERNEL/vmlinuz$VMMODS ];then
#  echo
#  echo "ERROR, kernels/$USEKERNEL/vmlinuz$VMMODS does not exist."
#  echo "WARNING: Using plain 'vmlinuz' kernel instead."
#  echo "WARNING: Substituting normal rootfs-complete/usr/bin/mksquashfs."
#  echo "WARNING: The pup_xxx.sfs file will not be compressed with LZMA."
#  echo -n "Press ENTER to continue: "
#  read nogohere
#  VMMODS=""
#  SQFSLZ=""
#  #get rid of symlink to mksquashfs-lzma...
#  cp -a --remove-destination ./mksquashfs rootfs-complete/usr/bin/mksquashfs
# fi
#fi
#if [ "`cat rootfs-complete/root/.packages/packages.txt | grep '^"e2fsprogs' | grep '" on "' | grep 'e2compr'`" != "" ];then #'geany
# #need to use kernel patch with ext2 compression support...
# VMMODS="${VMMODS}-E2COMPR"
#fi

echo "Copying kernels/$USEKERNEL/vmlinuz$VMMODS to isolinux-builds/..."
#v2.11...
cp -f kernels/$USEKERNEL/vmlinuz$VMMODS isolinux-builds/vmlinuz
sync

#v2.12 now have complete set of modules...
echo "Creating modules-complete/..."
rm -rf modules-complete
cp -a kernels/$USEKERNEL/all-modules modules-complete
cp -a kernels/$USEKERNEL/firmware.dep.$USEKERNEL modules-complete/lib/modules/
echo ""  >> modules-complete/lib/modules/firmware.dep.$USEKERNEL
echo '#These are the directories and files in each firmware pkg...'  >> modules-complete/lib/modules/firmware.dep.$USEKERNEL
#copy all the firmware pkgs...
for ONEFIRM in `ls -1 kernels/$USEKERNEL/all-firmware/ | tr '\n' ' '`
do
 echo "Adding $ONEFIRM filelist to modules-complete/lib/modules/firmware.dep.$USEKERNEL..."
 cp -af kernels/$USEKERNEL/all-firmware/$ONEFIRM/* modules-complete/
 #need a list of all files in the package...
 echo -n "${ONEFIRM}|" >> modules-complete/lib/modules/firmware.dep.$USEKERNEL
 cd kernels/$USEKERNEL/all-firmware/$ONEFIRM
 find ./ -mount | sed -e 's/^\.//g' | grep -v '^/$' | tr ' ' '_' | tr '\n' ' ' >> $PUPPYDIR/modules-complete/lib/modules/firmware.dep.$USEKERNEL
 sync
 echo >> $PUPPYDIR/modules-complete/lib/modules/firmware.dep.$USEKERNEL
 cd $PUPPYDIR
done
sync

#v2.14 put the glibc_locales pkg into zdrv...
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


#offer choice to have zdrv file in iso or a cutdown set of modules...
echo
echo "If you want the pup_$PUPPYVERSION.sfs file to have a self-contained"
echo "subset of the complete set of kernel modules, a selection to handle"
echo "a wide range of common hardware (about 12MB), and zdrv_$PUPPYVERSION.sfs"
echo "not included in the live-CD, then hit <y> key then <ENTER> key."
echo "Or, to build pup_$PUPPYVERSION.sfs with a very skeleton selection of"
echo "modules and a reliance on the zdrv_$PUPPYVERSION.sfs file as a supply"
echo "of modules for networking, sound, etc, then just hit <ENTER> key."
echo "(Note, if you choose the latter, you can still build the live-cd without"
echo "the zdrv file, but it would need to be somewhere on the h.d. so that"
echo "Puppy can find it at bootup)."
echo -n "Hit ENTER for default zdrv: "
read myzdrv
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 myzdrv
  if [ "$myzdrv" != "" ];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`"
    KPATTERN="/$NAMEKO"
    if [ "`grep "$KPATTERN" /tmp/zdrvcutdown.list`" = "" ];then
     rm -f modules-complete${ONEKO}
     grep -v "$KPATTERN" rootfs-complete/lib/modules/modules.dep.$USEKERNEL > /tmp/tmp.tmp
     mv -f /tmp/tmp.tmp rootfs-complete/lib/modules/modules.dep.$USEKERNEL
     grep -v "$KPATTERN" rootfs-complete/lib/modules/modules.isapnpmap.$USEKERNEL > /tmp/tmp.tmp
     mv -f /tmp/tmp.tmp rootfs-complete/lib/modules/modules.isapnpmap.$USEKERNEL
     grep -v "$KPATTERN" rootfs-complete/lib/modules/modules.pcimap.$USEKERNEL > /tmp/tmp.tmp
     mv -f /tmp/tmp.tmp rootfs-complete/lib/modules/modules.pcimap.$USEKERNEL
    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$SQFSLZ modules-complete  isolinux-builds/zdrv_$PUPPYVERSION.sfs
 fi
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
sync

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

echo "Converting rootfs-complete/ folder to squashfs isolinux-builds/pup_$PUPPYVERSION.sfs..."
if [ "$FLAGLZMA" = "" ];then #v2.17
 echo "(using LZMA)"
 $PUPPYDIR/mksquashfs 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-gzip 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..."
if [ "$HUMONGOUS" = "" ];then
 ./makeinitrdgz.sh
else
 mv -f ../isolinux-builds/pup_$PUPPYVERSION.sfs ./initrd-tree/
 [ -f ../isolinux-builds/zdrv_$PUPPYVERSION.sfs ] && mv -f ../isolinux-builds/zdrv_$PUPPYVERSION.sfs ./initrd-tree/
 ./makeinitrdgz.sh
 sync
 rm -f ./initrd-tree/pup_$PUPPYVERSION.sfs
 rm -f ./initrd-tree/zdrv_$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

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