#!/bin/bash

# initrd.brew
# by Stuart Winter <stuart@armedslack.org>
# See changelog.txt for changes.
#
# Questions you may have:
# Q. Why do I build shellutils & fileutils when I could just installpkg
#    a/coreutils-*.tgz ?
# A. Slackware's installer source uses shellutils & fileutils.  The binary sizes
#    (even when stripped) are considerably smaller than their coreutils counterparts.
# Q. Why do I use newer versions of some software than the Slackware installer?
# A. Because the Slackware installer has ancient versions which will not compile
#    for me and I don't see the point in making them build when I can use a newer
#    versions that work.  I just want to keep the installer initrd size down whilst
#    keeping a working installer :-)
# Q. Where is the real Slackware build script?
# A. There isn't one:
#    Patrick maintains a local tree of this stuff for Slackware and uses scripts
#    to re-create the initrd.img file from this tree.
#    This is how I have chosen to do it for ARMedslack.  Please send suggestions/bug fixes
#    if you can improve this script.
#
# This script:
# * Builds - busybox;
#          - elvistiny;  (although no longer included within the Slackware installer's initrd)
#          - diethotplug;
#          - fileutils;
#          - sh-utils;
#          - textutils;
#   Installs these into a temporary package directory on the filesystem
#
# * Takes Slackware's initrd.img file:
#          - copies it to a temporary place
#          - gunzips it
#          - mounts it loopback
#          - copies its contents to a temporary directory on the filesystem
#          - umounts it
#          - deletes the temporary gunzipped copy
#
# * Enters into the newly copied version of Slackware's initrd.img file:
#          - deletes x86 glibc 
#          - determines which files are arch-specific binaries and replaces them
#            with $PORTARCH versions from either the temporary package directory or the filesystem
#          - installs $PORTARCH glibc-solibs into this copy
#          - performs $PORTARCH-specific any modifications 
#
# * Creates a new ext2 filesystem image:
#          - determines how large the ext2 image needs to be in order to hold the
#            filesystem contents
#          - dd's a file of size determined by the above step
#          - mke2fs thatfile      # make ext2 filesystem on that file
#          - tune2fs -i0 thatfile # turn off mount counts
#          - mounts -oloop thatfile /someplace
#          - copies new initrd filesystem contents into new loopback mounted filesystem
#          - chroot /loopbackmountedfilesystem /sbin/ldconfig -r .
#          - umount /loopbackmountedfilesystem
#          - save new $PORTARCH version of the new initrd into somewhere ready for
#            inclusion into the distributed tree.
#
# # To do before installing this newly brewed initrd.img into the distributed tree:
#   - mount it loopback
#   - on the $PORTARCH host, chroot into it
#   - run the scripts and play around for a while to ensure that it works as it should
#   - boot into it on the $PORTARCH host system
#   - if it works as expected, insert this new version into the tree. 

### Functions ###

# Execute any post install scripts.  Typically these setup symlinks for
# libraries.
function run_doinstsh () {
( cd $TMPBUILD/extract-packages
  if [ -f install/doinst.sh ]; then
     echo -n "Please wait - running install script"
     ( bash install/doinst.sh ) > /dev/null 2>&1 
     echo " ... done"
  fi )
}

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/rootdisks
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete old cruft; create new temporary directories & finally cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="" ;;
   sparc)   export SLKCFLAGS="" ;;
   hppa)    export SLKCFLAGS="" ;;
   *)       export SLKCFLAGS="" ;;
esac

# Versions of software included:
BUSYBOX_VERSION=0.60.5
#BUSYBOX_VERSION=1.1.3
#BUSYBOX_VERSION=20061011
DIETHOTPLUG_VERSION=0.4
ELVISTINY_VERSION=1.4
FILEUTILS_VERSION=4.1  # Slackware uses v3.13
SHUTILS_VERSION=2.0.11 # Slackware uses v1.14
TEXTUTILS_VERSION=2.0  # Slackware uses v1.19

