#! /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 () {
    tar xjf nfs-utils-1.2.7.tar.bz2
    cd nfs-utils-1.2.7
    bunzip2 -c ../nfs-utils-1.2.6-wrapperfix-patch.bz2 | patch -p1 -b
    bunzip2 -c ../nfs-utils-1.2.6-missing-services-patch.bz2 | patch -p1 -b
    sed -i -e 's/"-ltirpc/"-ltirpc -lpthread/' configure

    # First the "light" build:
    CONFIG_SQLITE3_TRUE='#' CONFIG_NFSDCLD_TRUE='#' ./configure \
	    --disable-ipv6 --disable-nfsv4 --disable-nfsv41 --disable-tirpc \
	    --prefix=/usr --sysconfdir=/etc --disable-gss --disable-uuid \
	    --disable-caps --disable-mountconfig --disable-libmount-mount \
	    --with-statduser=nobody --disable-shared --with-tcp-wrappers
    # FIXME: build errors on arm, statd can't find sm_inter.h (support/nsm)
    # and mountd can't find mount.h (support/export).
    make
    rm -f utils/exportfs/exportfs
    make -C utils/exportfs exportfs LIBWRAP=
    strip -R .note -R .comment utils/exportfs/exportfs utils/nfsd/nfsd \
	    utils/mountd/mountd
    mkdir -p ../nfs3-server/sbin
    cp utils/exportfs/exportfs ../nfs3-server/sbin/
    cp utils/nfsd/nfsd ../nfs3-server/sbin/rpc.nfsd
    cp utils/mountd/mountd ../nfs3-server/sbin/rpc.mountd

    # Then the "heavy" build:
    make clean
    # FIXME: multiple problems on Etch(-SPARC):
    #        - Must compile and install libtirpc in /usr/local
    #        - Must use --with-tirpcinclude=/usr/local/include/tirpc
    #        - Must use --disable-nfsdcltrack (this feature requires inotify)
    #        - Must hack configure to disable second inotify check
    #        - /usr/lib/libnfsidmap.la contains troublesome -lldap dependency
    ./configure --enable-ipv6 --enable-nfsv4 --enable-nfsv41 --enable-tirpc \
	    --prefix=/usr --sysconfdir=/etc --disable-gss --disable-uuid \
	    --disable-caps --disable-mountconfig --disable-libmount-mount \
	    --with-statduser=nobody --disable-shared --with-tcp-wrappers
    # FIXME: Lenny has problem with undefined u_char type in libevent.h
    # (need to add "#include <sys/types.h>")
    make
    rm -f utils/exportfs/exportfs
    make -C utils/exportfs exportfs LIBWRAP=
    rm -f utils/idmapd/idmapd
    make -C utils/idmapd idmapd NFS4NOBODY_GROUP=\"nogroup\" \
	    LIBEVENT='/usr/lib/libevent.a -lrt'
    strip -R .note -R .comment utils/exportfs/exportfs utils/nfsd/nfsd \
	    utils/mountd/mountd utils/idmapd/idmapd utils/mount/mount.nfs
    mkdir -p ../nfs4-server/sbin ../nfs4-client/sbin
    cp utils/exportfs/exportfs ../nfs4-server/sbin/
    cp utils/nfsd/nfsd ../nfs4-server/sbin/rpc.nfsd
    cp utils/mountd/mountd ../nfs4-server/sbin/rpc.mountd
    cp utils/idmapd/idmapd ../idmapd/sbin/rpc.idmapd
    cp utils/mount/mount.nfs ../nfs4-client/sbin/

    cd ..
    rm -rf nfs-utils-1.2.7
}

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
