#! /bin/bash

trap "echo '*** BUILD FAILED! ***' 1>&2" ERR
set -ex -o pipefail
umask 022
export PATH='/bin:/usr/bin:/usr/local/bin:~/diet-pc/tools'
export CFLAGS=${CFLAGS:--Os}
export CXXFLAGS=${CXXFLAGS:--Os}

rebuild () {
    rm -rf glibc-2.7 glibc-build
    tar xjf glibc-2.7.tar.bz2
    cd glibc-2.7
    tar xjf ../glibc-ports-2.7.tar.bz2
    ln -s glibc-ports-2.7 ports
    gunzip -c ../glibc_2.7-18lenny4.diff.gz | patch -p1
    ln -s debian/patches
    mv manual/Makefile manual/Makefile.old
    quilt delete hppa/submitted-lt.diff
    quilt delete i386/local-linuxthreads-gscope.diff
    quilt delete amd64/local-linuxthreads-gscope.diff
    quilt delete m68k/local-pthread_lock.diff
    quilt delete sparc/local-sparcv9-target.diff
    quilt delete all/local-pthread-manpages.diff
    quilt delete any/local-linuxthreads-gscope.diff
    quilt delete any/local-linuxthreads-fd.diff
    quilt delete any/local-linuxthreads-signals.diff
    quilt delete any/local-linuxthreads-semaphore_h.diff
    quilt delete any/local-linuxthreads-tst-sighandler.diff
    quilt delete any/local-linuxthreads-weak.diff
    quilt push -a || true
    echo '*** Expected failure: hurd-i386/submitted-libc_once.diff' 1>&2
    quilt push -f || true
    quilt refresh
    quilt push -a || true
    echo '*** Expected failure: any/local-notls.diff' 1>&2
    quilt push -f || true
    quilt refresh
    quilt push -a || true
    echo '*** Expected failure: any/local-sysctl.diff' 1>&2
    quilt push -f || true
    quilt refresh
    quilt push -a
    ed -s io/Makefile 2>/dev/null <<EOF
/^static-only-routines =/+1
s/\Wfstatat64\W/ /
w
q
EOF
    mkdir ../glibc-build
    cd ../glibc-build
    BUILD=`cpp -dumpmachine`
    PORTS='glibc-ports-2.7,'
    LINKER_NAME='ld-linux.so.2'
    case "$BUILD" in
    arm-linux-gnu)
	BUILD="$BUILD --without-fp"
	;;
    arm-linux-gnueabi)
	LINKER_NAME='ld-linux.so.3'
	;;
    ppc-linux-gnu|powerpc-linux-gnu|mips*-linux-gnu)
	LINKER_NAME='ld.so.1'
	;;
    *86-linux-gnu)
	PORTS=
	;;
    x86_64-linux-gnu)
	PORTS=
	LINKER_NAME='ld-linux-x86-64.so.2'
	ln -sf lib ../libc6-minimal/lib64
	;;
    esac
    ../glibc-2.7/configure --prefix=/usr --sysconfdir=/etc \
	    --localstatedir=/var --enable-add-ons=${PORTS}nptl \
	    --disable-profile --enable-kernel=2.6.0 --with-tls \
	    --with-headers=/usr/include --build=$BUILD
    make
    strip -R .note -R .comment libc.so.6 elf/{ldconfig,$LINKER_NAME} \
	    crypt/libcrypt.so.1 dlfcn/libdl.so.2 math/libm.so.6 \
	    nis/libnsl.so.1 resolv/{libnss_dns.so.2,libresolv.so.2} \
	    nss/libnss_files.so.2 nptl/libpthread.so.0 rt/librt.so.1 \
	    login/libutil.so.1 iconvdata/*.so
    mkdir -p ../libc6-minimal/lib ../libc6-minimal/bin ../ldconfig/sbin
    cp libc.so.6 elf/$LINKER_NAME crypt/libcrypt.so.1 dlfcn/libdl.so.2 \
	    math/libm.so.6 nis/libnsl.so.1 \
	    resolv/{libnss_dns.so.2,libresolv.so.2} nss/libnss_files.so.2 \
	    nptl/libpthread.so.0 rt/librt.so.1 login/libutil.so.1 \
	    ../libc6-minimal/lib/
    cp /lib/libgcc_s.so.1 ../libc6-minimal/lib/
    chmod 755 ../libc6-minimal/lib/libgcc_s.so.1
    strip -R .note -R .comment ../libc6-minimal/lib/libgcc_s.so.1
    chmod u+w elf/ldd
    ed -s elf/ldd >/dev/null <<EOF
1s/bash/sh/
/set -o pipefail/
s/set -o pipefail/true/
w
q
EOF
    cp elf/ldd ../libc6-minimal/bin/
    cp elf/ldconfig ../ldconfig/sbin/
    cp iconvdata/{ISO8859-1,UNICODE}.so ../iconv-data-minimal/lib/gconv/
    cd ..
    rm -rf glibc-2.7 glibc-build
}

ln -sf /proc/mounts libc6-minimal/etc/mtab
chmod 0600 libc6-minimal/etc/shadow

while read FN; do
    eval "[ -e $FN ] || rebuild"
done < MANIFEST

ARCH=`ipkg -V 0 print_architecture | tail -n 1 | cut -f2 -d' '`
for CTRLFILE in */CONTROL/control; do
    sed -i -e "s/__ARCH__/$ARCH/g" $CTRLFILE
    sudo ipkg-build -o root -g root `dirname $CTRLFILE`/..
done
