#! /bin/bash

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

rebuild () {
    tar xjf spice-protocol-0.12.5.tar.bz2
    cd spice-protocol-0.12.5
    ./configure --prefix=/usr/local --datarootdir=/usr/local/lib
    sudo make install
    cd ..
    rm -rf spice-protocol-0.12.5

    # N.B. Must use host OpenSSL, not /usr/local version.
    tar xjf spice-0.12.2.tar.bz2
    cd spice-0.12.2
    bunzip2 -c ../spice-0.8.1-mwmhint-patch.bz2 | patch -p1 -b
    # FIXME 0.12.2: bogus(?) requirement for glib >= 2.22, not available on
    # < squeeze.  Hacked configure to avoid.
    ./configure --prefix=/usr/local --sysconfdir=/etc --localstatedir=/var \
	    --with-pic --without-sasl --disable-werror --disable-smartcard
    # FIXME lenny: errors on -Wno-suggest-attribute={const,pure}
    make -C spice-common
    # errors, need to prevent accidental link against SSL in /usr/local.
    make -C client
    cd client
    gcc -o spicec *.o x11/*.o ../spice-common/common/.libs/libspice-common.a \
	    ../spice-common/common/.libs/libspice-common-client.a \
	    -Wl,-Bstatic -lssl -lcrypto -lstdc++ -Wl,-Bdynamic -ljpeg -lm \
	    -ldl -lpthread -lrt -lz -lasound -lX11 -lXext -lXrandr -lXfixes \
	    -lXinerama /usr/local/lib/libpixman-1.so \
	    /usr/local/lib/libcelt051.so
    strip -R .note -R .comment spicec
    [ -n "`which upx`" ] && upx -9 spicec
    mkdir -p ../../spicec/bin
    cp spicec ../../spicec/bin/

    # Server: i386 and amd64 only.
    # Need ~/openssl-0.9.8r/crypto/bn/bn_prime.o and
    # ~/openssl-0.9.8r/crypto/rsa/rsa_gen.o to dodge the OpenSSL dependency.
    # Can't just steal them from /usr/lib/libcrypto.a, need -fPIC compilation.
    # Patch in to am_libspice_server_la_OBJECTS in server/Makefile.
    # FIXME: Have to omit "--whole-archive" linker directive when including
    # libspice-common*.a in order to drop references to EVP_PKEY_cmp, which
    # could conceivably mean that certain functions in the public headers
    # aren't actually available (but this probably isn't the case).
    # FIXME: have to back out change to server/reds.c that uses g_strlcpy
    # instead of strncpy, in order to remove glibc dependency that won't fly
    # due to static library compiled without -fPIC.
    #cd ..
    #rm -rf spice-0.12.0
}

if [ ! -e /usr/local/lib/libcelt051.so.0 ]; then
    if [ -d ../celt ]; then
	(cd ../celt; ./build.sh)
    else
	echo "Missing libcelt 0.5.1 library.  This ain't gonna work." 1>&2
	exit 1
    fi
fi

if [ -z "`which upx`" ]; then
    if [ -d ../upx ]; then
	(cd ../upx; ./build.sh)
    else
	echo "UPX not present, will not be able to compress spicec binary." 1>&2
    fi
fi

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