################################## Build busybox ########################################

echo "********* Building busybox *************"

# Extract source:
cd $TMPBUILD
#tar zxvvf $PORTCWD/sources/busybox*$BUSYBOX_VERSION*.tar.gz
tar jxvvf $CWD/busybox/busybox-$BUSYBOX_VERSION.tar.bz2
cd busybox*
slackhousekeeping

# Apply Slackware patches:
zcat $CWD/busybox/busybox-0.60.5.noisereduction.diff.gz | patch -p1 --verbose || failpatch
zcat $CWD/busybox/busybox-0.60.5.rootdisks.diff.gz      | patch -p1 --verbose || failpatch

# Copy config:
#install -m644 $PORTCWD/busybox-$BUSYBOX_VERSION-config .config

# busybox diff from Slack390:
cat $PORTCWD/sources/initrd.diff                        | patch -p1 --verbose || failpatch

# Build:
make CFLAGS_EXTRA="$SLKCFLAGS" || failmake
#make || failmake

# Install into package framework:
mkdir -p $PKG/busybox
make install PREFIX=$PKG/busybox

################################## Build fileutils ########################################

echo "********* Building fileutils *************"

# Extract source:
cd $TMPBUILD
tar zxvvf $PORTCWD/sources/fileutils_$FILEUTILS_VERSION.orig.tar.gz 
cd fileutils-*
slackhousekeeping
zcat $PORTCWD/sources/fileutils_$FILEUTILS_VERSION-*.diff.gz | patch -p1 --verbose || failpatch

# Create a separate build directory:
mkdir -p $ARCH-slackware-linux-build/man
( export CFLAGS="$SLKCFLAGS -I. -g"
  cd $ARCH-slackware-linux-build
  # Configure:
  ../configure \
     --build=$ARCH-slackware-linux \
     --host=$ARCH-slackware-linux \
     --prefix=/usr \
     --infodir=/junk \
     --mandir=/junk \
     -v || failconfig

  # Stop the man page section from fluffing up the build quite as much:
  install -m644 ../GNUmakefile .
  install -m644 ../man/GNUmakefile man/

  # Build: this will fail with some stuff about the manpages - it matters not.
  # the binaries get built.  If they don't then we'll find out about it later
  # after we've $PORTARCH-ified the Slackware initrd:
  make 

  # Install into temporary package directory inside its own fileutils directory
  # this is done so that none of its components clash with busybox's.  We'll sort
  # through what bits we include from which package later on.
  mkdir -p $PKG/fileutils
  make install DESTDIR=$PKG/fileutils ) 

################################## Build diethotplug ######################################

echo "********* Building diethotplug *************"

# Extract source:
cd $TMPBUILD
tar zxvvf $CWD/diethotplug-$DIETHOTPLUG_VERSION.tar.gz
cd diethotplug*
slackhousekeeping

# Build: but not for ARM. The RiscPC was chiseling its grave stone when hotplug
# was in nappies.
if [ $PORTARCH != arm ]; then
   make KERNEL_INCLUDE_DIR=/usr/src/linux/include || failmake

  # Install into diethotplug sub directory:
  mkdir -p $PKG/diethotplug/sbin
  install -m755 hotplug $PKG/diethotplug/sbin
fi

################################## Build textutils ########################################

echo "********* Building textutils *************"

# Extract source:
cd $TMPBUILD
tar zxvvf $PORTCWD/sources/textutils_$TEXTUTILS_VERSION.orig.tar.gz 
cd textutils-*
slackhousekeeping
zcat $PORTCWD/sources/textutils_$TEXTUTILS_VERSION-*.diff.gz | patch -p1 --verbose || failpatch

# Configure:
cp -fa /usr/share/automake*/config.guess .
cp -fa /usr/share/automake*/config.sub .
./configure \
   --prefix=/usr || failconfig

# Build:
make || failmake

# Install into the textutils sub directory:
mkdir -p $PKG/textutils
make install DESTDIR=$PKG/textutils

