#!/bin/bash
#(c) copyright Barry Kauler 2008 puppylinux.com
#2008 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#this is a special version of 'createpuppy' script for building 'UniPup',
#which runs totally in a initramfs.
#v406 kernels/<sub dir> name can now be named <kern version>-<text>
#v411 improved dvd write-check.

[ ! -f packages.txt ] && exit
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 250M. A temporary copy of this is made,"
echo "needing another 250M, plus temp files, so you may need about 600M"
echo "free space in the partition that is running this script."
echo "WARNING: Do NOT use a ntfs, msdos or vfat partition!!!!!"
echo "Executing df command (available space shown in MegaBytes):"
df -m
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

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

##########
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`"
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 '#!/bin/sh' > pinstall.sh
rm -rf rootfs-complete
sync
mkdir rootfs-complete
PKGLIST="`cat packages.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
  sync
done
echo 'echo "END OF pinstall.sh SCRIPT."' >> pinstall.sh
chmod 755 pinstall.sh


#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

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 exit,"
  echo -ne "or if you really want to ignore problem, just press ENTER: "
  read goto1
 fi
 [ "$goto1" = "c" ] && exit
 if [ -s /tmp/notusedlibs.txt ];then
  echo
  echo "INTERESTING, the following libs are in Puppy but apparently not used:"
  cat /tmp/notusedlibs.txt
  echo
  echo -e "Press \"c\" key then  ENTER key to exit,"
  echo -ne "or if you really want to ignore problem, just press ENTER: "
  read goto1
 fi
 [ "$goto1" = "c" ] && exit
fi
echo "...done."


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 "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
 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
echo "Building shared library cache..."
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."

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

#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 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 2>/dev/null
rm -f isolinux-builds/zdrv_*.sfs 2>/dev/null
sync

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

#v3.98 just copy firmware dirs as-is... note, see 'modprobe' script...
cp -a kernels/$DIRKERNEL/all-firmware modules-complete/lib/modules/
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 '
EXOTICSOUND=' sound/oss '
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

echo
echo "Do you want to remove kernel modules for most software-analog-modems?"
echo "Modules for some modems will be retained as they are small. "
echo "These will be removed:"
echo "  agrsm ess intel536 intel537 ltmodem martian mwave pctel slmodem"
echo "These will be retained:"
echo " cdcacm pl2303 (these 2 are for USB hardware modems, can still be purchased new)"
echo "Also support for true hardware serial-port analog modems will be retained."
echo "Press ENTER key only to remove software-modem support,"
echo -n "Press any other printable-char then ENTER to keep software modems: "
read keepsoft
if [ "$keepsoft" = "" ];then
 rm -rf modules-complete/lib/modules/all-firmware/agrsm*.tar.gz
 rm -rf modules-complete/lib/modules/all-firmware/cdcacm*.tar.gz
 rm -rf modules-complete/lib/modules/all-firmware/ess*.tar.gz
 rm -rf modules-complete/lib/modules/all-firmware/intel*.tar.gz
 rm -rf modules-complete/lib/modules/all-firmware/ltmodem*.tar.gz
 rm -rf modules-complete/lib/modules/all-firmware/martian*.tar.gz
 rm -rf modules-complete/lib/modules/all-firmware/mwave*.tar.gz
 rm -rf modules-complete/lib/modules/all-firmware/pctel*.tar.gz
 rm -rf modules-complete/lib/modules/all-firmware/slmodem*.tar.gz
 rm -f modules-complete/lib/modules/$USEKERNEL/extra/agrmodem.ko
 rm -f modules-complete/lib/modules/$USEKERNEL/extra/agrserial.ko
 rm -f modules-complete/lib/modules/$USEKERNEL/extra/martian_dev.ko
 rm -f modules-complete/lib/modules/$USEKERNEL/extra/ungrab-winmodem.ko
 rm -f modules-complete/lib/modules/$USEKERNEL/extra/agrmodem.ko
 rm -rf modules-complete/lib/modules/$USEKERNEL/ltmodem
 rm -rf modules-complete/lib/modules/$USEKERNEL/slmodem
 rm -f modules-complete/lib/modules/$USEKERNEL/misc/esscom_hw.ko
 rm -f modules-complete/lib/modules/$USEKERNEL/misc/esscom.ko
 rm -f modules-complete/lib/modules/$USEKERNEL/misc/linmodem.ko
 rm -f modules-complete/lib/modules/$USEKERNEL/misc/pctel_hw.ko
 rm -f modules-complete/lib/modules/$USEKERNEL/misc/pctel.ko
 rm -f modules-complete/lib/modules/$USEKERNEL/kernel/drivers/char/Intel536.ko
 rm -f modules-complete/lib/modules/$USEKERNEL/kernel/drivers/char/Intel537.ko
 sync
