#!/bin/sh
#
# Build-Ch5-2.3.0.sh
#
# Build Pure LFS Chapter 5
# ------------------------
#
# Authors:  Ryan Oliver  (PHA)(ryan.oliver@pha.com.au)
#           Greg Schafer (ZIP)(gschafer@zip.com.au)
#
# Created:  2003-01-26
#
#                      =========================================================
# Modified: 2003-05-09 - Added calls to function remove_dir
#                        Enabled by specifying REMOVE_PKGDIR=Y in plfs-config
#                        Erik-Jan Post <ej.lfs@xs4all.nl>
#                      =========================================================
#           2003-05-05 - If using --enable-version-specific-runtime-libs, 
#                        a symlink is now created for the c++ includes in 
#                        /stage1/include ( /stage1/include/c++/${VER} )
#                      - create a default ${LFS}/etc/hosts containing localhost
#                        only if we are unable to locate the hosts one. 
#                        Normally the build hosts /etc/hosts file is copied in
#                      =========================================================
#           2003-05-03 - gcc mmap configure patch now only applied for
#                        gcc-3.2.x
#                      =========================================================
#           2003-05-01 - add -fPIC to CFLAGS for zlib build on alpha
#                        ( gp-relative relocation issue )
#                        Fix from "Kelledin's page of Linux-Alpha tweaks"
#                        http://skarpsey.dyndns.org/alpha-lfs/alpha.html
#                        Pointed out by Joachim Schmeling <schmeling@tiscali.dk>
#                      =========================================================
#           2003-04-27 - ${GCC_VER} cannot be trusted to supply the actual gcc
#                        version (especially for CVS builds).
#                        Where required the version is now taken from
#                        gcc/version.c
#                      =========================================================
#           2003-04-22 - Change hard coded '/usr/include' references in
#                        util-linux to point at '/stage1/include'
#                      - changes to glibc to cater for NPTL
#                        --with-tls option added if using NPTL
#                        From Zack Winkle's <winkie@linuxfromscratch.org> 
#                        NPTL hint 
#                        ( http://hints.linuxfromscratch.org/hints/nptl.txt )
#                      =========================================================
#           2003-04-20 - Add option to use NPTL
#                      - check for existence of linuxthreads dirs in glibc
#                        ( provided with CVS glibc )
#                        Linuxthreads is only installed if it is not present,
#                        or removed if present for NPTL builds
#                      - Alter kernel header installation to cater for 2.5
#                        series kernels 
#                        ( will run include/asm target if it exists instead of
#                          symlinks target )
#                      - gcc specfile location ${SPECFILE} is determined from 
#                        gcc -v throughout the build now.
#                      =========================================================
#           2003-04-19 - Change version number to 2.3.0
#                      - add a user to ${LFS}/etc/passwd and shadow with a valid
#                        shell for ch6 coreutils check-root tests 
#                        ( test/rm/fail-2eperm ).
#                        User is "plfstest", uid 1000, group 100 (users).
#                      - For consistency the "plfstest" user will be temporarily
#                        added for performing ch5 coreutils tests instead of 
#                        user "nobody"
#                      =========================================================
#           2003-04-14 - Change version number to 2.2.8
#                      - Move sort symlink to ${LFS}/usr/bin 
#                      - fix sed edit of zlib makefile to cater for 
#                        sed versions < 4.05 
#                        (earlier versions do not handle embedded \n as newline)
#                        Erik-Jan Post <ej.lfs@xs4all.nl>
#                      - linux-elf.h gcc config header requires editing for
#                        dynamic linker location on alpha targets
#                        Joachim Schmeling <schmeling@tiscali.dk>
#                      - add linux-elf.h header to list of gcc config headers 
#                        and modify gcc seds to nicely cater for the 
#                        above.
#                        ( Should by rights now also work with arm)
#                      - Run non-root as well as root tests for coreutils
#                        Non-root tests are run as "nobody". Also a temporary
#                        group "plfstest" is created and nobody added to it
#                        for the chgrp tests
#           2003-04-08 - Fix c'n'p brainfart with double copy of mount and 
#                        umount during util-linux build
#                        Matt Bartley <mbartley@exair.org>
#                      - Add sed to mimic the ncurses cursesw vsscanf patch
#                        (unsure who provided the original patch, please contact
#                         the authors so we can attribute you correctly) 
#                      =========================================================
#                      - Change version number to 2.2.7
#                      - add cmp symlink (reqd for ch6 ed) (EJP)
#                      - copy /etc/hosts over from the host
#                        reqd for ch6 perl checks (EJP)
#                      - create more along with mount/umount 
#                        ( for e2fsprogs )
#                        Erik-Jan Post <ej.lfs@xs4all.nl> (thanks again Erik)
#           2003-04-07 - Add a sed to modify zlib's Makefile.in to allow
#                        us to sensibly pass in LDFLAGS from the environment
#           2003-04-04 - Add sort symlink (for findutils ch6 tests)
#                      - Add "nobody" user entry into shadow file
#                      =========================================================
#           2003-04-02 - Change version number to 2.2.6
#                      - Fix bug with devfs detection
#                        Thomas Kalla <xTx@gmx.de>
#                      - Fix broken sed command for specfile edit (oops)
#                        Martin Imobersteg <imm@gmx.ch> 
#                      =========================================================
#           2003-04-02 - Change version number to 2.2.5
#                      - Revert FHS compliance changes from 2.2.4,
#                        Broke Std LFS and LFS CVS build instructions
#                        Bill Maltby <lfsbill@wlmcs.com>
#                      - Changes to specfile/linux.h edits to allow for
#                        --dynamic-linker being ld.so.1 on certain archs
#                        Florian Engelhardt <f.engelhardt@gmx.net>
#                      - Additional ${LFS}/bin symlinks added to support
#                        ch6 findutils locate tests 
#                        (specifically updatedb barfs w/o these)
#                      - Chroot section at end of script commented out.
#                        If you do choose to use this ensure you check that it
#                        is suitable for you LFS build tree (works for me YMMV)
#                      =========================================================
#           2003-03-31 - Change version number to 2.2.4
#                        
#                      Following from Nicolas Roeser <n-roeser@gmx.net>
#                      (with some modifications), Thanks Nico
#                      - Replace user name "root" by number 0, because we can't
#                        be sure that the root user is called "root" on the host
#                        system. The name of the (first) root user is put into
#                        ROOTUSER .
#                      - Do not try to chown build tree if we are not building
#                        as a root user. build-ch6-toolchain-2.2.3.sh will take
#                        of this fact for us and do an extra chown.
#                      - When creating directories, take care of the fact that
#                        some of the *parent* directories may not exist
#                      - Get rid of the /usr/{doc,info,man} symlinks, increase
#                        FHS compliance of the directory structure
#                      - Modify /etc/passwd and /etc/group contents; now there
#                        is a user "nobody", a group "nogroup" and a "wheel"
#                        group
#                      - Handle broken permissions etc. in host system when
#                        copying files from there to our new system
#                      =========================================================
#           2003-03-28 - Change version number to 2.2.3
#                      - Fix output logging for bzip2
#                        Erik-Jan Post <ej.lfs@xs4all.nl>
#                      - Add support for powerpc 
#                        (ARCH set to rs6000 during gcc init build when powerpc)
#                        Florian Engelhardt <F.Engelhardt@gmx.net>
#                      =========================================================
#           2003-03-27 - Change version number to 2.2.2
#                      - Add '--cache-file=/dev/null' to expect configure to 
#                        avoid issues with Makefile creation for its testsuite
#                        Steve Martin <srm@netcomuk.co.uk>
#                      - Fix bug in logging for installation of ldscripts
#                        Matt Bartley <mbartley@exair.org>
#                      =========================================================
#           2003-03-24 - build a slightly bigger perl to run 'make check' in ch6
#                        without errors. perl installed now instead of miniperl
#                        Erik-Jan Post <ej.lfs@xs4all.nl>
#                      - Install expect regardless if tests pass or fail
#                      - Apply Kelledin's <kelledin+LFS@skarpsey.dyndns.org>
#                        vsnprintf patch to zlib.
#                        See mail-archives/lfs-dev/2003/02/0925.html for info.
#                        Patch at mail-archives/lfs-dev/2003/02/0930.html
#                        Bill Maltby <lfsbill@wlmcs.com>
#                      - fix naming of gcc-3.2.2.mmap_test patch (had -mmap :-/)
#                        Archaic <archaic@indy.rr.com>
#                      =========================================================
#           2003-03-22 - Change version number to 2.2.2-beta1
#                      - add check for existence of log directories before 
#                        their creation. Matt Bartley <mbartley@exair.org>
#                      - remove debug symbols from libgcc/libstdc++
#                        (seds mimic GS's patch, works for all gcc-3.2 versions)
#                      - hardcode filename for mmap test patch 
#                        (works for all gcc-3.2 versions, 3.2.2 latest to date) 
#                      - Move creation of utmp/wtmp/lastlog/btmp before ch6
#                        build. (GS)
#                      =========================================================
#           2003-03-18 - Change version number to 2.2.1
#                      - Remove gcc/configure hack. Now uses patch
#                        gcc-3.2.2.mmap_test.patch
#                        Greg Schafer and
#                        Donald Smith <dss-lfs@cfl.rr.com>
#           2003-03-17 - Add the option of using coreutils instead of
#                        fileutils, sh-utils and textutils.
#                        Set USE_COREUTILS=Y in build-init-2.2.1.sh 
#                      - Add the option to skip bootstrap of gcc during the
#                        second build.
#                        Set NOBOOTSTRAP=Y in build-init-2.2.1.sh
#                      - Add gcc/configure hack to attempt to avoid race
#                        conditions in mmap tests when ld.so.cache
#                        < 1 page (GS)
#           2003-03-15 - Numerous bugfixes
#           2003-03-11 - unpack_tarball ( build-init-2.2.1.sh ) now
#                        a) checks the tarball to determine where it will 
#                           unpack the package source to. Returns this as the
#                           environment var PKGDIR
#                        b) removes the target directory ( ${PKGDIR} ) before
#                           unpacking the tarball
#                        PKGDIR is now used throughout the script(s) when 
#                        cd'ing into the packages source tree.
#                      - Flex
#                        Now checks Makefile for a 'make bigcheck' target.
#                        If not, make check is performed instead 
#                        ( flex 2.5.27 doesn't have bigcheck target )
#                      - The devfs check now uses df -T /dev and greps for
#                        devfs, as opposed to checking for /dev/.devfsd.
#                        If successful it
#                        a) creates the device nodes ${LFS}/dev/console 
#                           (setting group ownership to 'tty') and
#                           ${LFS}/dev/null
#                        then
#                        b) mounts /dev over the top of ${LFS}/dev
#                      - Fix log directory creation to use what is specified in
#                        the *LOGS variables (AP)
#                      - Take ryan out of the root group in /etc/group ;-) (AP)
#                        Ainsley Pereira <marble@pebble.org.uk> (Thanks Ainsley)
#                      - move TZ and LC_ALL defs to build-init-2.2.1.sh
#                      =========================================================
#           2003-03-10 - fix bug in check for /dev/.devfsd 
#                        ( is char dev, not file )
#                      - add ${LFS}/bin/install symlink to 
#                        ${LFS}/usr/bin/install (BM)
#                      - fix bug with mount of /dev (BM)
#                        Bill Maltby (lfsbill@wlmcs.com)
#                      =========================================================
#           2003-03-08 - Change version number to 2.2.0
#                      - Move M4 build to before bison
#                      - Add some logging to check_binutils function 
#                        (see build-init-2.2.0.sh)
#                      - fix bug in binutils init build
#                      - fix bug with bzip2 log creation (forgot to specify LOG)
#                      =========================================================
#           2003-03-07 - Change version number to 2.1.0
#                      - Create universal binutils build. Should handle both FSF
#                        and HJL binutils.
#                      - check_binutils () (see build-init-2.1.0.sh)
#                        The check_binutils function is called during the first
#                        binutils build to determine cetain features, and if 
#                        necessary patch ld/Makefile.in.
#                      - Binutils init build
#                        The build now adjusts itself depending on
#                         o whether a 'configure-host' target exists in Makefile
#                         o whether '--with-lib-path=' exists in ld/configure.
#                        This should hopefully cover all eventualities
#                        with binutils development for a while
#                      =========================================================
#           2003-03-07 - Change version number to 2.0.0
#                      - Common functions between both ch5 and 6 scripts, along
#                        with the package version definitions, have been moved
#                        to the script 'build-init.sh'
#                        This is sourced after the definition of LFS and SCRIPTS
#                      =========================================================
#           2003-03-03 - Change version number to 1.3.0
#                      - Updated to glibc 2.3.2
#                      =========================================================
#                      - Change version number to 1.2.0
#                      - Gcc 
#                      - install-no-fixedincludes no longer used,
#                        reverting back to nofixincludes patch functionality
#                        ( Provided by a grep -Ev statement )
#                      - Changing of NATIVE_SYSTEM_HEADER_DIR now redundant
#                        as fixinc.sh script no longer runs.
#                      - no longer require separate install if libstc++-v3
#                        headers
#                      - remove '-v' from all calls to ldconfig 
#                      =========================================================
#                      - Change version number to 1.1.4
#                      - added definition for SCRIPTS, points at directory
#                        containing the buildscripts.
#                      - Add check_patches ( modified from check_patch supplied
#                        by Ronald Hummelink )
#                      - Add apply_patch for smart patch application
#                        Ronald Hummelink (ronald@hummelink.xs4all.nl) 
#                      =========================================================
#                      - Change version number to 1.1.3
#                      - Automate check_tarballs function.
#                      - check_tarballs loops through this list, expanding the 
#                        package names as it goes with the gen_pkname function,
#                        and checking for its existence in ${TARBALLS}
#                      =========================================================
#           2003-03-01 - Change version number to 1.1.2
#                      - Logging Enhancements.
#                      - code minimised between max_log_init and min_log_init.
#                      - max_log_init sets LOGFNAME ( passed log with appended
#                        timestamp  eg gcc-init.log-yyyymmdd )
#                      - min_log_init now only takes the log directory as an 
#                        argument. Sets LOGFILE (prepends the log directory
#                        onto LOGFNAME) 
#                      - LOGFILE now used throughout the script as the sole
#                        logging target.
#                      - Remove duplicate bash/sh/perl symlink creation
#                      =========================================================
#                      - Change version number to 1.1.1
#                      - Creation of yacc script only occurs now if the version
#                        of bison used does not.
#                      =========================================================
#           2003-02-28 - Change version number to 1.1.0
#                      - CXXFLAGS="-O2 -pipe" added to gcc (shared) configure
#                      - CFLAGS="-O2 -pipe" dropped from tcl build as it does
#                        not achieve anything ( -pipe already gets set and the
#                        optimisation gets overriden ).
#                        Also --disable-symbols dropped as it is the default.
#                      - remove unneeded LDFLAGS from ldscript rebuilds
#                        ( we only use the rebuilt ldscripts )
#                      - comment version numbers for tcl, expect and dejagnu 
#                      =========================================================
#                      - Change version number to 1.0.1
#                      - fix script bug, missing 'fi' after gcc make check info
#                      =========================================================
#                      - Change version number to 1.0.0
#                      - Minor changes to logging
#                      - Move log directory creation after check_tarball/patches
#                      =========================================================
#           2003-02-27 - Added VERSION var to script
#                        Added check_tarball routine
#                        Added check_patch routine
#                        Move export of Tcl/Expat/Dejagnu version to beginning
#                        Ronald Hummelink (ronald@hummelink.xs4all.nl) 
#           2003-02-26 - Add --enable-shared to shared binutils build
#                      - Move creation of ${LFS}/bin/sh and
#                        ${LFS}/usr/bin/perl symlinks to end of script
#                        Were getting set before the directories were
#                        created (Thanks again Bill)
#           2003-02-25 - Set shell to utilize glob style matches
#                        (unpack_tarball was broken)
#                      - Fix some logging output
#                      - minor bugfixes
#           2003-02-23 - Add creation of nsswitch.conf and the
#                        copying of the referenced files (services etc)
#                      - Add tgz support to unpack_tarball, bugfixes
#                        and fix some more of the logging stuff
#                        Bill Maltby (lfsbill@wlmcs.com) (Thanks Bill)
#           2003-02-22 - Bugfix to script (typo unpacking bison)
#                      - fix some logging
#           2003-02-21 - Fix chmod to make grep tests executable
#                      - Fix kernel header installation for ch6
#                        ( Thanks Jeroen )
#                      - Add support for sparc (UNTESTED)
#                      - allow for 32 and 64 bit dynamic linker when creating 
#                        or altering gcc's specfile. 
#                        *** NOTE: 64 bit hasn't been tested yet, YMMV ***
#           2003-02-20 - Add tarball unpacking function
#                      - Added --with-included-regex to grep configure
#                      - Added Perl (sed mimics current patch)
#           2003-02-13 - Add std log init functions, replace log init
#                        code with new. Shorten logging append code.
#                        Bill Maltby (lfsbill@wlmcs.com)
#           2003-02-12 - Returned findutils to the build
#                        (reqd for ch6 kernel headers)
#                      - Addition of a testing packages section (for ch6),
#                        currently incomplete 
#                        (only tcl is present at the moment).
#                      - Now, installs kernel headers in /stage1/include and in 
#                        final location ${LFS}/usr/include
#           2003-02-11 - bugfixes to script
#                      - Addition of rebuilding ldscripts during second
#                        binutils build for use in ch6
#           2003-02-10 - bugfixes to script
#                      - remove -s from LDFLAGS for zlib (breaks build)
#                      - Updated to (mostly) reflect current state of hint.
#           2003-02-06 - Revert to original toolchain build order
#           2003-02-05 - Added Logging, numerous enhancements (see hint)
#
# NOTE: This script deviates slightly from the hint, mainly with the use of
#       nls and compilation of c++.
#
# Modified from
# buildToolchain.sh
# Ryan Oliver (PHA), 2003-01-16
# (ryan.oliver@pha.com.au)
#
# Builds a complete toolchain for compiling and
# linking against a different glibc version.
# Then builds packages required for our final Ch6
# build
#