################################## Build shellutils ########################################

echo "********* Building shellutils *************"

# Extract source:
cd $TMPBUILD
tar zxvvf $PORTCWD/sources/shellutils_$SHUTILS_VERSION.orig.tar.gz 
cd shellutils-*
slackhousekeeping
zcat $PORTCWD/sources/shellutils_$SHELLUTILS_VERSION-*.diff.gz | patch -p1 --verbose || failpatch

# Patch for gcc 3.4 - clumsy but produces the desired result and saves me making a diff file:
sed -i -e 's?char.*malloc.*();? ?' lib/path-concat.c

# Create a separate build directory:
mkdir -p $ARCH-slackware-linux-build/man
( export CFLAGS="$SLKCFLAGS -g"
  cd $ARCH-slackware-linux-build
  # Configure:
  ../configure \
     --build=$ARCH-slackware-linux \
     --host=$ARCH-slackware-linux \
     --prefix=/usr \
     -v || failconfig

  # Stop the man page section from fluffing up the build quite as much:
  install -m644 ../GNUmakefile .
  install -m644 ../man/GNUmakefile man/

  # Build:
  make || failmake

  # Install into temporary package directory inside its own shellutils:
  mkdir -p $PKG/shellutils
  make install prefix=$PKG/shellutils )

################################## Build elvis-tiny ########################################

# Note: elvis-tiny isn't included within the Slackware installer anymore but the
#       source archive remains.  We'll build it anyway - maybe we'll want to include
#       it at a later date?
# Probably not since busybox has a good 'vi' built in.
#
#echo "********* Building elvis-tiny *************"

# Extract source:
#cd $TMPBUILD
#tar zxvvf $CWD/elvis-tiny_$ELVISTINY_VERSION.orig.tar.gz 
#cd elvis-tiny-*
#slackhousekeeping
#zcat $PORTCWD/sources/elvis-tiny_$ELVISTINY_VERSION-*.diff.gz | patch -p1 --verbose || failpatch

# Patch for gcc 3.4:
#bzcat $PORTCWD/sources/elvistiny.diff.bz2 | patch -p0 --verbose || failpatch

# Build:
#make CC="gcc -fsigned-char $SLKCFLAGS"
#gcc $SLKCFLAGS -s -o debian/wrapper debian/wrapper.c

# Install into sub directory within package:
#mkdir -p $PKG/elvis-tiny
#install -m755 elvis $PKG/elvis-tiny/elvis

################################## Build bash ########################################

# Slackware's installer uses 'ash' which is symlinked to busybox.
# However, I want to improve the speed of the installer by using pushd & popd
# in the installer scripts, so we'll use bash for this.

# Extract source:
cd $TMPBUILD
tar zxvvf $PORTCWD/sources/bash-3.1.tar.gz
cd bash-*
for patch in $PORTCWD/sources/bash-patches/bash* ; do
  zcat $patch | patch -p0 --verbose || failpatch
done

## Configure:
CFLAGS="-O2 $SLKCFLAGS" \
./configure \
   --enable-static-link \
   $ARCH-slackware-linux || failconfig

## Build:
make || failmake

## Install into the sub directory within the package:
strip --strip-unneeded bash
mkdir -p $PKG/bash-static
install -m755 bash $PKG/bash-static/

############################ Copy contents of Slackware's initrd  ##########################

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Copy the Slackware-current initrd into the temporary directory:
echo "************ Investigating the Slackware colour.gz initrd image ***********"
stat $SLACKSOURCE/../rootdisks/color.gz

# Create directories:
mkdir -p $TMPBUILD/mnt-initrd  # we will use this as the temporary mount point for color.gz
mkdir -p $PKG/$PORTARCH-initrd # temp dir into which we'll install our future image's contents

# Unpack the gzipped initrd so that we can mount it locally via loopback:
zcat $SLACKSOURCE/../rootdisks/color.gz > $TMPBUILD/slackware-colour.img

