#!/bin/bash

MAKEOPTS="-j 2"

# get the sdk; doesn't work since gcc and binutils there too old
#sdk=OpenWrt-SDK-brcm-2.4-for-Linux-`uname -m`
#wget http://downloads.openwrt.org/kamikaze/8.09.2/brcm-2.4/$sdk.tar.bz2
#tar xjvf $sdk.tar.bz2
# extract the toolchain
#mv $sdk/staging_dir/toolchain-mipsel_gcc3.4.6 ../toolchain
#TOOLCHAIN_PATH=`cd ..; pwd`/toolchain

test ! -d openwrt-buildroot \
    && echo "The openwt-buildroot folder is missing." \
    && exit

cd openwrt-buildroot
make distclean
cp config-bak .config
make $MAKEOPTS tools/install
make toolchain/install

TOOLCHAIN_PATH=$PWD/stagind_dir/toolchain-mipsel_gcc-4.4.3_uClibc-0.9.30.1/

PATH=$TOOLCHAIN_PATH/bin:$PATH
CONFIG_ARGS="--host=mipsel-linux --target=mipsel-linux"
PREFIX=$TOOLCHAIN_PATH


# install the iconv lib (needed by log4cxx)
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.9.1.tar.gz
tar xzvf libiconv-1.9.1.tar.gz
cd libiconv-1.9.1/
./configure --prefix=$PREFIX $CONFIG_ARGS
make && make install

cd ..


# install the openssl libs (needed by ariba)
wget http://www.openssl.org/source/openssl-0.9.8i.tar.gz
tar xzvf openssl-0.9.8i.tar.gz 
cd openssl-0.9.8i/
./Configure --prefix=$PREFIX shared linux-generic32
$cross=mipsel-linux-
make CC="${cross}gcc" AR="${cross}ar r" RANLIB="${cross}ranlib"
make install

cd ..


cd ..
rm -rf tmp