# Don't just run this script blindly.
# You will have to adjust the package version numbers to 
# suit what you are installing.

# Set SELF to be name of script called, minus any path...
SELF=$(basename ${0})
echo "Running ${SELF}"
VERSION="2.3.0"
DATE=$(date +'%Y%m%d')

# Set LFS to suit your build tree
export LFS=/lfs

# Set SRC for your sourcedir, TARBALLS for where you store
#  your tarballs

export SRC=${LFS}/usr/src
export TARBALLS=${LFS}/usr/tarballs
export PATCHES=${LFS}/usr/patches
export SCRIPTS=${LFS}/usr/scripts

# Logging
# Set for base of logging tree
export LOGS=${LFS}

export CONFLOGS=${LOGS}/conflogs
export BUILDLOGS=${LOGS}/buildlogs
export INSTLOGS=${LOGS}/instlogs
export TESTLOGS=${LOGS}/testlogs

# Source Functions and definitions
. ${SCRIPTS}/build-init-2.3.0.sh

set +x
unset LD_LIBRARY_PATH
unset LD_PRELOAD

export LDFLAGS="-s"

# Find out the name of the (first) user with UID 0
#  (necessary because some people like to rename "root")
#
# Set this manually if you want your specific root user and not the first one in
#  the passwd file (e.g. because you only know *your* "root" password and not
#  the other "root"s' ;-) )
ROOTUSER=`grep "^[^:]*:[^:]*:0:" /etc/passwd | head -n1 | cut -d: -f1`