# Mount the Slackware initrd via loopback 
# Hopefully you have the loopback Kernel module loaded or compiled into the Kernel:
mount -oloop  $TMPBUILD/slackware-colour.img $TMPBUILD/mnt-initrd || { echo "Failed to loopback mount Slackware initrd" ; exit 99 ;}

# We copy the contents of the initrd locally so that we can replace the Intel/x86
# binaries with $PORTARCH versions.  There are a number of symlinks and shell scripts
# here and only a small number of binaries:
echo "Copying contents of real Slackware initrd into local version to be $PORTARCH-ified"
( cd $TMPBUILD/mnt-initrd && tar pcf - . | ( cd $PKG/$PORTARCH-initrd && tar pxvf - ) )

# umount the real Slackware initrd as we no longer need it:
umount $TMPBUILD/mnt-initrd 

############################ Build $PORTARCH version of initrd  ##########################

# Originally this was going to be 'intelligent' and find a list of Intel/x86 binaries
# and simply copy them off the filesystem.  However, it's not quite that simple since 
# there are various different versions of each binary (and we have coreutils on the filesystem
# rather than the old 'textutils', 'fileutils' and 'shellutils' stuff).
# Since there aren't many binaries, we'll copy them individually.
# In order to determine what has changed between versions of the initrd, please use the
# initrd.differ shell script provided in this directory.

# Note: at some point I may use installpkg to install various packages into a temp dir
#       and use their contents rather than copying directly off the root filesystem.
#       This enables you to have a box that is not entirely uptodate (for whatever reason)
#       but still have the latest versions of binaries in your port's initrd (assuming your
#       distributed tree contains the latest packages).  But I don't really see the point
#       right now -- my ARMedslack boxes are usually uptodate with the latest stuff.
#       If anybody wants then I'll update this script do this.
# *** for now just make sure you:
#     -  run this script on your $PORTARCH host otherwise you'll get
#        non $PORTARCH binaries in your initrd ;-)
#     - have a fully installed system - all packages ***

############### /bin #############################################################

# Copy $PORTARCH's busybox:
cd $PKG/$PORTARCH-initrd
install -m755 $PKG/busybox/bin/busybox bin/

# Temporary hacks: sleep in busybox is broken with linux 2.6.15
#cp -fa /bin/sleep  bin/
#cp -fa /usr/bin/strace bin/

# Copy binaries from the host's filesystem:
# need to work out which binaries these come from (just grep through /var/log/packages some time):
#
# 'moved to busybox' -- at one point I upgraded to a newer version of busybox and
# these marked utils had been implemented within busybox.
# For the version of busybox that Slackware uses, we copy the binaries off
# the filesystem.
cp -fa /bin/tar-1.13                bin/
cp -fa /bin/dialog                  bin/
cp -fa /usr/bin/eject               bin/ # moved to busybox
cp -fa /bin/grep                    bin/grep.bin
cp -fa /usr/bin/zgrep               bin/
cp -fa /bin/gzip                    bin/gzip.bin
cp -fa /bin/ipmask                  bin/
cp -fa /usr/bin/ldd                 bin/
cp -fa /bin/mount                   bin/ # moved to busybox
cp -fa /bin/umount                  bin/ # moved to busybox
cp -fa /bin/printf                  bin/ # moved to busybox
cp -fa /bin/setterm                 bin/
cp -fa /usr/bin/strings             bin/ # moved to busybox

# this is version 4.x of sed - hopefully it's compatible with the version 3.02 
# in the Slackware initrd :-P
cp -fa /bin/sed                     bin/ # moved to busybox
cp -fa /bin/ps                      bin/ # moved to busybox

# fileutils stuff built from this script:
cp -fa $PKG/fileutils/usr/bin/df    bin/df.bin
cp -fa $PKG/fileutils/usr/bin/ls    bin/ # moved to busybox

