#!/bin/bash

set -o errexit	# Exit if any command fails
set -x		# Print commands executed

LLVM_SOURCE=$1
DRAGONEGG_SOURCE=$2
BUILD_DIR=$3

# Change the current directory to the build root. 
cd $BUILD_DIR

# Create links only if target directory or link does not exist.
if [ ! -d $BUILD_DIR/llvm ] ; then
   ln -sf $LLVM_SOURCE $BUILD_DIR/llvm
fi
if [ ! -d $BUILD_DIR/dragonegg ] ; then
   ln -sf $DRAGONEGG_SOURCE $BUILD_DIR/dragonegg
fi

export CC="gcc -m32"
export CXX="g++ -m32"
export LD_LIBRARY_PATH=$HOME/cfarm-32/lib:$LD_LIBRARY_PATH
export CPPFLAGS="-I/$HOME/cfarm-32/include"
export GCC_OPTIONS="--enable-languages=c,c++,fortran,objc,obj-c++ --build=i686-pc-linux-gnu --disable-multilib --enable-targets=all --with-mpfr=$HOME/cfarm-32/ --with-gmp=$HOME/cfarm-32/ --with-mpc=$HOME/cfarm-32/ --with-libelf=$HOME/cfarm-32/"
export LLVM_OPTIONS="--build=i686-pc-linux-gnu"

if [ -e gcc ] ; then
	svn cleanup gcc # In case a previous run was interrupted while checking out gcc
fi
$DRAGONEGG_SOURCE/extras/do_self_strap
