#!/bin/bash

TOOLCHAIN_DIR=$HOME/stuff/openwrt/cross
PATH=$TOOLCHAIN_DIR/bin:$PATH

CONFIG_ARGS="--host=mipsel-linux --target=mipsel-linux"
CXXFLAGS=-mips32

PATCHES="$PWD/ariba-brcm-2.4/patches/"

# check if everythin is in place
test ! -e ./aclocal.m4 \
    && echo "Should be called from the root of an ariba build tree." \
    && exit

test ! -e $TOOCHAIN_PATH/bin/mipsel-linux-g++ \
    && echo "The cross compiling toolchain is not where it should be." \
    && exit

test ! -e $PATCHES/boost_system-errors-brcm.patch \
    && echo "The patches directory is not where it should be." \
    && exit

mkdir build

# setup
export PREFIX=`cd ./build; pwd`
mkdir libs
cd libs

# apache runtime
wget http://apache.linux-mirror.org/apr/apr-1.3.9.tar.gz
tar xvvzf apr-1.3.9.tar.gz
patch -p0 < $PATCHES/apr-crosscompile.patch
cd apr-1.3.9 
./configure --prefix=$PREFIX $CONFIG_ARGS
make && make install 
cd ..

# apache runtime utilities
wget http://apache.linux-mirror.org/apr/apr-util-1.3.9.tar.gz
tar xvvzf apr-util-1.3.9.tar.gz
cd apr-util-1.3.9
./configure --prefix=$PREFIX --with-apr=$PREFIX $CONFIG_ARGS
make && make install 
cd ..

# log4cxx
wget http://ftp.uni-erlangen.de/pub/mirrors/apache/logging/log4cxx/0.10.0/apache-log4cxx-0.10.0.tar.gz
cp ../etc/patch/apache-log4cxx-0.10.0.diff ./
tar xvvzf apache-log4cxx-0.10.0.tar.gz
patch -p0 <apache-log4cxx-0.10.0.diff
cd apache-log4cxx-0.10.0 
LDFLAGS="-lpthread" ./configure --prefix=$PREFIX --with-apr=$PREFIX --with-apr-util=$PREFIX $CONFIG_ARGS
make && make install 
cd ..

# boost 1.42.0
# not working quite well with the ariba configure script
#wget http://switch.dl.sourceforge.net/sourceforge/boost/boost_1_42_0.tar.gz
#tar xvvzf boost_1_42_0.tar.gz
#cd boost_1_42_0
#echo "using gcc : mipsel : mipsel-linux-g++ : <cxxflags>-mips32 <archiver>mipsel-linux-ar ;" > tools/build/v2/user-config.jam
#patch -p1 < $PATCHES/boost_system-brcm.patch
#./bootstrap.sh --libdir=$PREFIX/lib --prefix=$PREFIX --with-libraries=date_time,system,thread,test,regex
#./bjam toolset=gcc-mipsel link=static architecture=mips32 install
#cd ..

# boost 1.39.0
wget http://switch.dl.sourceforge.net/sourceforge/boost/boost_1_39_0.tar.gz
tar xvvzf boost_1_39_0.tar.gz
cd boost_1_39_0
echo "using gcc : mipsel : mipsel-linux-g++ : <cxxflags>-mips32 <archiver>mipsel-linux-ar ;" > tools/build/v2/user-config.jam
patch -p1 < $PATCHES/boost_system-errors-brcm.patch
patch -p1 < $PATCHES/boost_thread-get_nprocs-brcm.patch
./bootstrap.sh --libdir=$PREFIX/lib --prefix=$PREFIX --with-libraries=date_time,system,thread,test,regex
./bjam toolset=gcc-mipsel link=static architecture=mips32 install
cd ..

read

# gnu multiprecision library
wget ftp://ftp.gmplib.org/pub/gmp-4.3.1/gmp-4.3.1.tar.gz
tar xvvzf gmp-4.3.1.tar.gz
cd gmp-4.3.1 
./configure --prefix=$PREFIX $CONFIG_ARGS
make && make install 
cd ..

cd ..
./configure --prefix=$PREFIX --with-boost=$PREFIX CPPFLAGS=-I$PREFIX/include LDFLAGS=-L$PREFIX/lib $CONFIG_ARGS
make && make install

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PREFIX/lib