# textutils stuff built from this script:
cp -fa $PKG/textutils/usr/bin/paste bin/
cp -fa $PKG/textutils/usr/bin/comm  bin/
cp -fa $PKG/textutils/usr/bin/sort  bin/ # moved to busybox
cp -fa $PKG/textutils/usr/bin/cut   bin/ # moved to busybox

############### /sbin #############################################################

# diethotplug built from this script:
rm -f sbin/hotplug # wipe the intel version (ARMedslack doesn't have hotplug & we don't want an intel bin in our initrd)
cp -fa $PKG/diethotplug/sbin/hotplug sbin/

# Copy binaries from the host's filesystem:
cp -fa /sbin/badblocks              sbin/
cp -fa /sbin/blkid                  sbin/
cp -fa /usr/sbin/cfdisk             sbin/cfdisk.bin
cp -fa /usr/bin/chattr              sbin/ # moved to busybox
cp -fa /sbin/debugfs                sbin/
cp -fa /sbin/dumpe2fs               sbin/
cp -fa /sbin/e2fsck                 sbin/
cp -fa /sbin/fsck                   sbin/
cp -fa /sbin/e2image                sbin/
cp -fa /sbin/fdisk                  sbin/fdisk.bin
cp -fa /usr/sbin/hdparm             sbin/ # moved to busybox
# Wipe insmod.old
rm -f sbin/insmod*
cp -fa /sbin/lsmod                  sbin/
cp -fa /sbin/modprobe               sbin/
cp -fa /sbin/rmmod                  sbin/
cp -fa /sbin/insmod                 sbin/
cp -fa /sbin/jfs_mkfs               sbin/
cp -fa /usr/sbin/klogd              sbin/
cp -fa /sbin/logsave                sbin/
cp -fa /usr/bin/lsattr              sbin/ # moved to busybox
cp -fa /sbin/lsraid                 sbin/
cp -fa /sbin/mkdosfs                sbin/
cp -fa /sbin/mke2fs                 sbin/ # moved to busybox
cp -fa /sbin/mkfs.xfs               sbin/
cp -fa /sbin/mklost+found           sbin/
cp -fa /sbin/mkraid                 sbin/
cp -fa /sbin/mkreiserfs             sbin/
cp -fa /sbin/pivot_root             sbin/
cp -fa /sbin/raidreconf             sbin/
cp -fa /sbin/raidstart              sbin/
cp -fa /sbin/rdev                   sbin/
cp -fa /sbin/reiserfsck             sbin/
cp -fa /sbin/resize2fs              sbin/
cp -fa /sbin/rpc.portmap            sbin/
cp -fa /sbin/tune2fs                sbin/ # moved to busybox
cp -fa /usr/bin/uuidgen             sbin/

# don't need this for ARM- other arch's?
# cp -fa syslinux                   sbin/
if [ $PORTARCH = arm ]; then
   rm -f sbin/syslinux
   # We don't need the network script since all network hardware
   # is compiled into the Kernel.
   install -m755 -oroot -groot $PORTCWD/sources/network.sh bin/network
fi

############### ARM Specific binaries #####################################################

if [ "$PORTARCH" = "arm" ]; then
   install -pm755 $PORTCWD/sources/acorn-fdisk-wrapper sbin/
   cp -favv /sbin/acorn-fdisk    sbin/
   cp -favv /usr/sbin/partprobe  sbin/
   # This is also useful to have, especially for QEMU:
   cp -favv /sbin/dhcpcd         sbin/
   # We also use bash proper so we can use pushd & popd in the install scripts:
   ( cd bin
     rm -fv ash bash sh
     cp -favv $PKG/bash-static/bash .
     ln -vfs bash sh
     ln -vfs bash ash )
   # And let's set the standard PS1 prompt: "root@slackware:/# "
   # otherwise we get the line number appended to the PS1.
   echo "export PS1='\u@\h:\w# '" >> etc/profile

   # And we'll use the patched installpkg, too (uses pushd & popd):
   install -m755 /sbin/installpkg usr/lib/setup/installpkg   
