#
# Build Debian-style cross compiler packages
#
# Copyright 2006-2010  Hector Oron <hector.oron@gmail.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#
# Latest version of this file can be downloaded from:
#  http://www.emdebian.org/repos/current/host/trunk/buildcross/trunk/


# Based on a script:
# Written by Milan Buska - newishi at newishi dot net
# Created with resources to
# http://crux.nu
# http://cross-lfs.org/view/clfs-sysroot/x86
# http://www.eglibc.org/cgi-bin/viewcvs.cgi/branches/eglibc- 2_10/libc/EGLIBC.cross-building? rev = 8421 & view = markup /
#
# Before running, select the disk is formatted
# partition and attach it to the directory /opt/$tuplet_directory, p.e. /opt/arm-ballonboard-linux-gnueabi
#
set -e
VERBOSE=no

#-------------------------------------------------------------------------------------------------
# Settings variables
#
bboard=pc            # Build vendor or board
tboard=ballonboard   # Target vendor or board
bgnuarch=i686        # Build GNU arch
tgnuarch=arm         # Target GNU arch | Can get via dpkg-architecture
debarch=armel        # Debian architecture
linux_arch="arm"
blibc=gnu
tlibc=gnueabi
bkernel=linux
tkernel=linux
build="$bgnuarch-$bboard-$bkernel-$blibc"
host=$build
target="$tgnuarch-$tboard-$tkernel-$tlibc"
sysroot="/home/zumbi/tmp/$target"

#-------------------------------------------------------------------------------------------------
# up_init: Initializes upstream version setup
#
up_init () {
# stage1
gmpv=gmp-4.3.2
mpfrv=mpfr-2.4.2
pplv=ppl-0.10.2
cloogv=cloog-ppl-0.15.7
mpcv=mpc-0.8.1
binutilsv=binutils-2.20
gccv=gcc-4.4.3
eglibcv=eglibc-2.10
linuxv=linux-2.6.32.8
filev=file-5.03
groffv=groff-1.20.1
shadowv=shadow-4.1.4.2
ncursesv=ncurses-5.7

upsrc=$sysroot/upsrc
mkdir -pv $upsrc

#
}

#-------------------------------------------------------------------------------------------------
# deb_init: Initializes upstream version setup
#
deb_init () {
# stage1
gmpv=gmp-*
mpfrv=mpfr-*
pplv=ppl-*
cloogv=cloog-ppl-*
#mpcv=mpc-*
#libmpdclient2v=libmpdclient-*
binutilsv=binutils-*
gccv=gcc-*
eglibcv=eglibc-*
linuxv=linux-2.6-*
filev=file-*
groffv=groff-*
shadowv=shadow-*
ncursesv=ncurses-*
bashv=bash-*
bisonv=bison-*
bzip2v=bzip2-*
coreutilsv=coreutils-*
diffutilsv=diffutils-*
findutilsv=findutils-*
filev=file-*
flexv=flex-*
gawkv=gawk-*
gettextv=gettext-*
grepv=grep-*
gzipv=gzip-*
m4v=m4-*
makev=make-dfsg-*
patchv=patch-*
sedv=sed-*
tarv=tar-*
texinfov=texinfo-*
xzv=xz-utils-*
utillinuxv=util-linux-*
zlibv=zlib-*

debsrc=$sysroot/debsrc
mkdir -pv $debsrc

#
}

#-------------------------------------------------------------------------------------------------
# up_fetch: Fetch upstream version
#
up_fetch () {
#
gnuurl="http://ftp.sh.cvut.cz/MIRRORS/gnu/pub/gnu"
echo ""
echo " == Getting upstream sources == "
wget -nc -P $upsrc \
   $gnuurl/binutils/binutils-2.20.tar.bz2 \
   $gnuurl/groff/groff-1.20.1.tar.gz \
   $gnuurl/ncurses/ncurses-5.7.tar.gz \
   http://ftp.sh.cvut.cz/MIRRORS/kernel/linux/kernel/v2.6/linux-2.6.32.8.tar.bz2 \
   http://gcc.fyxm.net/releases/gcc-4.4.3/gcc-4.4.3.tar.bz2 \
   http://gcc.fyxm.net/infrastructure/gmp-4.3.2.tar.bz2 \
   http://gcc.fyxm.net/infrastructure/mpfr-2.4.2.tar.bz2 \
   http://gcc.fyxm.net/infrastructure/mpc-0.8.1.tar.gz \
   http://gcc.fyxm.net/infrastructure/ppl-0.10.2.tar.gz \
   http://gcc.fyxm.net/infrastructure/cloog-ppl-0.15.7.tar.gz \
   ftp://ftp.astron.com/pub/file/file-5.03.tar.gz \
   ftp://pkg-shadow.alioth.debian.org/pub/pkg-shadow/shadow-4.1.4.2.tar.bz2 \
   http://www.lfs-matrix.org/pub/clfs/conglomeration/eglibc/eglibc-2.10.tar.bz2 \
   http://patches.cross-lfs.org/sysroot-dev/shadow-4.1.4.2-sysroot_hacks-1.patch \
   http://patches.cross-lfs.org/sysroot-dev/ncurses-5.7-bash_fix-1.patch
echo ""
}

#-------------------------------------------------------------------------------------------------
# deb_fetch: Fetches Debian sources
#
deb_fetch () {
echo ""
echo " == Getting Debian sources (only for Debian based systems) == "
cd $debsrc
apt-get source \
            binutils \
            groff \
            ncurses \
            linux-2.6 \
            gcc-4.4 \
            gmp \
            mpfr \
            mpc \
            ppl \
            cloog-ppl \
            file \
            shadow \
            eglibc
#extra
apt-get source \
            bash \
            bison \
            bzip2 \
            coreutils \
            diffutils \
            file \
            findutils \
            flex \
            gawk \
            gettext \
            grep \
            gzip \
            m4 \
            make-dfsg \
            patch \
            sed \
            tar \
            texinfo \
            xz-utils \
            util-linux \
            zlib1g
            #libmpdclient2 \     # Required by MPC
            
echo ""
#
}

