#!/bin/bash
#
# Fetches and crosscompiles log4cxx and the libs it needs.
# They compile, but ultimately don't work.
#
# 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

# apache runtime
wget http://apache.linux-mirror.org/apr/apr-1.3.9.tar.gz
tar xvvzf apr-1.3.9.tar.gz
cd apr-1.3.9
./configure --prefix=$PREFIX $CONFIG_ARGS \
            ac_cv_file__dev_zero="yes" \
            ac_cv_func_setpgrp_void="yes" \
            apr_cv_process_shared_works="yes" \
            apr_cv_mutex_robust_shared="no" \
            apr_cv_tcp_nodelay_with_cork="yes" \
            ac_cv_sizeof_struct_iovec="8" \
            apr_cv_mutex_recursive="yes" \
            CFLAGS="$CFLAGS" \
            CXXFLAGS="$CXXFLAGS" \
            LDFLAGS="$LDFLAGS"

make $MAKEOPTS
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
CFLAGS=$CFLAGS CXXFLAGS=$CXXFLAGS LDFLAGS=$LDFLAGS ./configure --prefix=$PREFIX --with-apr=$PREFIX $CONFIG_ARGS --with-iconv=$TOOLCHAIN_DIR
make $MAKEOPTS
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 
CFLAGS=$CFLAGS CXXFLAGS=$CXXFLAGS LDFLAGS=$LDFLAGS ./configure --prefix=$PREFIX --with-apr=$PREFIX --with-apr-util=$PREFIX $CONFIG_ARGS
rm -rf src/examples
sed -i s/examples// src/Makefile
make $MAKEOPTS
make install 
cd ..