fi

############### Libraries #################################################################

# Delete the x86/Intel '/lib' directory.  We do this because these library files are
# usually versioned and our $PORTARCH versions are not necessarily the same
# (since the initrd for Slackware is rarely updated, the port's versions are
# significantly more recent).
# I know in advance which files we want because I went to the effort of working it out ;-)
rm -rf lib/
mkdir -p lib

# We install the library packages and copy only the parts that we need.
# 
mkdir -p $TMPBUILD/extract-packages # temporary directory used to extract packages

# a/e2fsprogs:
installpkg -root $TMPBUILD/extract-packages $PKGSTORE/a/e2fsprogs-*.tgz
cp -fa $TMPBUILD/extract-packages/lib/*  lib/
rm -rf $TMPBUILD/extract-packages

# a/glibc-solibs:
mkdir -p $TMPBUILD/extract-packages
installpkg -root $TMPBUILD/extract-packages $PKGSTORE/a/glibc-solibs-*.tgz
cp -fa /lib/ld-linux*so* lib/
cp -fa $TMPBUILD/extract-packages/lib/*  lib/
mkdir -p usr/libexec
cp -fa $TMPBUILD/extract-packages/usr/libexec/pt_chown usr/libexec
rm -rf $TMPBUILD/extract-packages

# l/ncurses:
mkdir -p $TMPBUILD/extract-packages
installpkg -root $TMPBUILD/extract-packages $PKGSTORE/l/ncurses-*.tgz
cp -fa $TMPBUILD/extract-packages/lib/*  lib/
rm -rf $TMPBUILD/extract-packages

# a/procps:
mkdir -p $TMPBUILD/extract-packages
installpkg -root $TMPBUILD/extract-packages $PKGSTORE/a/procps-*.tgz
cp -fa $TMPBUILD/extract-packages/lib/*  lib/
rm -rf $TMPBUILD/extract-packages

# a/gpm:
mkdir -p $TMPBUILD/extract-packages
installpkg -root $TMPBUILD/extract-packages $PKGSTORE/a/gpm-*.tgz
cp -fa $TMPBUILD/extract-packages/lib/*  lib/
rm -rf $TMPBUILD/extract-packages

# Kernel modules:
# For the StrongARM RiscPC we do not have any Kernel Modules because all device
# drivers are compiled into the Kernel (there are so few!).  ARMedslack does ship
# an a/kernel-modules package though but this is mainly just to provide the
# directory structure as a place holder, and the modules.dep file.
#
# For other ARM machines, such as the Iyonix, which take PCI devices we'll
# want to look at this as and when Kernels become available.
# For now we'll make a holding directory:
mkdir -p lib/modules


# Apply ARMedslack patches to the installer scripts:
# Some of these should go upstream since they're generic - on to do list.
# 
# Modify /sbin/probe to use acorn-fdisk if it finds itself running on 'Acorn' branded
# hardware.  The reason for this is to fix the fdisk problem - see patches/fdisk-l-problem.txt
patch --verbose -p0 < $PORTCWD/patches/probe-fdisk.patch || failpatch
# Find whether swap is on under Linux 2.6. Also patch a bug/typo:
# Is now patched in a different way in Slackware - may need some intervention incase
# any ARM specific stuff rears its head.
# patch --verbose -p0 < $PORTCWD/patches/setswap.patch || failpatch
# We don't need to set a Kernel for ARMedslack on the Iyonix or RiscPC:
patch --verbose -p0 < $PORTCWD/patches/setkernel.exit || failpatch
# Fix a bug where the temporary tagfile created by the maketag scripts is copied but
# creates a blank file if the temporary 'tagfile' already exists. 
# Not sure where this bug comes from but it's not the script, but this fixes it anyway:
# ** Nope: inside busybox, cp -f /dev/null file copies the /dev/null special file
#          which it shouldn't do.  In the maketag files, I'd replaced some cat /dev/null with cp.
#          Now I FINALLY find a use for one of Pat's cats :)
# patch --verbose -p0 < slackinstall.wipetagfile.patch
# 

# Install easy NFS mount script for development:
install -m755 $PORTCWD/mountpris .

############### Set Slackware packaging policies ########################################

# Apply generic Slackware policies to the contents of the initrd.
# It's not a package but we may as well set the relevant policies anyway.

slackstripall   # strip all .a archives and all ELFs
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs

############### Create the new initrd  ##################################################

# Leave a note in the installer image to help us work out which build version
# it is -- just aids with bug reports for now:
cat << EOF > .installer-version
Version details:
----------------

Installer version: $SLACKWARE_INSTALLER_VERSION
Installer build..: $SLACKWARE_INSTALLER_BUILD
Build date.......: $( date )

Build details:
--------------
Installer arch...: $SLACKWARE_INSTALLER_ARCH
Build host.......: $( uname -a )

EOF

# Path to the newly created initrd:
#SLACKINITRD=$PKGSTORE/$PKGSERIES/color
# I don't like the idea of overwriting the original installer image with
# a newer one, so for this one we'll copy it into the source directory 
# ( e.g. 'armedslack-current/source/rootdisks' )
SLACKINITRD=$PORTCWD/color

# Determine the size of the installer contents and create an image file
# that is 12% larger.  For ARMedslack 11 our image size is about 7MB
# (glibc-2.3.6 is huge!).  The additional space is for creating directories
# and temporary files.
echo "Installer size: $( du -sh . )" 
DISK_SIZE=$( du -ks | cut -d. -f1 )
#PERCENT_INC="$( echo "( $DISK_SIZE / 100 ) * 12" | bc )"
# Why was I doing a percentage of that size? that's a weird way
# of doing it.  Let's just give it a fixed increase of a 3K -- which
# is enough for the installer's scratch space.
PERCENT_INC=3
let CREATE_SIZE="$DISK_SIZE+$PERCENT_INC"
#dd if=/dev/zero of=$SLACKINITRD bs=${CREATE_SIZE}k count=1
# This is done manually by assessing the size of the contents,
# mke2fsing the file; copying the contents to it (loop back mounted)
# and leaving as little as possible for scratch space.
#dd if=/dev/zero of=$SLACKINITRD bs=7900k count=1
dd if=/dev/zero of=$SLACKINITRD bs=9900k count=1
#dd if=/dev/zero of=$SLACKINITRD bs=9800k count=1

# Format the new image file with an ext2 filesystem:
mke2fs -m0 -b1024 -FL "$PORTARCH unofficial Slackware installer" $SLACKINITRD
# Prevent fsck checks when mounting it:
tune2fs -i0 $SLACKINITRD

# Mount the new image via loopback to a temporary directory:
mount -oloop $SLACKINITRD $TMPBUILD/mnt-initrd

# Copy the filesystem contents into our new initrd:
( cd $PKG/$PORTARCH-initrd && tar pcf - . | ( cd $TMPBUILD/mnt-initrd && tar pxvf - ) )
# This is the way to create a cpio version rather than loopback:
# find * | cpio -o -H newc > initrd 

# Create the ldconfig.so.cache:
cd $TMPBUILD/mnt-initrd
#cat << EOF > etc/ld.so.conf
#/lib
#/usr/lib
#EOF
ldconfig -r.

# Finally let's scan for any remaining x86 binaries we didn't overwrite:
slackx86

# umount the new initrd:
cd $PORTCWD
umount $TMPBUILD/mnt-initrd

# gzip the new initrd/installer to reduce size:
echo -n "gzipping the new initrd"
gzip -vf9 $SLACKINITRD
echo " ... done"

# Now you will loopback mount it and thoroughly check it :-)
echo "Your new installer image is ${SLACKINITRD}.gz"
echo "Now:"
echo " [1] Test it"
echo " [2] When done, mv -f color.gz ../../rootdisks/"

############################################################################################
