#! /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 firefox-2.0.0.20-source.tar.bz2
    cd mozilla
    bunzip2 -c ../firefox-2.0.20-armel.patch.bz2 | patch -p1 -b
    bunzip2 -c ../firefox-2.0.20-mips.patch.bz2 | patch -p1 -b
    ./configure --prefix=/usr/local --localstatedir=/var --sysconfdir=/etc \
	    --with-default-mozilla-five-home=/usr/local/lib/firefox \
	    --with-system-jpeg --with-system-zlib --with-system-png \
	    --without-system-nspr --without-system-nss \
	    --enable-default-toolkit=gtk --enable-application=browser \
	    --enable-canvas --disable-mailnews --disable-ldap --enable-xprint \
	    --disable-gnomevfs --disable-gnomeui --enable-crypto \
	    --enable-xinerama --enable-single-profile \
	    --disable-profile-sharing --disable-composer \
	    --enable-extensions=default --disable-installer --disable-updater \
	    --disable-tests --enable-static --disable-shared \
	    --enable-optimize="$CFLAGS -Wl,-rpath -Wl,/usr/local/lib/firefox"
    # FIXME: can't cope with 3.x kernels, need to symlink to
    # security/coreconf/Linux2.6.mk.
    # FIXME: needs old version of fontconfig without dependency on
    # FT_Select_Size symbol.
    # FIXME: undisclosed dependency on libXi.
    make || true
    cd browser/app
    # Only necessary on i386/amd64?
    ar x /usr/lib/libX11.a RegstFlt.o
    g++ -o firefox-bin -Wl,-rpath -Wl,/usr/local/lib/firefox \
	    nsBrowserApp.o nsStaticComponents.o RegstFlt.o -L../../dist/bin \
	    -L../../dist/lib -L../../dist/lib/components \
	    ../../dist/lib/libxulapp_s.a -L../../dist/bin -lmozjs \
	    -L../../dist/bin -lxpcom -lxpcom_core -L../../dist/lib -lplds4 \
	    -lplc4 -lnspr4 -lpthread -ldl -L/usr/lib -lgtk -lglib -lXext \
	    -lX11 -lm ../../dist/lib/components/libxpcom_compat_c.a \
	    ../../dist/lib/components/libxpconnect.a \
	    ../../dist/lib/components/libuconv.a \
	    ../../dist/lib/components/libucvmath.a \
	    ../../dist/lib/components/libi18n.a \
	    ../../dist/lib/components/libmork.a \
	    ../../dist/lib/components/libstoragecomps.a \
	    ../../dist/lib/components/libnecko.a \
	    ../../dist/lib/components/libnecko2.a \
	    ../../dist/lib/components/libjar50.a \
	    ../../dist/lib/components/libpref.a \
	    ../../dist/lib/components/libcaps.a \
	    ../../dist/lib/components/librdf.a \
	    ../../dist/lib/components/libhtmlpars.a \
	    ../../dist/lib/components/libgfxps.a \
	    ../../dist/lib/components/libgfxxprint.a \
	    ../../dist/lib/components/libgfx_gtk.a \
	    ../../dist/lib/components/libimglib2.a \
	    ../../dist/lib/components/libgkplugin.a \
	    ../../dist/lib/components/libwidget_gtk.a \
	    ../../dist/lib/components/libgklayout.a \
	    ../../dist/lib/components/libdocshell.a \
	    ../../dist/lib/components/libembedcomponents.a \
	    ../../dist/lib/components/libwebbrwsr.a \
	    ../../dist/lib/components/libeditor.a \
	    ../../dist/lib/components/libtxmgr.a \
	    ../../dist/lib/components/libcomposer.a \
	    ../../dist/lib/components/libnsappshell.a \
	    ../../dist/lib/components/liboji.a \
	    ../../dist/lib/components/libaccessibility.a \
	    ../../dist/lib/components/libchrome.a \
	    ../../dist/lib/components/libmozfind.a \
	    ../../dist/lib/components/libappcomps.a \
	    ../../dist/lib/components/libfileview.a \
	    ../../dist/lib/components/libremoteservice.a \
	    ../../dist/lib/components/libcommandlines.a \
	    ../../dist/lib/components/libtoolkitcomps.a \
	    ../../dist/lib/components/libpipboot.a \
	    ../../dist/lib/components/libpipnss.a \
	    ../../dist/lib/components/libpippki.a \
	    ../../dist/lib/components/libcookie.a \
	    ../../dist/lib/components/libxmlextras.a \
	    ../../dist/lib/components/libautoconfig.a \
	    ../../dist/lib/components/libtransformiix.a \
	    ../../dist/lib/components/libuniversalchardet.a \
	    ../../dist/lib/components/libwebsrvcs.a \
	    ../../dist/lib/components/libauth.a \
	    ../../dist/lib/components/libpermissions.a \
	    ../../dist/lib/components/libsearchservice.a \
	    ../../dist/lib/components/libbrowserdirprovider.a \
	    ../../dist/lib/components/libbrowsercomps.a \
	    ../../dist/lib/libunicharutil_s.a ../../dist/lib/libucvutil_s.a \
	    ../../dist/lib/libgtksuperwin.a ../../dist/lib/libgtkxtbin.a \
	    ../../dist/lib/libgfxshared_s.a ../../dist/lib/libgfxft2_s.a \
	    ../../dist/lib/libgfxpsshar.a ../../dist/lib/libxlibrgb.a \
	    ../../dist/lib/libxprintutil.a ../../dist/lib/libgkgfx.a \
	    ../../dist/lib/libjsj.a ../../dist/lib/libxulapp_s.a -lpng -ljpeg \
	    -lz -L-L../../dist/bin -L../../dist/lib -lcrmf -lsmime3 -lssl3 \
	    -lnss3 -lsoftokn3 -lmozcairo -lmozlibpixman -lfontconfig \
	    -lfreetype -lXinerama -lXt -lXp -L../../dist/lib -lxpcom_compat  
    strip -R .note -R .comment firefox-bin
    cd ../..
    # FIXME
    #cd ..
    #rm -rf mozilla
}

(cd freetype2-old; ./build.sh)

mkdir -p firefox/bin

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
