#!/bin/bash
#
# Fetches and crosscompiles the libs needed by Ariba.
# Should be called from the root of an ariba build tree.
#
# Martin Florian <mflorian@lafka.net>
# Mar-2010
#

# this should point to your ariba-brcm-2.4 dir
ARIBA_BRCM_DIR=$PWD/ariba-brcm-2.4

test ! -e ./aclocal.m4 \
    && echo "Should be called from the root of an ariba build tree." \
    && exit

test ! -e $ARIBA_BRCM_DIR/variables \
    && echo "ARIBA_BRCM_DIR should point to the ariba-brcm-2.4 directory." \
    && exit

source $ARIBA_BRCM_DIR/variables

# 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 $TOOLCHAIN_DIR/usr/bin/${CROSS}g++ \
    && echo "The cross compiling toolchain is not where it should be." \
    && exit

test ! -e $PATCHES_DIR/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

# boost 1.42.0 is not working quite well with the ariba configure script

# 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 : ${CROSS}g++ : <cxxflags>${CXXFLAGS} <archiver>${CROSS}ar ;" > tools/build/v2/user-config.jam
patch -p1 < $PATCHES_DIR/boost_system-errors-brcm.patch
patch -p1 < $PATCHES_DIR/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 architecture=mips32 install
cd ..

# gnu multiprecision library
# note: I did "make check" and ran the tests by hand on the router,
# they all passed
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 
CFLAGS=$CFLAGS CXXFLAGS=$CXXFLAGS LDFLAGS=$LDFLAGS ./configure --prefix=$PREFIX $CONFIG_ARGS
make $MAKEOPTS
make install 
cd ..

cd ..

echo
echo "If everything went OK, you can call ariba-brcm-2.4/make_ariba now!"