export OLDPATH=${PATH}
export PATH=/stage1/bin:${PATH}

# If stage 1 directory doesn't exist, create it
test -d ${LFS}/stage1 || mkdir -p ${LFS}/stage1

# If stage1 symlink doesn't exist, create it
test -L /stage1 || ln -sf ${LFS}/stage1 /

echo '##### Pure-LFS chapter 5 builder version: '${VERSION}
echo ' o Checking for needed sources and tarballs'

check_tarballs
check_patches

mkdir -p ${CONFLOGS}
mkdir -p ${BUILDLOGS}
mkdir -p ${INSTLOGS}
mkdir -p ${TESTLOGS}

#
# Binutils pass 1 - static
# Will be using latest binutils for creating glibc
# ( Keeps configure script happy )
#

# TODO: set ARCH so we can check for architecture specific patches
#       implement patches in "check_binutils" for
#       alpha binutils patch (need to check if reqd for HJLA

# For want of a better way
machine=`uname -m`
# add entries for architectures that need binutils patches
# ( we'll set ARCH later after gcc build )
case ${machine} in
   alpha* )
      ARCH="alpha"
   ;;
esac

BUILD_OK="${REMOVE_PKGDIR}"
BIN_BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG="binutils-static.log"

unpack_tarball binutils-${BINUTILS_VER} &&

# Determine whether this binutils requires
# 'make configure-host' and whether
# ld search path can be set with --with-lib-path.
# Returned in ${BINUTILS_CONF_HOST} and
# ${BINUTILS_WITH_LIB_PATH} respectively

# TODO: check for architecture specific patches in check_binutils
#       ( alpha )
check_binutils

test -d binutils-${BINUTILS_VER}-static-build && 
   rm -rf binutils-${BINUTILS_VER}-static-build

mkdir binutils-${BINUTILS_VER}-static-build
cd binutils-${BINUTILS_VER}-static-build

max_log_init Binutils ${BINUTILS_VER} Static ${CONFLOGS} ${LOG}
../${PKGDIR}/configure --prefix=/stage1 --disable-nls \
   >> ${LOGFILE} 2>&1 &&
{
   test Y != "${BINUTILS_CONF_HOST}" ||
   {  
      echo -e "\nmake configure-host\n${BRKLN}" >> ${LOGFILE}
      make configure-host >> ${LOGFILE} 2>&1 
   }
} &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-all-static -s" \
   >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
   >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}1" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

#
# Binutils pass 1 - Rebuild ldscripts
#
# Here we rebuild ld for the sole purpose
# of regenerating the linker scripts, setting
# LIB_PATH=/stage1/lib, limiting ld's default
# search order
# This will be installed later
#

LOG="binutils-static-ldscripts.log"
max_log_init Binutils ${BINUTILS_VER} "Rebuild ldscripts" ${CONFLOGS} ${LOG}
cd ld &&
make clean > /dev/null 2>&1 &&
make LIB_PATH=/stage1/lib \
   >> ${LOGFILE} 2>&1 &&

BIN_BUILD_OK="${BUILD_OK}2" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

#----------------------------------------------------
#
# Gcc build
#

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG="gcc-static.log"

unpack_tarball gcc-${GCC_VER} 
cd ${PKGDIR}

#3.0 20030427
# Cannot trust ${GCC_VER} to supply us with the correct
# gcc version (especially if cvs).
# Grab it straight from version.c
actual_gcc_ver=`grep version_string gcc/version.c | \
                sed 's@.* = "\([0-9.]*\).*@\1@g'`
# Since we are going to rebuild this later, may as well export actual_gcc_ver
export actual_gcc_ver

case ${actual_gcc_ver} in
   3.2* )
      # Fix mmap test for race conditions when ld.so.cache < 1 page
      # patch applies to all gcc-3.2 versions (20030322 - latest is 3.2.2)
      apply_patch gcc-3.2.2.mmap_test
   ;;
esac


cd ${SRC}/${PKGDIR}/gcc
test -f Makefile.in-ORIG ||
   cp Makefile.in Makefile.in-ORIG

# Following mimics the nofixincludes patch.
# Also avoid debug symbols in libgcc2
grep -Ev '(README| ./fixinc.sh )' Makefile.in-ORIG | \
sed 's@LIBGCC2_DEBUG_CFLAGS = -g@LIBGCC2_DEBUG_CFLAGS =@g' \
   > Makefile.in

mkdir ${SRC}/gcc-${GCC_VER}-static-build &&
cd ${SRC}/gcc-${GCC_VER}-static-build &&

max_log_init Gcc ${GCC_VER} Static ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ../${PKGDIR}/configure --prefix=/stage1 \
       --with-local-prefix=/stage1 --enable-languages=c \
       --disable-nls --enable-shared \
       >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make BOOT_LDFLAGS="-static -s" BOOT_CFLAGS="-O2 -pipe" \
       STAGE1_CFLAGS="-pipe" bootstrap \
       >> ${LOGFILE} 2>&1 &&
BUILD_OK="${BUILD_OK}1" && 
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make -k check \
      >>  ${LOGFILE} 2>&1
test 0 = "${?}" && {
   echo " o Test OK"
   BUILD_OK="${BUILD_OK}Y" 
} || {
   echo " o Test NOT OK, check ${LOGFILE} for details"
   BUILD_OK="${BUILD_OK}${REMOVE_PKGDIR_CH5_GCC_STATIC}" 
}

echo "Installing Gcc" &&
min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}3" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

# Added force to ln -s to make sure we get it done.
ln -sf gcc /stage1/bin/cc

cd ${SRC}
test Y1Y3 = "${BUILD_OK}" && {
   remove_dir ${SRC}/gcc-${GCC_VER}-static-build
   remove_dir ${SRC}/gcc-${GCC_VER}
}

#------------------------------------------
#
# Kernel Headers
# Now install kernel headers into /stage1/include
#

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=kernel-headers.log
unpack_tarball linux-${KERNEL_VER} &&
ln -sf ${PKGDIR} linux 

cd ${SRC}/linux
max_log_init kernel-headers ${KERNEL_VER} '' ${INSTLOGS} ${LOG}
make mrproper >> ${LOGFILE} 2>&1 &&

# 2.5 series kernels dont have "make symlinks" target but use
# "make include/asm" 
# Check for the include/asm target and use it if its available
   
make include/linux/version.h &&
grep "^include/asm:" Makefile > /dev/null 2>&1 &&
{
   make include/asm
} || {
   make symlinks 
} >> ${LOGFILE} 2>&1 &&

# Install kernel headers in both
#  /stage1/include ( for ch5 )
#   and
#  ${LFS}/usr/include ( for ch6 )