#-------------------------------------------------------------------------------------------------
# up_unpack: Unpack upstream sources
#
up_unpack () {
cd $upsrc
echo ""
echo " == Unpacking GMP == "
tar -xf gmp-4.3.2.tar.bz2
echo ""
echo " == Unpacking MPFR == "
tar -xf mpfr-2.4.2.tar.bz2
echo ""
echo " == Unpacking MPC == "
tar -xf mpc-0.8.1.tar.gz
echo ""
echo " == Unpacking PPL == "
tar -xf ppl-0.10.2.tar.gz
echo ""
echo " == Unpacking CLOOG-PPL == "
tar -xf cloog-ppl-0.15.7.tar.gz
echo ""
echo " == Unpacking BINUTILS == "
tar -xf binutils-2.20.tar.bz2
echo ""
echo " == Unpacking GCC == "
tar -xf gcc-4.4.3.tar.bz2
echo ""
echo " == Unpacking KERNEL == "
tar -xf linux-2.6.32.8.tar.bz2
echo ""
echo " == Unpacking EGLIBC == "
tar -xf eglibc-2.10.tar.bz2
echo ""
echo " == Unpacking FILE == "
tar -xf file-5.03.tar.gz
echo ""
echo " == Unpacking GROFF == "
tar -xf groff-1.20.1.tar.gz
echo ""
echo " == Unpacking SHADOW == "
tar -xf shadow-4.1.4.2.tar.bz2
echo ""
echo " == Unpacking NCURSES == "
tar -xf ncurses-5.7.tar.gz
echo ""
echo " == All O.K == "
echo ""
#
}

#-------------------------------------------------------------------------------------------------
# deb_unpack: Unpacks Debian sources
#
deb_unpack () {
cd $debsrc
echo ""
echo " == Unpacking Debian sources (only for Debian based systems) == "
echo ""
echo " == Unpacking GCC == "
# XXX Fix temporary hack (version name)
(cd $debsrc/$gccv &&
tar --xz -xf gcc-4.4.4-dfsg.tar.xz)
echo " == Patching EGLIBC == "
# XXX Fix temporary hack (version name)
(cd $debsrc/$eglibcv &&
debian/rules patch)
echo ""
echo " Already unpacked :-)"
echo ""
#
}