fi

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

##########
#v404 unipup has all modules in initrd only...
  echo
  echo "Press ENTER key only to place all modules into rootfs-complete."
  echo "Press any printable char then ENTER to place a usable subset of"
  echo "modules (about 10MB compressed) into rootfs-complete."
  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
   depmod -b $WKGDIR/modules-complete -a -F $WKGDIR/kernels/$DIRKERNEL/System.map $USEKERNEL
   ./pickmodules.sh $USEKERNEL unipup #this copies modules, not firmware.
   cp -af modules-complete/lib/modules/all-firmware rootfs-complete/lib/modules/
  fi
 sync

#v404 compress all modules...
echo
echo "Compressing all modules in rootfs-complete/lib/modules/${USEKERNEL}..."
for ONEMODULE in `find rootfs-complete/lib/modules/$USEKERNEL -type f -name \*.ko`
do
 gzip $ONEMODULE
done
sync

depmod -b $WKGDIR/rootfs-complete -F $WKGDIR/kernels/$DIRKERNEL/System.map $USEKERNEL
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

#v404 for unipup, so it runs at bootup...
ln -sf bin/busybox rootfs-complete/init
mv -f rootfs-complete/etc/rc.d/rc.sysinit-UNIPUP rootfs-complete/etc/rc.d/rc.sysinit
mv -f rootfs-complete/etc/rc.d/rc.shutdown-UNIPUP rootfs-complete/etc/rc.d/rc.shutdown
mkdir rootfs-complete/sys
#tidy-up /etc/rc.d/ remove unused scripts...
rm -f rootfs-complete/etc/rc.d/BOOTCONFIG
rm -f rootfs-complete/etc/rc.d/rc.country-PUP*
rm -f rootfs-complete/etc/rc.d/rc.local0
rm -f rootfs-complete/etc/rc.d/rc.modem
rm -f rootfs-complete/etc/rc.d/rc.modules
rm -f rootfs-complete/etc/rc.d/rc.modules2
rm -f rootfs-complete/etc/rc.d/rc.update
rm -f rootfs-complete/etc/rc.d/README.txt
sync

##v404 nah, will do this in rc.sysinit...
##create list of all files (use when saving a session)...
#UNIDEV="`find rootfs-complete/dev | sed -e 's%^rootfs-complete/dev/%%'`"
#UNIBIN="`find rootfs-complete/bin | sed -e 's%^rootfs-complete/bin/%%'`"
#UNISBIN="`find rootfs-complete/sbin | sed -e 's%^rootfs-complete/sbin/%%'`"
#UNIETC="`find rootfs-complete/etc | sed -e 's%^rootfs-complete/etc/%%'`"
#UNIROOT="`find rootfs-complete/root | sed -e 's%^rootfs-complete/root/%%'`"
#UNILIB="`find rootfs-complete/lib | sed -e 's%^rootfs-complete/lib/%%'`"
#UNIOPT="`find rootfs-complete/opt | sed -e 's%^rootfs-complete/opt/%%'`"
##/usr is a layerfs in running puppy and files in usr_xxx.sfs can be determined at shutdown.
#echo "UNIDEV='${UNIDEV}'
#UNIBIN='${UNIBIN}'
#UNISBIN='${UNISBIN}'
#UNIETC='${UNIETC}'
#UNIROOT='${UNIROOT}'
#UNILIB='${UNILIB}'
#UNIOPT='${UNIOPT}'" > rootfs-complete/root/.packages/initrd.vars.files
#sync

#v404 convert everything in rootfs-complete/usr into a sfs...
echo
echo "Creating rootfs-complete/usr.sfs..."
./mksquashfs rootfs-complete/usr rootfs-complete/usr_$PUPPYVERSION.sfs
sync
rm -rf rootfs-complete/usr
mkdir rootfs-complete/usr
sync

echo
echo "usr_$PUPPYVERSION.sfs is currently inside rootfs-complete, so there will"
echo "only be one file, initrd.gz, which the kernel will load into RAM at bootup."
echo "However, you can move it out as a separate file, and at bootup Puppy will"
echo "find it and load it (currently without copying it to ram)."
echo "Press ENTER only to leave usr_$PUPPYVERSION.sfs where it is."
echo -n "Press any printable char then enter to move it out: "
read willmoveit
if [ "$willmoveit" != "" ];then
 echo "...ok, moved to isolinux-builds/"
 mv -f rootfs-complete/usr_$PUPPYVERSION.sfs isolinux-builds/
 sync