{
for destdir in /stage1 ${LFS}/usr; do
   mkdir -p ${destdir}/include/asm
   yes | cp include/asm/* ${destdir}/include/asm
   yes | cp -R include/asm-generic ${destdir}/include
   yes | cp -R include/linux ${destdir}/include
   touch ${destdir}/include/linux/autoconf.h
done
}  >> ${LOGFILE} 2>&1 &&

cd ${SRC} &&
test Y = "${REMOVE_PKGDIR_CH5_KERNEL}" && 
   remove_dir ${SRC}/${PKGDIR}

#
# Glibc
#
# Need to specify to configure the location of our new binutils
# ( avoid issues with GAS_HIDDEN )
#

cd ${SRC}

BUILD_OK="${REMOVE_PKGDIR}"

LOG="glibc-init.log"

unpack_tarball glibc-${GLIBC_VER}
cd ${PKGDIR}
OLDPKGDIR=${PKGDIR}

#3.0 - from ch6-toolchain 20030420
test "Y" = "${USE_NPTL}" &&
{
   # remove linuxthreads dirs if they exist
   # (CVS source contains linuxthreads)
   test -d linuxthreads && 
      rm -rf linuxthreads*
   unpack_tarball nptl-${NPTL_VER}
   extra_conf_options="--with-tls"
} || {
   # if it doesn't exist, unpack linuxthreads
   test -d ./linuxthreads &&
   test -d ./linuxthreads_db ||
   unpack_tarball glibc-linuxthreads-${GLIBC_VER}
}
PKGDIR=${OLDPKGDIR}

# create ld.so.conf for new glibc
mkdir -p /stage1/etc
touch /stage1/etc/ld.so.conf

mkdir ${SRC}/glibc-${GLIBC_VER}-init-build
cd ${SRC}/glibc-${GLIBC_VER}-init-build

max_log_init Glibc ${GLIBC_VER} initial ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ../${PKGDIR}/configure --prefix=/stage1 \
   --disable-profile --enable-add-ons \
   --with-headers=/stage1/include ${extra_conf_options} \
   --with-binutils=/stage1/bin \
   --without-gd \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make \
   >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&
BUILD_OK="${BUILD_OK}1" && 

min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1

########################################################################
# Break the conditional execution to let me continue
########################################################################
if [ $? -eq 0 ] ; then
    BUILD_OK="${BUILD_OK}Y" && 
    echo " o Test OK"
else
    BUILD_OK="${BUILD_OK}${REMOVE_PKGDIR_CH5_GLIBC}" && 
    echo "XXXXXX glibc make check NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"
fi

min_log_init ${INSTLOGS} &&
make install \
   >> ${LOGFILE} 2>&1 &&
echo " o install OK" &&

make localedata/install-locales \
   >>  ${LOGFILE} 2>&1 &&
echo " o install-locales OK" &&

BUILD_OK="${BUILD_OK}3" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

# clean up temp var
unset extra_conf_options 

cd ${SRC}
test Y1Y3 = "${BUILD_OK}" && {
   remove_dir ${SRC}/glibc-${GLIBC_VER}-init-build
   remove_dir ${SRC}/glibc-${GLIBC_VER}
}

#
# Binutils pass 1 - Final
#
# Here we install the rebuilt ldscripts.
# Binaries and libraries built with this linker will
# link against libs in /stage1/lib (location of our new glibc)
# 
#

cd ${SRC}
LOG="binutils-static-ldscripts.log"
cd binutils-${BINUTILS_VER}-static-build
cd ld
max_log_init Binutils ${BINUTILS_VER} "Static Pass 2 - installing linker scripts" \
   ${INSTLOGS} ${LOG}

make install-data-local \
   >> ${LOGFILE} 2>&1 &&

BIN_BUILD_OK="${BIN_BUILD_OK}3" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd ${SRC}
test Y123 = "$BIN_BUILD_OK" && {
   remove_dir ${SRC}/binutils-${BINUTILS_VER}-static-build
   remove_dir ${SRC}/binutils-${BINUTILS_VER}
   }

# Change dynamic linker definition in gcc specs file
# to point at our new dynamic linker in /stage1/lib.
#

TGT_TRIPLE=`/stage1/bin/gcc -dumpmachine`
SPECFILE=`/stage1/bin/gcc -v 2>&1 | grep specs | sed 's@.* @@'`
cp ${SPECFILE} ./XX
sed -e 's@/lib/ld-linux.so.2@/stage1/lib/ld-linux.so.2@g' \
    -e 's@/lib/ld.so.1@/stage1/lib/ld.so.1@g' \
    -e 's@/lib64/ld-linux.so.2@/stage1/lib64/ld-linux.so.2@g' \
    -e 's@/lib64/ld.so.1@/stage1/lib64/ld.so.1@g' ./XX > ${SPECFILE}
rm -f ./XX

# Set ARCH so we can script the config/${ARCH}/linux.h alterations
CPU=`echo ${TGT_TRIPLE} | awk -F\- '{print $1}'`

case ${CPU} in
   i?86 )
      ARCH=i386
   ;;

   *sparc* )
      ARCH=sparc
   ;;

   powerpc )
      ARCH=rs6000
   ;;

   alpha* )
      echo "Additional work may be required in this script for alpha..."
      echo "Please let the authors know of any architecture dependant mods you require"
      echo "( hardware donations also accepted ;-) )"
      ARCH=alpha
   ;;

   * )
      echo "${CPU} hasn't been included in this script yet..."
      echo "Please update me, and dont forget to contact the authors with your changes :-)" 
   ;;
esac

#
# Gcc build
# Now we build cc and c++ which use our new glibc
# ( when linking will use /stage1/ld, will use
#   /stage1/lib/ld-linux.so.2 as its dynamic linker
#  and will search /stage1/include first for headers )
#
# Gcc will bootstrap itself built against our new glibc.

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=gcc-init.log

unpack_tarball gcc-${GCC_VER} &&
cd ${PKGDIR}

#3.0 20030427
# Cannot trust ${GCC_VER} to supply us with the correct
# gcc version (especially if cvs).
# Grab it straight from version.c
actual_gcc_ver=`grep version_string gcc/version.c | \
                sed 's@.* = "\([0-9.]*\).*@\1@g'`

# Check if mmap configure patch is required
case ${actual_gcc_ver} in
   3.2* )
      # Fix mmap test for race conditions when ld.so.cache < 1 page
      # patch applies to all gcc-3.2 versions (20030322 - latest is 3.2.2)
      apply_patch gcc-3.2.2.mmap_test
   ;;
esac

cd ${SRC}/${PKGDIR}/gcc

# Dont run fixincludes 
# ( Following mimics the nofixincludes patch )
# Also avoid debug symbols in libgcc2

test -f Makefile.in-ORIG ||
   cp Makefile.in Makefile.in-ORIG

grep -Ev '(README| ./fixinc.sh )' Makefile.in-ORIG | \
sed 's@LIBGCC2_DEBUG_CFLAGS = -g@LIBGCC2_DEBUG_CFLAGS =@g' \
   > Makefile.in

# Avoid debug symbols in libstc++
cd ${SRC}/${PKGDIR}/libstdc++-v3

test -f configure-ORIG ||
   cp configure configure-ORIG

sed s@DEBUG_FLAGS=\'-g\'@DEBUG_FLAGS=\'\'@g configure-ORIG > configure

# We want our new gcc to use our new dynamic linker
# ( /stage1/lib/ld-linux.so.2 ) so we will need to modify
# gcc/config/$ARCH/linux.h and/or gcc/config/$ARCH/linux64.h 
# (where $ARCH is cpu-series type)
#
# LINK_SPEC needs tp get defined so that
#  -dynamic-linker is /stage1/lib/ld-linux.so.2 or /stage1/lib64/ld-linux.so.2
#
# This sets --dynamic-linker correctly in the generated spec file

cd ${SRC}/${PKGDIR}/gcc/config/${ARCH}
for hfile in linux.h linux64.h linux-elf.h; do
   if [ -f ${hfile} ]; then
      if [ ! -f ${hfile}-ORIG ]; then
         cp ${hfile} ${hfile}-ORIG
      fi

      sed -e 's@/lib/ld-linux.so.2@/stage1&@g' \
          -e 's@/lib/ld.so.1@/stage1&@g' \
          -e 's@/lib64/ld-linux.so.2@/stage1&@g' \
          -e 's@/lib64/ld.so.1@/stage1&@g' ${hfile}-ORIG > ${hfile}
   fi
done

cd ${SRC}

test -d gcc-${GCC_VER}-init-build && rm -rf gcc-${GCC_VER}-init-build

mkdir gcc-${GCC_VER}-init-build &&
cd gcc-${GCC_VER}-init-build &&

max_log_init Gcc ${GCC_VER} "initial (shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" CXXFLAGS="-O2 -pipe" \
   ../${PKGDIR}/configure --prefix=/stage1 --with-local-prefix=/stage1 \
   --enable-languages=c,c++ --enable-shared --enable-threads=posix \
   --enable-__cxa_atexit \
   --enable-version-specific-runtime-libs \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
test Y = "${NOBOOTSTRAP}" &&
{
   make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1
} || {
   make BOOT_LDFLAGS="-s" BOOT_CFLAGS="-O2 -pipe" \
      STAGE1_CFLAGS="-pipe" bootstrap \
      >> ${LOGFILE} 2>&1 
} &&
echo " o Build OK" &&
BUILD_OK="${BUILD_OK}1" &&

min_log_init ${TESTLOGS} &&
make -k check \
   >>  ${LOGFILE} 2>&1

########################################################################
# Break the conditional execution to let me continue
########################################################################
if [ $? -eq 0 ] ; then
    BUILD_OK="${BUILD_OK}Y" &&
    echo " o Test OK"
else
    BUILD_OK="${BUILD_OK}${REMOVE_PKGDIR_CH5_GCC_SHARED}" 
    echo \
"=========================================================================
 gcc make check NOT OK
-----------------------
Versions 3.2, 3.2.1 and 3.2.2.

This is not as bad as it looks, this is expected.

Please check ${LOGFILE} and compare against the following.

libstdc++
---------
All of the above versions will fail libstc++v3 tests due to
26 unexpected passes and a single failure.

FAIL: 26_numerics/c99_classification_macros_c.cc (test for excess errors)

                === libstdc++-v3 Summary ===

# of expected passes            432
# of unexpected failures        1
# of unexpected successes       26

Of real interest though is gcc and g++ tests.

gcc
---
Gcc tests SHOULD pass, no unexpected failures or passes.

g++
---
G++ will have one unexpected pass
   XPASS: g++.other/init5.C  Execution test

This is so far fairly consistent across the 3.2 series

========================================================================="
fi

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}3" &&
echo " o install OK" &&

test -L /stage1/bin/cc || ln -s gcc /stage1/bin/cc

# Following req'd when using --enable-version-specific-runtime-libs
# Add the path to our new gcc libs in /stage1/etc/ld.so.conf
SPECFILE=`/stage1/bin/gcc -v 2>&1 | grep specs | sed 's@.* @@'`
echo "${SPECFILE}" | sed 's@/specs@@' >> /stage1/etc/ld.so.conf
# Better make a symlink for the c++ includes
mkdir /stage1/include/c++
ln -s `echo "${SPECFILE}" | sed 's@/specs@/include/c++@'` /stage1/include/c++/${actual_gcc_ver}

# update /stage1/etc/ld.so.cache
/stage1/sbin/ldconfig 

cd ${SRC}
test Y1Y3 = "${BUILD_OK}" && {
   remove_dir ${SRC}/gcc-${GCC_VER}-init-build
   remove_dir ${SRC}/gcc-${GCC_VER}
}

#
# Binutils pass 2 - build all against new glibc
#
# Now for our final step, we will rebuild binutils from our
# shiny new toolchain ( will utilise /stage1/bin/gcc ),
# so it too is built against the latest glibc
#
# Again we will be setting LIB_PATH in ld/Makefile to "/stage1/lib"
#

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG="binutils-init.log"

# Clear out the existing binutils source tree and
# re-apply ld/Makefile.in changes if necessary.
unpack_tarball binutils-${BINUTILS_VER} &&
# TODO: check for architecture specific patches in check_binutils
#       ( alpha )
check_binutils

mkdir binutils-${BINUTILS_VER}-init-build
cd binutils-${BINUTILS_VER}-init-build

# If this binutils has the --with-lib-path configure
# option we wil use it
test Y == ${BINUTILS_WITH_LIB_PATH} &&
   BINUTILS_LIB_PATH="--with-lib-path=/stage1/lib" ||
   BINUTILS_LIB_PATH=""

max_log_init Binutils ${BINUTILS_VER} "initial (shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ../${PKGDIR}/configure --prefix=/stage1 \
   --enable-shared ${BINUTILS_LIB_PATH} \
   >> ${LOGFILE} 2>&1 && 

{
   # If this version of binutils requires make configure-host,
   # run it.
   test Y != "${BINUTILS_CONF_HOST}" ||
   {  
      echo -e "\nmake configure-host\n${BRKLN}" >> ${LOGFILE}
      make configure-host >> ${LOGFILE} 2>&1 
   } 
} &&
echo " o Configure OK" &&

{
   # If there is no --with-lib-path option we have to edit the
   # ld Makefile and manually set LIB_PATH
   test Y == ${BINUTILS_WITH_LIB_PATH} ||
   {
      test ! -f ld/Makefile-ORIG &&
         mv ld/Makefile ld/Makefile-ORIG
      # Set LIB_PATH
      sed 's@^LIB_PATH = @LIB_PATH = /stage1/lib@g' ld/Makefile-ORIG \
         > ld/Makefile
   }
} &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
   >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make -k check \
   >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
   >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}1" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"


#
# Rebuild the ldscripts for installation during ch6
#
LOG="binutils-init-ldscripts.log"
max_log_init Binutils ${BINUTILS_VER} "Rebuild ldscripts (for ch6)" \
  ${CONFLOGS} ${LOG}
cd ld &&
make clean > /dev/null 2>&1 &&
#rm -f Makefile &&
#cp Makefile-ORIG Makefile &&
make LIB_PATH=/lib:/usr/lib \
   >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}2" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd ${SRC}

# save BUILD_OK to a tempfile so binutils buildstatus survives the chroot
echo "${BUILD_OK}" > build_ok.tmp

# Now all we do is set PATH to have /stage1/bin and /stage1/sbin
# and anything we compile will be built against our new glibc from
# our complete toolchain

unset LD_LIBRARY_PATH
unset LD_PRELOAD
export PATH=/stage1/bin:/stage1/sbin:${OLDPATH}

# Job Done :-)
#EOF
#=====================================================================

### PACKAGES ###
ldconfig

### ZLIB ###
# export ZLIB_VER=1.1.4

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=zlib-init.log
unpack_tarball zlib-${ZLIB_VER}
cd ${PKGDIR}

# Fix up zlibs Makefile so we can properly pass in LDFLAGS from
# the env without clobbering existing settings for zlib build
test -f Makefile.in-ORIG ||
   cp Makefile.in Makefile.in-ORIG

# make LDFLAGS in Makefile.in empty and move existing LDFLAGS
# to ZLIB_LDFLAGS. Append $(ZLIB_LDFLAGS) wherever $(LDFLAGS) exists

# Following only works for sed vers 4.05 or later
#sed -e 's@LDFLAGS=.*@LDFLAGS=\nZLIB_&@' \
#    -e 's@$(LDFLAGS)@& $(ZLIB_LDFLAGS)@g' \
#    Makefile.in-ORIG > Makefile.in

sed -e 's@LDFLAGS=.*@ZLIB_&@' \
    -e '/ZLIB_LDFLAGS=/i\
LDFLAGS=' \
    -e 's@$(LDFLAGS)@& $(ZLIB_LDFLAGS)@g' \
    Makefile.in-ORIG > Makefile.in

# Apply Kelledin's vsnprintf patch
# see http://archive.linuxfromscratch.org/lfs-dev/2003/02/..... 
max_log_init Zlib ${ZLIB_VER} "initial (shared)" ${CONFLOGS} ${LOG}

#2.9
# Check to see if we are on alpha
# Req's a fix as specified on Kelledins Alpha page
# Pointed out by J.Schmelling
test "alpha" = "${ARCH}" &&
   extra_cflags="-fPIC"

test "1.1.4" = "${ZLIB_VER}" &&
{
   #TODO update patch to new one
   #apply_patch zlib-${ZLIB_VER}-2-vsnprintf
   apply_patch zlib-${ZLIB_VER}-final-vsnprintf
   CFLAGS="-O2 -pipe ${extra_cflags}" CPPFLAGS="-DHAS_vsnprintf" ./configure --prefix=/stage1 --shared \
      >> ${LOGFILE} 2>&1 
} || {
   CFLAGS="-O2 -pipe ${extra_cflags}" ./configure --prefix=/stage1 --shared \
      >> ${LOGFILE} 2>&1 
}
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make test \
   >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

unset extra_cflags

#mkdir -p /stage1/share/man/man3
#cp zlib.3 /stage1/share/man/man3
ldconfig

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### GAWK ###

cd ${SRC}
# export GAWK_VER=3.1.1

BUILD_OK="${REMOVE_PKGDIR}"

LOG=gawk-init.log
unpack_tarball gawk-${GAWK_VER} &&
cd ${PKGDIR}

max_log_init Gawk ${GAWK_VER} "initial (shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

BUILD_OK="${REMOVE_PKGDIR}"

# If we are using coreutils, dont install fileutils
test Y = "${USE_COREUTILS}" &&
{
   ### COREUTILS ###
   cd ${SRC}
   LOG=coreutils-init.log
   unpack_tarball coreutils-${COREUTILS_VER}
   cd ${PKGDIR}

   max_log_init Coreutils initial "(shared)" ${CONFLOGS} ${LOG}
   CFLAGS="-O2 -pipe" ./configure --prefix=/stage1  \
      >> ${LOGFILE} 2>&1 &&
   echo " o Configure OK" &&

   min_log_init ${BUILDLOGS} &&
   make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
   echo " o Build OK" &&

   min_log_init ${TESTLOGS} &&
   echo -e "\n Root Tests \n------------" >> ${LOGFILE} &&
   make check-root \
      >>  ${LOGFILE} 2>&1 &&
   
   BUILD_OK="${BUILD_OK}1" && 
   echo " o Root Tests OK" &&

   # Will be running user tests as "plfstest"
   # This user is temporary, and will be created with group "users".
   # Its home directory will _NOT_ be created.
   useradd -g users -c "plfstest user" -s /bin/bash -M \
      -d /home/plfstest plfstest
   # For chgrp tests the user needs to be in more than one group.
   # we will temporarily add the group "plfstest" and add "plfstest" to it
   groupadd plfstest &&
   usermod -G plfstest plfstest &&

   echo -e "\n User Tests \n------------" >> ${LOGFILE} &&
   su plfstest -c "env RUN_EXPENSIVE_TESTS=yes make check" \
      >>  ${LOGFILE} 2>&1 &&

   BUILD_OK="${BUILD_OK}2" && 
   echo " o User Tests OK" || 
	echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

   # Remove plfstest group
   groupdel plfstest 
   # Remove plfstest user
   userdel plfstest

   min_log_init ${INSTLOGS} &&
   make install \
      >> ${LOGFILE} 2>&1 &&
   
   BUILD_OK="${BUILD_OK}3" && 
   echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"
   
   cd ${SRC}
   test Y123 = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

} || {
   ### FILEUTILS ###
   # export FILEUTILS_VER=4.1
   cd ${SRC}
   LOG=fileutils-init.log
   unpack_tarball fileutils-${FILEUTILS_VER}
   cd ${PKGDIR}

   max_log_init Fileutils initial "(shared)" ${CONFLOGS} ${LOG}
   CFLAGS="-O2 -pipe" ./configure --prefix=/stage1  \
      >> ${LOGFILE} 2>&1 &&
   echo " o Configure OK" &&

   min_log_init ${BUILDLOGS} &&
   make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
   echo " o Build OK" &&

   min_log_init ${TESTLOGS} &&
   make check \
      >>  ${LOGFILE} 2>&1 &&
   echo " o Test OK" &&

   min_log_init ${INSTLOGS} &&
   make install \
      >> ${LOGFILE} 2>&1 &&

   BUILD_OK="${BUILD_OK}Y" && 
   echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"
   
   cd ${SRC}
   test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

}

### BZIP2 ###
# export BZIP2_VER=1.0.2

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=bzip2-init.log
unpack_tarball bzip2-${BZIP2_VER} &&
cd ${PKGDIR}

max_log_init Bzip2 ${BZIP2_VER} "initial (shared)" ${BUILDLOGS} ${LOG}
make -f Makefile-libbz2_so \
      >> ${LOGFILE} 2>&1 &&
echo -e "\n${BRKLN}" >> ${LOGFILE} &&
make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make PREFIX=/stage1 install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

yes | cp bzip2-shared /stage1/bin/bzip2
ln -s libbz2.so.1.0 libbz2.so
cp -a libbz2.so* /stage1/lib
rm -f /stage1/bin/{bunzip2,bzcat}
ln -s bzip2 /stage1/bin/bunzip2
ln -s bzip2 /stage1/bin/bzcat
ldconfig

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### GZIP ###
# export GZIP_VER=1.3.3

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=gzip-init.log
unpack_tarball gzip-${GZIP_VER} &&
cd ${PKGDIR}

max_log_init Gzip initial "(shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make check \
      >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

rm -f /stage1/bin/{gunzip,zcat}
ln -s gzip /stage1/bin/gunzip
ln -s gzip /stage1/bin/zcat
ln -s gunzip /stage1/bin/uncompress

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### DIFFUTILS ###
# export DIFFUTILS_VER=2.8.1

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=diffutils-init.log
unpack_tarball diffutils-${DIFFUTILS_VER} &&
cd ${PKGDIR}

max_log_init Diffutils initial "(shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### FINDUTILS ###
# export FINDUTILS_VER=4.1

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=findutils-init.log

unpack_tarball findutils-${FINDUTILS_VER} &&
cd ${PKGDIR}
test 4.1 = "${FINDUTILS_VER}" &&
   apply_patch findutils-${FINDUTILS_VER}

max_log_init Findutils ${FINDUTILS_VER} "initial (shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1 

if [ 0 -eq $? ] ; then
   echo " o Test OK" 
   BUILD_OK="${BUILD_OK}Y" 
else
   echo "XXXXXX Test NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"
   BUILD_OK="${BUILD_OK}${REMOVE_PKGDIR_CH5_FINDUTILS}" 
fi

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}2" &&
echo " o install OK" || echo "XXXXXX Install NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd ${SRC}
test YY2 = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### M4 ###
# export M4_VER=1.4

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=m4-init.log
unpack_tarball m4-${M4_VER} &&
cd ${PKGDIR} 

max_log_init M4 ${M4_VER} "initial (shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make  LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### BISON ###
# export BISON_VER=1.875

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=bison-init.log
unpack_tarball bison-${BISON_VER} &&
cd ${PKGDIR}

max_log_init Bison ${BISON_VER} "initial (shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

test -f /stage1/bin/yacc ||
   cat > /stage1/bin/yacc << "EOF"
#!/bin/sh
# Begin /stage1/bin/yacc

exec /stage1/bin/bison -y "$@"

# End /stage1/bin/yacc
EOF

chmod 755 /stage1/bin/yacc

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### FLEX ###
# export FLEX_VER=2.5.4a

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=flex-init.build
unpack_tarball flex-${FLEX_VER} &&
cd ${PKGDIR}

max_log_init Flex ${FLEX_VER} "initial (shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

# flex 2.5.27 does not have make bigcheck target,
# build will now double check the Makefile and adjust itself
# accordingly

min_log_init ${TESTLOGS} &&
grep "^bigcheck:" Makefile &&
{
   make bigcheck \
      >>  ${LOGFILE} 2>&1
} || {
   make check \
      >>  ${LOGFILE} 2>&1
} &&

echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"


cat > /stage1/bin/lex << "EOF"
#!/bin/sh
# Begin /stage1/bin/lex

exec /stage1/bin/flex -l "$@"

# End /stage1/bin/lex
EOF
chmod 755 /stage1/bin/lex

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### MAKE ###
# export MAKE_VER=3.80

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=make-init.log
unpack_tarball make-${MAKE_VER} &&
cd ${PKGDIR}

max_log_init Make ${MAKE_VER} "initial (shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

# chgrp 0 /stage1/bin/make &&
chmod 755 /stage1/bin/make

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### GREP ###
# export GREP_VER=2.5

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=grep-init.log
unpack_tarball grep-${GREP_VER} &&
cd ${PKGDIR}

max_log_init Grep ${GREP_VER} "initial (shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 --disable-perl-regexp \
      --with-included-regex \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

# Make Grep tests executable
chmod 750 ./tests/* &&
min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1 

# May fails 2 tests...
if [ $? -eq 0 ] ; then
    BUILD_OK="${BUILD_OK}Y" && 
    echo " o Test OK"
else
    BUILD_OK="${BUILD_OK}${REMOVE_PKGDIR_CH5_GREP}" && 
    echo "XXXXXX grep make check NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"
fi

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}2" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd ${SRC}
test YY2 = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### SED ###
# export SED_VER=4.0.5

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=sed-init.build
unpack_tarball sed-${SED_VER} &&
cd ${PKGDIR}

max_log_init Sed ${SED_VER} "initial (shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### GETTEXT ###
# export GETTEXT_VER=0.11.5

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=gettext-init.log
unpack_tarball gettext-${GETTEXT_VER} &&
cd ${PKGDIR}

max_log_init Gettext initial "(shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1 

########################################################################
# Break the conditional execution to let me continue
########################################################################
if [ $? -eq 0 ] ; then
    BUILD_OK="${BUILD_OK}Y" && 
    echo " o Test OK"
else
    BUILD_OK="${BUILD_OK}${REMOVE_PKGDIR_CH5_GETTEXT}" && 
    echo "XXXXXX gettext make check NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"
fi

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}2" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd ${SRC}
test YY2 = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

test Y = "${USE_COREUTILS}" ||
{
   ### TEXTUTILS ###

   BUILD_OK="${REMOVE_PKGDIR}"

   cd ${SRC}
   LOG=textutils-init.log
   unpack_tarball textutils-${TEXTUTILS_VER} &&
   cd ${PKGDIR}

   max_log_init Textutils initial "(shared)" ${CONFLOGS} ${LOG}
   CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 \
      >> ${LOGFILE} 2>&1 &&
   echo " o Configure OK" &&

   min_log_init ${BUILDLOGS} &&
   make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
   echo " o Build OK" &&

   min_log_init ${TESTLOGS} &&
   make check \
      >>  ${LOGFILE} 2>&1 &&
   echo " o Test OK" &&

   min_log_init ${INSTLOGS} &&
   make install \
      >> ${LOGFILE} 2>&1 &&

   BUILD_OK="${BUILD_OK}Y" && 
   echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

   cd ${SRC}
   test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

   ### SHUTILS ###

   BUILD_OK="${REMOVE_PKGDIR}"

   cd ${SRC}
   LOG=sh-utils-init.log
   unpack_tarball sh-utils-${SHUTILS_VER} &&
   cd ${PKGDIR}

   max_log_init Sh-utils ${SHUTILS_VER} "initial (shared)" ${CONFLOGS} ${LOG}
   CFLAGS="-O2 -pipe" ./configure --host=${TGT_TRIPLE} --prefix=/stage1 \
      >> ${LOGFILE} 2>&1 &&
   echo " o Configure OK" &&

   min_log_init ${BUILDLOGS} &&
   make LDFLAGS="-s"  \
      >> ${LOGFILE} 2>&1 &&
   echo " o Build OK" &&

   min_log_init ${TESTLOGS} &&
   make check \
      >>  ${LOGFILE} 2>&1 &&
   echo " o Test OK" &&

   min_log_init ${INSTLOGS} &&
   make install \
      >> ${LOGFILE} 2>&1 &&
   BUILD_OK="${BUILD_OK}Y" && 
   echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"
   
   cd ${SRC}
   test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

}

### NCURSES ###

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=ncurse-init.log

unpack_tarball ncurses-${NCURSES_VER} &&
cd ${PKGDIR}

# replace some deprecated headers
# strstream deprecated in favour of sstream
cd c++
test -f cursesw.h-ORIG ||
   mv cursesw.h cursesw.h-ORIG
sed 's/include <strstream.h>/include <sstream>/g' ./cursesw.h-ORIG > ./cursesw.h

# Apply cursesw.cc vsscanf patch
# Unsure who provided the original patch used.
# Please contact the authors so we can attribute you correctly.

test -f ./cursesiw.cc-ORIG ||
   cp cursesw.cc cursesw.cc-ORIG
grep -v strstreambuf cursesw.cc-ORIG |
sed 's@ss.vscan(@::vsscanf(buf, @' > cursesw.cc

cd ..

max_log_init Ncurses ${NCURSES_VER} "initial (shared)" ${CONFLOGS} ${LOG}
./configure --prefix=/stage1 --with-shared \
   --without-debug \
   >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make \
   >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${INSTLOGS} &&
make install \
   >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

chmod 755 /stage1/lib/*.${NCURSES_VER} &&
ln -s libncurses.a /stage1/lib/libcurses.a
ln -sf /stage1/lib/libncurses.so.5 /stage1/lib/libcurses.so
ldconfig

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### PATCH ###

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=patch-init.log
unpack_tarball patch-${PATCH_VER}
cd ${PKGDIR}

max_log_init Patch ${PATCH_VER} "initial (shared)" ${CONFLOGS} ${LOG}
CPPFLAGS=-D_GNU_SOURCE \
CFLAGS="-O2 -pipe" \
   ./configure --prefix=/stage1 \
   >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
   >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### TAR ###

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=tar-init.log
unpack_tarball tar-${TAR_VER}
cd ${PKGDIR}

test 1.13 = "${TAR_VER}" &&
   apply_patch tar-${TAR_VER}

max_log_init Tar ${TAR_VER} "initial (shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
   >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### TEXINFO ###

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=texinfo-init.log
unpack_tarball texinfo-${TEXINFO_VER} &&
cd ${PKGDIR}

max_log_init Texinfo initial "(shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 \
   >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
   >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make check \
   >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
      >> ${LOGFILE} 2>&1 &&
make TEXMF=/stage1/share/texmf install-tex \
      >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

## BASH ###

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=bash-init.log
unpack_tarball bash-${BASH_VER} &&
cd ${PKGDIR}

max_log_init Bash ${BASH_VER} "initial (shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure --prefix=/stage1 --with-curses \
   >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make LDFLAGS="-s" \
   >> ${LOGFILE} 2>&1 &&
echo " o Build OK" &&

min_log_init ${TESTLOGS} &&
make tests \
   >>  ${LOGFILE} 2>&1 &&
echo " o Test OK" &&

min_log_init ${INSTLOGS} &&
make install \
   >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd /stage1/bin
ln -s /stage1/bin/bash /stage1/bin/sh

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

### UTIL-LINUX ###

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=util-linux-init.log
unpack_tarball util-linux-${UTILLINUX_VER} &&
cd ${PKGDIR}

echo " o Changing hard coded references to /usr/include to point at /stage1"
flist=`grep -d recurse -l /usr/include *`
for file in ${flist}; do
   (
   echo " - editing ${file}"
   test -f ${file}-ORIG || cp ${file} ${file}-ORIG
   sed 's@/usr/include@/stage1/include@g' ${file}-ORIG > ${file}
   )
done

max_log_init Util-linux ${UTILLINUX_VER} "initial (shared)" ${CONFLOGS} ${LOG}
CFLAGS="-O2 -pipe" ./configure \
      >> ${LOGFILE} 2>&1 &&
echo " o Configure OK" &&

min_log_init ${BUILDLOGS} &&
make -C lib \
      >> ${LOGFILE} 2>&1 &&
echo " o Build lib - OK" &&
make -C mount LDFLAGS=-static mount umount \
      >> ${LOGFILE} 2>&1 &&
echo " o Build mount/umount - OK" &&
make -C text-utils LDFLAGS=-static more \
      >> ${LOGFILE} 2>&1 &&
echo " o Build more - OK" &&

cp mount/{mount,umount} /stage1/bin &&
cp text-utils/more /stage1/bin &&

BUILD_OK="${BUILD_OK}Y" &&
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

#
# Perl (reqd by glibc)
# We install miniperl to facilitate the make check of ch6 glibc
#

BUILD_OK="${REMOVE_PKGDIR}"

cd ${SRC}
LOG=perl-miniperl.log
unpack_tarball perl-${PERL_VER} &&
cd ${PKGDIR}

# Following implements the perl patch with seds
cd hints
if [ ! -f linux.sh-ORIG ]; then
   cp linux.sh linux.sh-ORIG
fi

chmod u+w linux.sh          # For those not running as a root user

sed 's@/lib/libc.so.6@/stage1/lib/libc.so.6@g' linux.sh-ORIG | \
sed 's@libc=/lib/\$libc@libc=/stage1/lib/\$libc@g' > linux.sh
echo "locincpth=\"\"
loclibpth=\"\"
glibpth=\"/stage1/lib\"
static_ext=\"IO re Fcntl\"" >> linux.sh

cd ${SRC}/${PKGDIR}

max_log_init Perl ${PERL_VER} "miniperl only" ${CONFLOGS} ${LOG}
./configure.gnu --prefix=/stage1 -Doptimize='-O2 -pipe' \
      >> ${LOGFILE} 2>&1 &&
min_log_init ${BUILDLOGS} &&
make perl \
      >> ${LOGFILE} 2>&1 &&
echo -e "${BRKLN}\n" >> ${LOGFILE} &&
make utilities \
      >> ${LOGFILE} 2>&1 &&
min_log_init ${INSTLOGS} &&
{
   cp perl /stage1/bin/perl &&
   cp pod/pod2man /stage1/bin &&
   mkdir -p /stage1/lib/perl5/${PERL_VER} &&
   cp -R lib/* /stage1/lib/perl5/${PERL_VER}
}  >> ${LOGFILE} 2>&1 &&

BUILD_OK="${BUILD_OK}Y" && 
echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

cd ${SRC}
test YY = "${BUILD_OK}" && remove_dir ${SRC}/${PKGDIR}

#
# Optional ancillary test packages 
#

if [ "X${BUILD_TEST_PKGS}" = "XY" ]; then

   # Tcl ( Reqd for expect )
   #

   BUILD_OK="${REMOVE_PKGDIR}"

   cd ${SRC}
   LOG=tcl-init.log
   unpack_tarball tcl${TCL_VER}-src &&
   cd ${PKGDIR}/unix

   max_log_init Tcl ${TCL_VER} "initial (shared)" ${CONFLOGS} ${LOG}
   ./configure --prefix=/stage1 \
      >> ${LOGFILE} 2>&1 &&
   echo " o Configure OK" &&

   min_log_init ${BUILDLOGS} &&
   make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
   echo " o Build OK" &&

   min_log_init ${TESTLOGS} &&
   make test \
      >>  ${LOGFILE} 2>&1 &&
   echo " o Test OK" &&
   # clock test may fail... not a real concern for our purposes...

   min_log_init ${INSTLOGS} &&
   make install \
      >> ${LOGFILE} 2>&1 &&

   BUILD_OK="${BUILD_OK}1" && 
   echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

   # Create symlink for tclsh
   TCLSH=$(basename $(find /stage1/bin -type f -name tclsh\*))
   test "tclsh" != "${TCLSH}" &&
      ln -s ./${TCLSH} /stage1/bin/tclsh
   cd ${SRC}

   #
   # Expect ( Reqd for Dejagnu )
   #
   cd ${SRC}
   LOG=expect-init.log
   unpack_tarball expect-${EXPECT_VER} &&
   cd ${PKGDIR}
   apply_patch expect-${EXPECT_VER}

   max_log_init Expect ${EXPECT_VER} "initial (shared)" ${CONFLOGS} ${LOG}
   CFLAGS="-O2 -pipe" \
      ./configure --prefix=/stage1 --with-tcl=/stage1/lib \
         --with-x=no --disable-symbols \
         --cache-file=/dev/null \
         >> ${LOGFILE} 2>&1 &&
   echo " o Configure OK" &&

   min_log_init ${BUILDLOGS} &&
   make LDFLAGS="-s" \
      >> ${LOGFILE} 2>&1 &&
   echo " o Build OK" &&

   min_log_init ${TESTLOGS} &&
   make test \
      >>  ${LOGFILE} 2>&1 
   # 1 test may fail... hopefully not a real concern...

   test 0 = ${?} &&
   {
      BUILD_OK="${BUILD_OK}Y" && 
      echo " o Test OK" 
   } || {
      BUILD_OK="${BUILD_OK}${REMOVE_PKGDIR_CH5_EXPECT}" && 
      echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"
   }

   min_log_init ${INSTLOGS} &&
   make install \
      >> ${LOGFILE} 2>&1 &&

   BUILD_OK="${BUILD_OK}3" && 
   echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

   #
   # Dejagnu
   #
   cd ${SRC}
   LOG=dejagnu-init.log
   unpack_tarball dejagnu-${DEJAGNU_VER} &&
   cd ${PKGDIR}

   max_log_init Dejagnu ${DEJAGNU_VER} "initial (shared)" ${CONFLOGS} ${LOG}
   ./configure --prefix=/stage1 \
         >> ${LOGFILE} 2>&1 &&
   echo " o Configure OK" &&

   min_log_init ${BUILDLOGS} &&
   make  \
         >> ${LOGFILE} 2>&1 &&
   echo " o Build OK" &&

   min_log_init ${INSTLOGS} &&
   make install \
         >> ${LOGFILE} 2>&1 &&

   BUILD_OK="${BUILD_OK}4" && 
   echo " o ALL OK" || echo "XXXXXX NOT OK - CHECK LOGS (${LOGFILE}) XXXXXX"

   cd ${SRC}
   test Y1Y34 = "${BUILD_OK}" && {
      remove_dir ${SRC}/tcl${TCL_VER}
      remove_dir ${SRC}/expect-${EXPECT_VER}
      remove_dir ${SRC}/dejagnu-${DEJAGNU_VER}
   }

fi

### Final Prep for chroot ###
chown -R root:root ${LFS}/stage1

mkdir -p ${LFS}/{bin,boot,dev/pts,etc/opt,home,lib,mnt,proc}
mkdir -p ${LFS}/{root,sbin,tmp,usr/local,var,opt}

for dirname in ${LFS}/usr ${LFS}/usr/local
    do

    # Apparently not FHS compliant but without these LFS breaks (RO)
    mkdir -p $dirname/{bin,etc,include,lib,sbin,share,src}
    ln -sf share/{man,doc,info} $dirname

    # More FHS compliant but breaks LFS builds
    #mkdir -p $dirname/{bin,include,lib,sbin,share,src}

    mkdir -p $dirname/share/{dict,doc,info,locale,man}
    mkdir -p $dirname/share/{nls,misc,terminfo,zoneinfo}
    mkdir -p $dirname/share/man/man{1,2,3,4,5,6,7,8}
done
mkdir -p ${LFS}/usr/local/games
ln -sf share/man ${LFS}/usr/local
mkdir -p ${LFS}/var/{lock,log,mail,run,spool} &&
mkdir -p ${LFS}/var/{tmp,opt,cache,lib/misc,local} &&
mkdir -p ${LFS}/opt/{bin,doc,include,info} &&
mkdir -p ${LFS}/opt/{lib,man/man{1,2,3,4,5,6,7,8}} &&

touch ${LFS}/var/run/utmp ${LFS}/var/log/{btmp,lastlog,wtmp} &&
chmod 644 ${LFS}/var/run/utmp ${LFS}/var/log/{btmp,lastlog,wtmp}

chmod 0750 ${LFS}/root &&
chmod 1777 ${LFS}/tmp ${LFS}/var/tmp

# setup bash symlinks
ln -sf ../stage1/bin/bash ${LFS}/bin
ln -s bash ${LFS}/bin/sh

# Required for glibc build
# Add pwd symlink
ln -sf ../stage1/bin/pwd ${LFS}/bin
# Add perl symlink
ln -sf ../../stage1/bin/perl ${LFS}/usr/bin
# added for glibc make check
ln -sf ../stage1/bin/cat ${LFS}/bin

# added for binutils ar test
# stty
ln -sf ../stage1/bin/stty ${LFS}/bin
# gettext symlinks
ln -sf ../../stage1/bin/msgfmt   ${LFS}/usr/bin
ln -sf ../../stage1/bin/xgettext ${LFS}/usr/bin
ln -sf ../../stage1/bin/msgmerge ${LFS}/usr/bin
# install
ln -sf ../../stage1/bin/install ${LFS}/usr/bin
ln -sf ../usr/bin/install ${LFS}/bin

# Added for ch6 findutils locate tests
ln -sf ../stage1/bin/echo ${LFS}/bin
ln -sf ../stage1/bin/sed ${LFS}/bin
ln -sf ../stage1/bin/awk ${LFS}/bin
ln -sf ../stage1/bin/rm ${LFS}/bin
ln -sf ../stage1/bin/mv ${LFS}/bin
ln -sf ../stage1/bin/chmod ${LFS}/bin
ln -sf ../stage1/bin/chgrp ${LFS}/bin
ln -sf ../../stage1/bin/sort ${LFS}/usr/bin

# ch6 ed make check needs cmp
ln -sf ../../stage1/bin/cmp ${LFS}/usr/bin


if [ ! -f ${LFS}/etc/passwd ]; then
   # Our new root user will be called "root".
   echo "root:x:0:0:root:/root:/bin/bash" > ${LFS}/etc/passwd
   echo "plfstest:x:1000:100:root:/root:/bin/bash" >> ${LFS}/etc/passwd
   echo "nobody:x:65534:65534:nobody:/home:/bin/true" >> ${LFS}/etc/passwd
fi

# May as well move over the root password into a new
# shadow file ( can only do that if we are root )
# Use ":" as delimiter in sed expression here since a username can't contain it.
test `id -u` = 0 && 
{
   grep "^${ROOTUSER}:" /etc/shadow | \
      head -n 1 | sed "s:^${ROOTUSER}\::root\::g" > ${LFS}/etc/shadow
   # Get number of seconds since epoch
   LASTCHG=`date +%s`
   # Convert to days since epoch
   LASTCHG=`expr ${LASTCHG} / 86400`
   echo "plfstest:!:${LASTCHG}:0:99999:7:::" >> ${LFS}/etc/shadow
   echo "nobody:!:${LASTCHG}:0:99999:7:::" >> ${LFS}/etc/shadow
}

# Note: these are from LFS book CVS 2003-03-24, but with some modifications
if [ ! -f ${LFS}/etc/group ]; then
	cat > ${LFS}/etc/group << "EOF"
root:x:0:
bin:x:1:
sys:x:2:
kmem:x:3:
tty:x:4:
tape:x:5:
floppy:x:6:
disk:x:7:
lp:x:8:
dialout:x:9:
audio:x:10:
wheel:x:11:
users:x:100:
nogroup:x:65534:
EOF
fi

# Create initial nsswitch.conf file

cat > ${LFS}/etc/nsswitch.conf << "EOF" 
#
# /etc/nsswitch.conf
#

passwd:	files
shadow:	files
group:	files

hosts:	files dns

EOF

# Copy files required for NSS

# Copy over hosts file
test -f /etc/hosts &&
{
   cp /etc/hosts ${LFS}/etc 
} || {
   echo -e "127.0.0.1\tlocalhost.localdomain\tlocalhost" > ${LFS}/etc/hosts
}
chmod 644 ${LFS}/etc/hosts

# As we've specified dns for hosts, copy across 
# /etc/resolv.conf
# We chmod the file 644, because there are braindead people which might have had
# it chmoded 7xy, so we need to fix this.

test -f /etc/resolv.conf &&
{
   cp /etc/resolv.conf ${LFS}/etc &&
   chmod 644 ${LFS}/etc/resolv.conf
}

# Check for existence of the following nss files.
# If they exist, copy them across and update our new
# ${LFS}/etc/nsswitch.conf
# TODO: should use etc.sample files from netkit-base if not available
#       on host

NSSFILES="ethers netmasks networks protocols rpc services netgroup automount aliases"
for nssfile in ${NSSFILES}; do
   test -e /etc/${nssfile} && \
      cp /etc/${nssfile} ${LFS}/etc &&
      # fix permissions, if executable bit was set before copying:
      chmod 644 ${LFS}/etc/${nssfile} &&
      echo "${nssfile}: files" >> ${LFS}/etc/nsswitch.conf
done

#
# Mount /dev over ${LFS}/dev if devfs is enabled in current kernel
# 

cat /proc/mounts | grep ' devfs ' > /dev/null 2>&1 &&
{
   # From Tushar Teredesai's "Using devfs and devfsd" hint
   mknod -m 611 ${LFS}/dev/console c 5 1
   # should check the tty gid in our new ${LFS}/etc/group,
   # may differ from the host, and cant expect the above /etc/group creation not
   # to be changed
   TTYGID=`grep ^tty: ${LFS}/etc/group | awk -F\: '{print $3}'`
   chgrp ${TTYGID} ${LFS}/dev/console
   mknod -m 666 ${LFS}/dev/null c 1 3

   mount --bind /dev ${LFS}/dev
}

# For automated build only (RO)
# Check against your build tree and modify accordingly before uncommenting

CHSCRIPTS=`echo ${SCRIPTS} | sed "s@^${LFS}@@"`
CHLOGS=`echo ${LOGS} | sed "s@^${LFS}@@"`

chroot ${LFS} /stage1/bin/env -i \
   HOME=/root TERM=$TERM PS1='[\u@\h \W]\$ ' \
   PATH=/sbin:/bin:/usr/sbin:/usr/bin:/stage1/sbin:/stage1/bin \
   LDFLAGS="-s" \
   ${CHSCRIPTS}/build-ch6-toolchain-${VERSION}.sh 2>&1 | \
      tee ${LOGS}/buildlog-ch6.log-${DATE}