#-------------------------------------------------------------------------------------------------
# rfs_init: Initializes new RootFS
#
rfs_init () {
work=$sysroot/work
mkdir -pv $work
tools=$sysroot/tools
mkdir -pv $tools
echo ""
# Creating Directories
mkdir -pv $sysroot/bin
mkdir -pv $sysroot/sbin
mkdir -pv $sysroot/boot
mkdir -pv $sysroot/dev
mkdir -pv $sysroot/proc
mkdir -pv $sysroot/sys
mkdir -pv $sysroot/etc
mkdir -pv $sysroot/tmp
mkdir -pv $sysroot/mnt
mkdir -pv $sysroot/root
mkdir -pv $sysroot/lib
mkdir -pv $sysroot/lib/modules
mkdir -pv $sysroot/opt
mkdir -pv $sysroot/opt/bin
mkdir -pv $sysroot/opt/sbin
mkdir -pv $sysroot/usr
mkdir -pv $sysroot/usr/{bin,include,lib,sbin,share,src,man}
mkdir -pv $sysroot/usr/man/man{1,2,3,4,5,6,7,8}
test -d $sysroot/usr/var || ln -sv ../var $sysroot/usr/var
mkdir -pv $sysroot/var
mkdir -pv $sysroot/var/cache
mkdir -pv $sysroot/var/lib
mkdir -pv $sysroot/var/lib/pkg
mkdir -pv $sysroot/var/lock
mkdir -pv $sysroot/var/log
mkdir -pv $sysroot/var/log/old
mkdir -pv $sysroot/var/run
touch $sysroot/var/run/utmp
mkdir -pv $sysroot/var/spool
mkdir -pv $sysroot/var/spool/mail
mkdir -pv $sysroot/var/tmp
mkdir -pv $sysroot/var/ftp
mkdir -pv $sysroot/var/www
mkdir -pv $sysroot/var/empty
test -d $sysroot/var/mail || ln -sv spool/mail $sysroot/var/mail
mkdir -pv $sysroot/home
chmod -v 1777 $sysroot/var/spool/mail
chmod -v 1777 $sysroot/tmp
chmod -v 1777 $sysroot/var/lock
chmod -v 1777 $sysroot/var/tmp
chmod -v 0750 $sysroot/root
### /dev
test -e $sysroot/dev/console || sudo mknod $sysroot/dev/console c 5 1
sudo chmod -v 0600 $sysroot/dev/console
## XXX: wookey: how was to setup serial tty? mknod $sysroot/dev/tty1 c ? ?
#
### Creating the passwd, group, fstab, issue,
#   mime.types, motd, mtab,securetty, shadow,
#   shells and log Files
#
# passwd
cat > $sysroot/etc/passwd << EOF
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:
daemon:x:2:2:daemon:/sbin:
mail:x:8:12:mail:/var/spool/mail:
ftp:x:14:11:ftp:/var/ftp:
www:x:15:15:www:/var/www:/bin/false
messagebus:x:81:81::/var/empty:/bin/false
nobody:x:99:99:nobody:/:
EOF
# group
cat > $sysroot/etc/group << EOF
root::0:root
bin::1:root,bin,daemon
daemon::2:root,bin,daemon
sys::3:root,bin
adm::4:root,daemon
tty::5:
disk::6:root
lp::7:daemon
mem::8:
kmem::9:
wheel::10:root
ftp::11:
mail::12:
mlocate::13:
dialout::14:
www::15:
audio::16:
video::17:
floppy::18:
tape::19:
scanner::20:
cdrom::21:
nobody::99:
messagebus::81:
users::100:
EOF
# fstab
cat > $sysroot/etc/fstab << EOF
# /etc/fstab: static file system information
#
# <file system>        <dir>     <type>    <options>              <dump>
# <pass>

#/dev/#REISERFS_ROOT#  /         reiserfs  defaults               0 0
#/dev/#EXT3FS_ROOT#    /         ext3      defaults               0 1
#/dev/#EXT4FS_ROOT#    /         ext4      defaults               0 1
#/dev/#JFS_ROOT#       /         jfs       defaults               1 1
#/dev/#XFS_ROOT#       /         xfs       defaults               0 0
#/dev/#SWAP#           swap      swap      defaults               0 0
#/dev/#REISERFS_HOME#  /home     reiserfs  defaults               0 0
#/dev/#EXT3FS_HOME#    /home     ext3      defaults               0 2
#/dev/#EXT4FS_HOME#    /home     ext4      defaults               0 2
#/dev/#JFS_HOME#       /home     jfs       defaults               1 2
#/dev/#XFS_HOME#       /home     xfs       defaults               0 0
#/dev/cdrom            /cdrom    iso9660   ro,user,noauto,unhide  0 0
#/dev/dvd              /dvd      udf       ro,user,noauto,unhide  0 0
#/dev/floppy/0         /floppy   vfat      user,noauto,unhide     0 0
devpts                 /dev/pts  devpts    defaults               0 0
none                   /sys      sysfs     defaults               0 0
none                   /proc     proc      defaults               0 0
#tmp                   /tmp      tmpfs     defaults               0 0
#shm                   /dev/shm  tmpfs     defaults               0 0
#usb                   /proc/bus/usb usbfs defaults               0 0

# End of file
EOF
# issue
cat > $sysroot/etc/issue << EOF

Welcome to new system  (\n) (\l)

EOF
# mime.types
cat > $sysroot/etc/mime.types << EOF
# /etc/mime.types: mime types
#

application/andrew-inset        ez
application/excel               xls
application/octet-stream        bin
application/oda                 oda
application/pdf                 pdf
application/pgp                 pgp
application/postscript          ps PS eps
application/rtf                 rtf
application/x-arj-compressed    arj
application/x-bcpio             bcpio
application/x-chess-pgn         pgn
application/x-cpio              cpio
application/x-csh               csh
application/x-debian-package    deb
application/x-msdos-program     com exe bat
application/x-dvi               dvi
application/x-gtar              gtar
application/x-gunzip            gz
application/x-hdf               hdf
application/x-latex             latex
application/x-mif               mif
application/x-netcdf            cdf nc
application/x-perl              pl pm
application/x-rar-compressed    rar
application/x-sh                sh
application/x-shar              shar
application/x-sv4cpio           sv4cpio
application/x-sv4crc            sv4crc
application/x-tar               tar
application/x-tar-gz            tgz tar.gz
application/x-tcl               tcl
application/x-tex               tex
application/x-texinfo           texi texinfo
application/x-troff             t tr roff
application/x-troff-man         man
application/x-troff-me          me
application/x-troff-ms          ms
application/x-ustar             ustar
application/x-wais-source       src
application/x-zip-compressed    zip

audio/basic                     snd
audio/midi                      mid midi
audio/ulaw                      au
audio/x-aiff                    aif aifc aiff
audio/x-wav                     wav

image/gif                       gif
image/ief                       ief
image/jpeg                      jpe jpeg jpg
image/png                       png
image/tiff                      tif tiff
image/x-cmu-raster              ras
image/x-portable-anymap         pnm
image/x-portable-bitmap         pbm
image/x-portable-graymap        pgm
image/x-portable-pixmap         ppm
image/x-rgb                     rgb
image/x-xbitmap                 xbm
image/x-xpixmap                 xpm
image/x-xwindowdump             xwd

text/html                       html htm
text/plain                      asc txt
text/css                        css
text/richtext                   rtx
text/tab-separated-values       tsv
text/x-setext                   etx

video/dl                        dl
video/fli                       fli
video/gl                        gl
video/mpeg                      mp2 mpe mpeg mpg
video/quicktime                 mov qt
video/x-msvideo                 avi
video/x-sgi-movie               movie

x-world/x-vrml                  vrm vrml wrl

# End of file
EOF
# motd
touch $sysroot/etc/motd
# mtab
touch $sysroot/etc/mtab
# securetty
cat > $sysroot/etc/securetty << EOF
# /etc/securetty: defines which devices root can log in on
#

console
ttyS0
tty1
tty2
tty3
tty4
tty5
tty6

# End of file
EOF
# shadow
cat > $sysroot/etc/shadow << EOF
root::0::::::
bin:x:0::::::
daemon:x:0::::::
mail:x:0::::::
ftp:x:0::::::
www:x:0::::::
messagebus:x:0::::::
nobody:x:0::::::
EOF
# shells
cat > $sysroot/etc/shells << EOF
# /etc/shells: defines valid login shells
#

/bin/sh
/bin/bash

# End of file
EOF
#
touch $sysroot/var/run/utmp $sysroot/var/log/{btmp,lastlog,wtmp}
chmod -v 664 $sysroot/var/run/utmp $sysroot/var/log/lastlog
chmod -v 600 $sysroot/var/log/btmp
#
}