fi

#v404 compress some executables...
#these are used heavily in scripts and should nt be compressed...
SKIPEXE=' bash busybox bunzip2 bzip2 cat cp cpio cut date df-FULL find gawk grep gzip ln ls mv ps-FULL readlink rm rmdir sed sleep sort tar touch waitmax fdisk guess_fstype hdparm hwclock ifplugstatus ifplugstatus-0.28 lsmod modprobe.bin modprobe pcmcia-check-broken-cis pcmcia-socket-startup udevsettle udevtrigger udevadm bc cmp curl dialog diff dirname du eject file fuser less lzma lzmadec mktemp rename rev rxvt seq stat test time unzip aulchown buttondialog disktype dmidecode elspci get-edid gtkdialog2 gtkdialog3 lspci mut parse-edid puppyserialdetect Xdialog gtf xmessage yaf-splash '
echo
echo "UPX-compressing some executables..."
for ONEEXEC in `find rootfs-complete -type f -perm -o+x`
do
 [ `stat --format=%s $ONEEXEC` -lt 20000 ] && continue
 if [ "`file $ONEEXEC  | grep 'ELF 32-bit LSB executable'`" != "" ];then
  ONENAME="`basename $ONEEXEC`"
  onPATTERN=" $ONENAME "
  [ "`echo "$SKIPEXE" | grep "$onPATTERN"`" != "" ] && continue
  ONEPATH="`dirname $ONEEXEC`"
  cd $ONEPATH
  echo -n " $ONENAME"
  $WKGDIR/upx $ONENAME >/dev/null
  cd $WKGDIR
 fi
done
sync
echo

echo
echo "Converting rootfs-complete folder to initrd.gz..."
cd rootfs-complete
#find . | ../cpio -o -H newc | gzip -9 > ../isolinux-builds/initrd.gz
#v404 no, this results in much bigger initrd.gz (86MB)...
#find . | ../cpio -o -H newc | ../lzma e -si ../isolinux-builds/initrd.gz
#do it like this, size drops to 71MB...
find . | ../cpio -o -H newc > ../isolinux-builds/initrd
sync
cd ..
if [ "`echo "$NAMEKERNEL" | grep '\-RDLZMA'`" != "" ];then
 lzma e isolinux-builds/initrd isolinux-builds/initrd.gz
 sync
 rm -f isolinux-builds/initrd
else
 gzip isolinux-builds/initrd
 sync
fi

#v2.15 now create the isolinux.cfg file... v2.16rc modified...
echo "Creating isolinux-builds/isolinux.cfg..."
echo 'default puppy
display boot.msg
prompt 1
label puppy
kernel vmlinuz
append initrd=initrd.gz pmedia=cd
timeout 50' > isolinux-builds/isolinux.cfg
sync

#404
cp -af boot.msg.unipup 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/initrd.gz > /tmp/dvd-initrd.gz.chk
    [ -f /mnt/data/usr_$PUPPYVERSION.sfs ] && md5sum /mnt/data/usr_$PUPPYVERSION.sfs > /tmp/dvd-usr_$PUPPYVERSION.sfs.chk #v411
    sync
    md5sum ./isolinux-builds/vmlinuz > /tmp/vmlinuz.chk
    md5sum ./isolinux-builds/initrd.gz > /tmp/initrd.gz.chk
    [ -f ./isolinux-builds/usr_$PUPPYVERSION.sfs ] && md5sum ./isolinux-builds/usr_$PUPPYVERSION.sfs > /tmp/usr_$PUPPYVERSION.sfs.chk #v411
    sync
    umount /mnt/data
    CHKVMLINUZ="`cat /tmp/vmlinuz.chk | cut -f 1 -d ' '`"
    CHKINITRD="`cat /tmp/initrd.gz.chk | cut -f 1 -d ' '`"
    if [ -f /tmp/usr_$PUPPYVERSION.sfs.chk ];then #v411
     CHKUSRSFS="`cat /tmp/usr_$PUPPYVERSION.sfs.chk | cut -f 1 -d ' '`"
     [ ! "`cat /tmp/dvd-usr_$PUPPYVERSION.sfs.chk | cut -f 1 -d ' '`" = "$CHKUSRSFS" ] && echo "AAARGH! FAIL!"
    fi
    [ ! "`cat /tmp/dvd-vmlinuz.chk | cut -f 1 -d ' '`" = "$CHKVMLINUZ" ] && 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####