#-------------------------------------------------------------------------------------------------
# build_set: Sets the build for Debian or upstream
#
build_set () {
###  Set upstream or debian source
case $buildtype in
    upstream|u)
        [ x$VERBOSE = xyes ] | echo " "
        [ x$VERBOSE = xyes ] | echo " == Build set for upstream sources == "
        [ x$VERBOSE = xyes ] | echo " "
        src=$upsrc # | $debsrc # XXX Should match version names
        ;;
    debian|deb|d)
        [ x$VERBOSE = xyes ] | echo " "
        [ x$VERBOSE = xyes ] | echo " == Build set for Debian sources == "
        [ x$VERBOSE = xyes ] | echo " "
        src=$debsrc # | $debsrc # XXX Should match version names
        ;;
esac
}

#-------------------------------------------------------------------------------------------------
# build_stage1: Start the build for Debian or upstream (upstream way)
#
build_stage1 () {
###  Constructing Cross-Compile Tools
#  GMP
echo " "
echo " == Compiling $gmpv == "
echo " "
mkdir -pv $work/gmp
cd $work/gmp
$src/$gmpv/configure --prefix=$tools \
                    --enable-cxx

make
make install

# MPFR
echo " "
echo " == Compiling $mpfrv == "
echo " "
mkdir -pv $work/mpfr
cd $work/mpfr
LDFLAGS="-Wl,-rpath,$tools/lib" \
$src/$mpfrv/configure --prefix=$tools \
                     --enable-shared \
                     --with-gmp=$tools

make
make install

# libmpdclient2
## echo " "
## echo " == Compiling $libmpdclient2v == "
## echo " "
## mkdir -pv $work/libmpdclient2
## cd $work/libmpdclient2
## LDFLAGS="-Wl,-rpath,$tools/lib" \
## $src/$libmpdclient2v/configure --prefix=$tools \
##                      --enable-shared \
##                      --with-gmp=$tools \
##                      --with-mpfr=$tools
## 
## make
## make install
## 
## # MPC
## echo " "
## echo " == Compiling $mpcv == "
## echo " "
## mkdir -pv $work/mpc
## cd $work/mpc
## LDFLAGS="-Wl,-rpath,$tools/lib" \
## LIBMPDCLIENT_LIBS="$tools/lib" \
## $src/$mpcv/configure --prefix=$tools \
##                     --enable-shared \
##                     --with-gmp=$tools \
##                     --with-mpfr=$tools
## 
## make
## make install

## # PPL
## echo " "
## echo " == Compiling $pplv == "
## echo " "
## mkdir -pv $work/ppl
## cd $work/ppl
## LDFLAGS="-Wl,-rpath,$tools/lib" \
## $src/$pplv/configure --prefix=$tools \
##                     --enable-shared \
##                     --enable-interfaces="c,cxx" \
##                     --disable-optimization \
##                     --with-libgmp-prefix=$tools \
##                     --with-libgmpxx-prefix=$tools
## 
## make
## make install
## 
# CLOOG
echo " "
echo " == Compiling $cloogv == "
echo " "
mkdir -pv $work/cloog
cd $work/cloog
LDFLAGS="-Wl,-rpath,$tools/lib" \
$src/$cloogv/configure --prefix=$tools \
                      --enable-shared \
                      --with-bits=gmp \
                      --with-gmp=$tools \
                      --with-ppl=$tools

make
make install

## ERROR:
## zumbi@eefk2m5:~/tmp/arm-ballonboard-linux-gnueabi/work/zlib$ LDFLAGS="-Wl,-rpath,$tools/lib" /home/zumbi/tmp/arm-ballonboard-linux-gnueabi/debsrc/zlib-1.2.5.dfsg/configure --prefix=/home/zumbi/tmp/arm-ballonboard-linux-gnueabi/tools --shared
## /home/zumbi/tmp/arm-ballonboard-linux-gnueabi/debsrc/zlib-1.2.5.dfsg/configure: 23: cannot open zlib.h: No such file
## /home/zumbi/tmp/arm-ballonboard-linux-gnueabi/debsrc/zlib-1.2.5.dfsg/configure: 24: cannot open zlib.h: No such file
## /home/zumbi/tmp/arm-ballonboard-linux-gnueabi/debsrc/zlib-1.2.5.dfsg/configure: 25: cannot open zlib.h: No such file
## /home/zumbi/tmp/arm-ballonboard-linux-gnueabi/debsrc/zlib-1.2.5.dfsg/configure: 26: cannot open zlib.h: No such file
## Checking for gcc...
## Checking for shared library support...
## No shared library support.
## Building static library libz.a version with gcc.
## Checking for off64_t... Yes.
## Checking for fseeko... Yes.
## cp: ha fallat stat() sobre «zconf.h.in»: El fitxer o directori no existeix
## /home/zumbi/tmp/arm-ballonboard-linux-gnueabi/debsrc/zlib-1.2.5.dfsg/configure: 304: cannot open zconf.h: No such file
## mv: ha fallat stat() sobre «zconf.temp.h»: El fitxer o directori no existeix
## Checking for unistd.h... Yes.Checking whether to use vs[n]printf() or s[n]printf()... using vs[n]printf().
## Checking for vsnprintf() in stdio.h... Yes.
## Checking for return value of vsnprintf()... Yes.
## Checking for attribute(visibility) support... Yes.
## /home/zumbi/tmp/arm-ballonboard-linux-gnueabi/debsrc/zlib-1.2.5.dfsg/configure: 573: cannot open Makefile.in: No such file
## /home/zumbi/tmp/arm-ballonboard-linux-gnueabi/debsrc/zlib-1.2.5.dfsg/configure: 596: cannot open zlib.pc.in: No such file
## 
## # zlib
## echo " "
## echo " == Compiling $zlibv == "
## echo " "
## #cd $src/$zlibv
## mkdir -pv $work/zlib
## cd $work/zlib
## ## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
## LDFLAGS="-Wl,-rpath,$tools/lib" \
## $src/$zlibv/configure --prefix=$tools \
##                       --shared
##                       #--libdir=$tools/lib64
## 
## make
## make install

# BINUTILS
echo " "
echo " == Compiling $binutilsv == "
echo " "
mkdir -pv $work/binutils
cd $work/binutils
AR=ar AS=as \
$src/$binutilsv/configure --prefix=$tools \
                         --host=$host \
                         --target=$target \
                         --with-sysroot=$sysroot \
                         --disable-nls \
                         --enable-shared \
                         --disable-multilib \
                         --with-pkgversion="$tboard"

make configure-host
make
make install

# GCC - 1
echo " "
echo " == Compiling $gccv - 1 == "
echo " "
mkdir -pv $work/gcc1
cd $work/gcc1
AR=ar LDFLAGS="-Wl,-rpath,$tools/lib" \
$src/$gccv/$gccv/configure --prefix=$tools \
                    --build=$build \
                    --host=$host \
                    --target=$target \
                    --disable-multilib \
                    --with-sysroot=$sysroot \
                    --disable-nls \
                    --without-headers \
                    --with-newlib \
                    --disable-decimal-float \
                    --disable-libgomp \
                    --disable-libmudflap \
                    --disable-libssp \
                    --with-mpfr=$tools \
                    --with-gmp=$tools \
                    --with-mpc=$tools \
                    --with-ppl=$tools \
                    --with-cloog=$tools \
                    --disable-shared \
                    --disable-threads \
                    --enable-languages=c

PATH=$tools/bin:$PATH make
PATH=$tools/bin:$PATH make install

# Install kernel headers
echo " "
echo " == Installing $linuxv headers == "
echo " "
mkdir -pv $work/linux
cp -r $src/$linuxv $work/linux
cd $work/linux/$linuxv
PATH=$tools/bin:$PATH \
make headers_install \
ARCH=$linux_arch CROSS_COMPILE=$target- \
INSTALL_HDR_PATH=$sysroot/usr
find $sysroot/usr/include -name .install -or -name ..install.cmd | xargs /bin/rm -fv
cp -v $src/$binutilsv/include/libiberty.h $sysroot/usr/include

# EGLIBC - HEADERS
echo " "
echo " == Compiling $eglibcv headers - 1 == "
echo " "
mkdir -pv $work/eglibc-headers
mkdir -pv $src/eglibcv/manual
cd $work/eglibc-headers
echo "ac_cv_prog_AUTOCONF=autoconf2.50" > config.cache
PATH=$tools/bin:$PATH \
BUILD_CC=gcc \
CC=$tools/bin/$target-gcc \
CXX=$tools/bin/$target-g++ \
AR=$tools/bin/$target-ar \
RANLIB=$tools/bin/$target-ranlib \
$src/$eglibcv/configure --prefix=/usr \
                       --with-headers=$sysroot/usr/include \
                       --build=$build \
                       --host=$target \
                       --disable-profile \
                       --without-gd \
                       --without-cvs \
                       --enable-add-ons \
                       -C config.cache

make install-headers install_root=$sysroot \
    install-bootstrap-headers=yes

echo " "
echo " == Done install-headers == "
echo " "
#
#
#
mkdir -pv $sysroot/usr/lib
#cd $src/$eglibcv
make csu/subdir_lib
cp -v csu/{crt1.o,crti.o,crtn.o} $sysroot/usr/lib
#
$tools/bin/$target-gcc -nostdlib -nostartfiles -shared -x c /dev/null \
                      -o $sysroot/usr/lib/libc.so

# GCC - 2
echo " "
echo " == Compiling $gccv - 2 == "
echo " "
mkdir -pv $work/gcc2
cd $work/gcc2
AR=ar LDFLAGS="-Wl,-rpath,$tools/lib" \
$src/$gccv/$gccv/configure --prefix=$tools \
                    --build=$build \
                    --host=$host \
                    --target=$target \
                    --with-sysroot=$sysroot \
                    --disable-libssp \
                    --disable-libgomp \
                    --disable-libmudflap \
                    --with-mpfr=$tools \
                    --with-gmp=$tools \
                    --with-mpc=$tools \
                    --with-ppl=$tools \
                    --with-cloog=$tools \
                    --disable-nls \
                    --disable-multilib \
                    --disable-decimal-float \
                    --enable-languages=c

PATH=$tools/bin:$PATH make
PATH=$tools/bin:$PATH make install

# EGLIBC
echo " "
echo " == Compiling $eglibcv - 2 == "
echo " "
mkdir -pv $work/eglibc
cd $work/eglibc
cat > configparms << EOF
install_root=$sysroot
EOF
#CFLAGS="-march=$tgnuarch -g -O2" \
CFLAGS="-g -O2" \
BUILD_CC=gcc \
CC=$tools/bin/$target-gcc \
AR=$tools/bin/$target-ar \
RANLIB=$tools/bin/$target-ranlib \
$src/$eglibcv/configure --prefix=/usr \
                       --with-headers=$sysroot/usr/include \
                       --build=$build \
                       --host=$target \
                       --disable-profile \
                       --without-gd \
                       --without-cvs \
                       --enable-add-ons \
                       --with-pkgversion="$tboard"

PATH=$tools/bin:$PATH make
PATH=$tools/bin:$PATH make install install_root=$sysroot
PATH=$tools/bin:$PATH make install_root=$sysroot localedata/install-locales

ln -sfv ../usr/share/zoneinfo/UTC $sysroot/etc/localtime
mkdir -pv $sysroot/etc/ld.so.conf.d $sysroot/usr/lib/locale
rm $sysroot/usr/include/rpcsvc/{rquota.h,rquota.x}
rm -rf $sysroot/usr/share/info

#
# $sysroot/etc (host.conf,hosts,ld.so.conf,nsswitch.conf,resolv.conf)
#
# host.conf
cat > $sysroot/etc/host.conf << EOF
# /etc/host.conf: resolver configuration file
#

order hosts,bind
multi on

# End of file
EOF
# hosts
cat > $sysroot/etc/hosts << EOF
# /etc/hosts: static lookup table for host names
#

# IPv4
127.0.0.1       localhost
#<ip-address>   <hostname.domain.org>   <aliases>

# IPv6
#::1            ip6-localhost   ip6-loopback
#fe00::0        ip6-localnet
#ff00::0        ip6-mcastprefix
#ff02::1        ip6-allnodes
#ff02::2        ip6-allrouters
#ff02::3        ip6-allhosts

# End of file
EOF
# ld.so.conf
cat > $sysroot/etc/ld.so.conf << EOF
# /etc/ld.so.conf: dynamic linker configuration file
#

include /etc/ld.so.conf.d/*.conf

# End of file
EOF
# nsswitch.conf
cat > $sysroot/etc/nsswitch.conf << EOF
# /etc/nsswitch.conf: name service switch configuration file
#

passwd:      files
group:       files
shadow:      files

hosts:       files dns
networks:    files

services:    files
protocols:   files
rpc:         files
publickey:   files
ethers:      files
netmasks:    files
netgroup:    files
bootparams:  files

automount:   files
aliases:     files

# End of file
EOF
# resolv.conf
cat > $sysroot/etc/resolv.conf << EOF
# /etc/resolv.conf: resolver configuration file
#

#search <domain.org>
#nameserver <ip-address>

# End of file
EOF


# GCC - 3
echo " "
echo " == Compiling $gccv - 3 == "
echo " "
mkdir -pv $work/gcc3
cd $work/gcc3
AR=ar LDFLAGS="-Wl,-rpath,$tools/lib" \
$src/$gccv/$gccv/configure --prefix=$tools \
                    --build=$build \
                    --host=$host \
                    --target=$target \
                    --enable-c99 \
                    --enable-long-long \
                    --enable-threads=posix \
                    --enable-shared \
                    --enable-__cxa_atexit \
                    --disable-libssp \
                    --disable-libgomp \
                    --disable-libmudflap \
                    --disable-nls \
                    --disable-multilib \
                    --enable-languages=c,c++,objc \
                    --with-sysroot=$sysroot \
                    --with-mpfr=$tools \
                    --with-gmp=$tools \
                    --with-mpc=$tools \
                    --with-ppl=$tools \
                    --with-cloog=$tools \
                    --with-pkgversion="$tboard"

PATH=$tools/bin:$PATH make \
AS_FOR_TARGET="$target-as" \
LD_FOR_TARGET="$target-ld"
PATH=$tools/bin:$PATH make install

echo " "
echo " == FINISH - stage 1 == :-)"
echo " "
}

#-------------------------------------------------------------------------------------------------
# build_stage2: Start the build for Debian or upstream (upstream way)
#
build_stage2 () {
#
# FILE
echo " "
echo " == Compiling $filev == "
echo " "
mkdir -pv $work/file
cd $work/file
$src/$filev/configure --prefix=$tools \
                      --libdir=$tools/lib \
                      --build=$build \
                      --host=$target

make
make install
## cat $SOURCES/file-5.03-cross_compile-1.patch | patch -p1
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
## 	--libdir=/tools/lib64 --build=${CLFS_HOST} --host=${CLFS_TARGET}

# GROFF
echo " "
echo " == Compiling $groffv == "
echo " "
mkdir -pv $work/groff
cd $work/groff
PAGE="A4" \
$src/$groffv/configure --prefix=$tools \
                       --libdir=$tools/lib \
                       --build=$build \
                       --host=$target \
                       --without-x

make
make install

# SHADOW
echo " "
echo " == Compiling $shadowv == "
echo " "
cd $src/$shadowv
patch -Np1 -i ../shadow-4.1.4.2-sysroot_hacks-1.patch
mkdir -pv $work/shadow
cd $work/shadow
cat > config.cache << EOF
shadow_cv_passwd_dir=$sysroot/bin"
EOF
cat >> config.cache << EOF
ac_cv_func_lckpwdf=no
EOF
$src/$shadowv/configure --prefix=$tools \
                       --sbindir=$tools/bin \
                       --sysconfdir=$sysroot/etc \
                       --disable-shared \
                       --without-libpam \
                       --without-audit \
                       --libdir=$tools/lib \
                       --build=$build \
                       --host=$target \
                       --without-selinux \
                       --program-prefix=$target- \
                       --without-nscd

cp config.h{,.orig}
sed "/PASSWD_PROGRAM/s/passwd/$target-&/" config.h.orig > config.h

make
make install

# NCURSES
echo " "
echo " == Compiling $ncursesv == "
echo " "
cd $src/$ncursesv
patch -Np1 -i ../ncurses-5.7-bash_fix-1.patch
mkdir -pv $work/ncurses
cd $work/ncurses
$src/$ncursesv/configure --prefix=$tools \
                         --libdir=$tools/lib \
                         --build=$build \
                         --host=$target \
                         --without-debug \
                         --without-shared

make -C include
make -C progs tic
install -m755 progs/tic $tools/bin

#

## # bash
echo " "
echo " == Compiling $bashv == "
echo " "
cd $src/$bashv
mkdir -pv $work/bash
cd $work/bash
## cat > config.cache << "EOF"
## ac_cv_func_mmap_fixed_mapped=yes
## ac_cv_func_strcoll_works=yes
## ac_cv_func_working_mktime=yes
## bash_cv_func_sigsetjmp=present
## bash_cv_getcwd_malloc=yes
## bash_cv_job_control_missing=present
## bash_cv_printf_a_format=yes
## bash_cv_sys_named_pipes=present
## bash_cv_ulimit_maxfds=yes
## bash_cv_under_sys_siglist=yes
## bash_cv_unusable_rtsigs=no
## gt_cv_int_divbyzero_sigfpe=yes
## EOF
## CC="${CC} ${BUILD64}" CXX="${CXX} ${BUILD64}" \
## 	./configure --prefix=/tools \
## 	--build=${CLFS_HOST} --host=${CLFS_TARGET} \
## 	--without-bash-malloc --cache-file=config.cache
$src/$bashv/configure --prefix=$tools \
                         --libdir=$tools/lib \
                         --build=$build \
                         --host=$target \
                         --without-debug \
                         --without-bash-malloc \
                         --cache-file=config.cache

make
make install
ln -sfv bash $tools/bin/sh

## # bison
echo " "
echo " == Compiling $bisonv == "
echo " "
cd $src/$bisonv
mkdir -pv $work/bison
cd $work/bison
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$bisonv/configure --prefix=$tools \
                       --build=$build \
                       --host=$target
make
make install

## XXX # bzip2
echo " "
echo " == Compiling $bzip2v == "
echo " "
cd $src/$bzip2v
mkdir -pv $work/bzip2
cd $work/bzip2
## $src/$bzip2v/configure --prefix=$tools \
## cp -v Makefile{,.orig}
## sed -e 's@^\(all:.*\) test@\1@g' -e 's@/lib\(/\| \|$\)@/lib64\1@g' Makefile.orig > Makefile
## make CC="${CC} ${BUILD64}" AR="${AR}" RANLIB="${RANLIB}"
## make PREFIX=/tools install
## 
## 
## # coreutils
echo " "
echo " == Compiling $coreutilsv == "
echo " "
cd $src/$coreutilsv
mkdir -pv $work/coreutils
cd $work/coreutils
## cat > config.cache << EOF
## fu_cv_sys_stat_statfs2_bsize=yes
## gl_cv_func_rename_trailing_slash_bug=no
## gl_cv_func_working_mkstemp=yes
## EOF
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$coreutilsv/configure --prefix=$tools \
                           --build=$build \
                           --host=$target \
                           --enable-install-program=hostname 
                           #--cache-file=config.cache
## 	--build=${CLFS_HOST} --host=${CLFS_TARGET} \
## 	--enable-install-program=hostname --cache-file=config.cache
touch man/hostname.1
make
make install

## #diffutils
echo " "
echo " == Compiling $diffutilsv == "
echo " "
cd $src/$diffutilsv
mkdir -pv $work/diffutils
cd $work/diffutils
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$diffutilsv/configure --prefix=$tools \
                           --build=$build \
                           --host=$target

make
make install

## #find
echo " "
echo " == Compiling $findutilsv == "
echo " "
cd $src/$findutilsv
mkdir -pv $work/findutils
cd $work/findutils
## echo "gl_cv_func_wcwidth_works=yes" > config.cache
## echo "ac_cv_func_fnmatch_gnu=yes" >> config.cache
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$findutilsv/configure --prefix=$tools \
                           --build=$build \
                           --host=$target
                        ## --cache-file=config.cache

make
make install

## # flex
echo " "
echo " == Compiling $flexv == "
echo " "
cd $src/$flexv
mkdir -pv $work/flex
cd $work/flex
## cat $SOURCES/flex-2.5.35-gcc44-1.patch | patch -p1
## cp -v Makefile.in{,.orig}
## sed "s/-I@includedir@//g" Makefile.in.orig > Makefile.in
## cat > config.cache << EOF
## ac_cv_func_malloc_0_nonnull=yes
## ac_cv_func_realloc_0_nonnull=yes
## EOF
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$flexv/configure --prefix=$tools \
                           --build=$build \
                           --host=$target
                        ## --cache-file=config.cache

make
make install

## # gawk
echo " "
echo " == Compiling $gawkv == "
echo " "
cd $src/$gawkv
mkdir -pv $work/gawk
cd $work/gawk
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$gawkv/configure --prefix=$tools \
                           --build=$build \
                           --host=$target \
                           --disable-libsigsegv
                        ## --cache-file=config.cache

make
make install

## # gettext
echo " "
echo " == Compiling $gettextv == "
echo " "
cd $src/$gettextv
mkdir -pv $work/gettext
cd $work/gettext
## cd gettext-tools
## echo "gl_cv_func_wcwidth_works=yes" > config.cache
## CC="${CC} ${BUILD64}" CXX="${CXX} ${BUILD64}" \
## 	./configure --prefix=/tools 
$src/$gettextv/configure --prefix=$tools \
                         --disable-shared \
                         --build=$build \
                         --host=$target
                        ## --cache-file=config.cache

make -C gnulib-lib
make -C src msgfmt
cp -v src/msgfmt $tools/bin

## # grep
echo " "
echo " == Compiling $grepv == "
echo " "
cd $src/$grepv
mkdir -pv $work/grep
cd $work/grep
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$grepv/configure --prefix=$tools \
                      --disable-perl-regexp \
                      --without-included-regex \
                      --build=$build \
                      --host=$target

make
make install

## # gzip
echo " "
echo " == Compiling $gzipv == "
echo " "
cd $src/$gzipv
mkdir -pv $work/gzip
cd $work/gzip
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$gzipv/configure --prefix=$tools \
                      --build=$build \
                      --host=$target

make
make install

## # m4
echo " "
echo " == Compiling $m4v == "
echo " "
cd $src/$m4v
mkdir -pv $work/m4
cd $work/m4
## cat > config.cache << EOF
## gl_cv_func_btowc_eof=yes
## gl_cv_func_mbrtowc_incomplete_state=yes
## gl_cv_func_mbrtowc_sanitycheck=yes
## gl_cv_func_mbrtowc_null_arg=yes
## gl_cv_func_mbrtowc_retval=yes
## gl_cv_func_mbrtowc_nul_retval=yes
## gl_cv_func_wcrtomb_retval=yes
## gl_cv_func_wctob_works=yes
## EOF
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$m4v/configure --prefix=$tools \
                    --build=$build \
                    --host=$target
                    ## --cache-file=config.cache

make
make install

## # make-dfsg
echo " "
echo " == Compiling $makev == "
echo " "
cd $src/$makev
mkdir -pv $work/make
cd $work/make
$src/$makev/configure --prefix=$tools \
                    --build=$build \
                    --host=$target
                    ## --cache-file=config.cache

make
make install

## # patch
echo " "
echo " == Compiling $patchv == "
echo " "
cd $src/$patchv
mkdir -pv $work/patch
cd $work/patch
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$patchv/configure --prefix=$tools \
                    --build=$build \
                    --host=$target
                    ## --cache-file=config.cache

make
make install

## # sed
echo " "
echo " == Compiling $sedv == "
echo " "
cd $src/$sedv
mkdir -pv $work/sed
cd $work/sed
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$sedv/configure --prefix=$tools \
                    --build=$build \
                    --host=$target
                    ## --cache-file=config.cache

make
make install

## # tar
echo " "
echo " == Compiling $tarv == "
echo " "
cd $src/$tarv
mkdir -pv $work/tar
cd $work/tar
## cat > config.cache << EOF
## gl_cv_func_wcwidth_works=yes
## gl_cv_func_btowc_eof=yes
## ac_cv_func_malloc_0_nonnull=yes
## ac_cv_func_realloc_0_nonnull=yes
## gl_cv_func_mbrtowc_incomplete_state=yes
## gl_cv_func_mbrtowc_nul_retval=yes
## gl_cv_func_mbrtowc_null_arg=yes
## gl_cv_func_mbrtowc_retval=yes
## gl_cv_func_wcrtomb_retval=yes
## EOF
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$tarv/configure --prefix=$tools \
                    --build=$build \
                    --host=$target
                    ## --cache-file=config.cache

make
make install

## # texinfo
echo " "
echo " == Compiling $texinfov == "
echo " "
cd $src/$texinfov
mkdir -pv $work/texinfo
cd $work/texinfo
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$texinfov/configure --prefix=$tools \
                    --build=$build \
                    --host=$target
                    ## --cache-file=config.cache

make -C tools/gnulib/lib
make -C tools
make
make install

## # xz-utils
echo " "
echo " == Compiling $xzv == "
echo " "
cd $src/$xzv
mkdir -pv $work/xz
cd $work/xz
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$xzv/configure --prefix=$tools \
                    --build=$build \
                    --host=$target
                    ## --cache-file=config.cache

make
make install

## # util-linux
echo " "
echo " == Compiling $utillinuxv == "
echo " "
cd $src/$utillinuxv
mkdir -pv $work/utillinux
cd $work/utillinux
## CC="${CC} ${BUILD64}" ./configure --prefix=/tools \
$src/$utillinuxv/configure --prefix=$tools \
                    --disable-makeinstall-chown \
                    --build=$build \
                    --host=$target
                    ## --cache-file=config.cache

make
make install

## # C'est fini !

echo ""
echo "  == FINISH - stage 2 == :-)"
echo ""
echo "  == Warning: Before you install other packages,
 == add the variable PATH - $sysroot/tools/bin"
# End of file
}

#-------------------------------------------------------------------------------------------------
# bootstrap_debsrc: <buildtype> # Buildtype set to upstream, debian or true_debian 
#
bootstrap_debsrc () {

 buildtype=$1

 case $buildtype in
    upstream|u)
        [ x$VERBOSE = xyes ] || echo "Build set for upstream sources"
	echo " == Disabled == "
	exit 0
        src=$upsrc # | $debsrc # XXX Should match version names
up_init     # Initializes upstream version setup
up_fetch    # Fetch upstream version
#deb_init     # Initializes Debian version
#deb_fetch   # Fetches Debian sources
up_unpack   # Unpack upstream sources
#deb_unpack  # Unpacks Debian sources
rfs_init    # Initializes new RootFS
build_set   # Sets the build for Debian or upstream
build_stage1 # Start the build for Debian or upstream (upstream way)
#build_stage2 # Start the build for Debian or upstream (upstream way)
        ;;
    debian|deb|d)
        [ x$VERBOSE = xyes ] || echo "Build set for Debian sources (upstream)"
        src=$debsrc # | $debsrc # XXX Should match version names
deb_init     # Initializes Debian version
deb_fetch   # Fetches Debian sources
deb_unpack  # Unpacks Debian sources
rfs_init    # Initializes new RootFS
build_set   # Sets the build for Debian or upstream
build_stage1 # Start the build for Debian or upstream (upstream way)
build_stage2 # Start the build for Debian or upstream (upstream way)
        ;;
    true_debian)
        [ x$VERBOSE = xyes ] || echo "Build set for true Debian sources"
        src=$debsrc # | $debsrc # XXX Should match version names
	echo " == Disabled == "
	exit 0
#up_init     # Initializes upstream version setup
#up_fetch    # Fetch upstream version
#deb_init     # Initializes Debian version
#deb_fetch   # Fetches Debian sources
#up_unpack   # Unpack upstream sources
#deb_unpack  # Unpacks Debian sources
#rfs_init    # Initializes new RootFS
#build_set   # Sets the build for Debian or upstream
#build_stage1 # Start the build for Debian or upstream (upstream way)
#build_stage2 # Start the build for Debian or upstream (upstream way)
        ;;
esac
  
}
