if [ "$SAGE_LOCAL" = "" ]; then
   echo "SAGE_LOCAL undefined ... exiting";
   echo "Maybe run 'sage -sh'?"
   exit 1
fi

cd src

echo "Running boost bootstrap"
# We provide the toolset as the only toolset properly auto-detected are
# gcc (or icc) on linux
# clang on OS X
# but not clang on linux for example
# First sanitize toolset name from CC, it assume something of the form
# CC=/path/to/CC_name-version at worst.
# Please do not mix CC and CXX from different vendors.
C_COMPILER_NAME=${CC##*/}
C_COMPILER_NAME=${C_COMPILER_NAME%-*}
BOOST_TOOLSET=""
if [ "$UNAME" = "Darwin" ]; then
    # On darwin provided the compiler is clang, default is fine.
    # Remember that /usr/bin/gcc is clang.
    if [ "$(which $C_COMPILER_NAME)" != "/usr/bin/gcc" -a "$C_COMPILER_NAME" != "clang" ]; then
        BOOST_TOOLSET="--with-toolset=$C_COMPILER_NAME"
    fi
else
    BOOST_TOOLSET="--with-toolset=$C_COMPILER_NAME"
fi

./bootstrap.sh $BOOST_TOOLSET
if [[ $? -ne 0 ]]; then
    echo >&2 "Failed to bootstrap boost."
    exit 1
fi

echo "Building boost"
# By default this is populated by a system value.
# If the boost build system (b2, bjam and associated files under /usr/share/boost-build)
# has been installed system wide it can cause interference.
# The build will fail purely and simply without much of an explanation.
# see http://trac.sagemath.org/ticket/20776 for details.
export BOOST_BUILD_PATH="${SAGE_LOCAL}"/share/boost-build
./b2
if [[ $? -ne 0 ]]; then
    echo >&2 "Failed to build boost."
    exit 1
fi

echo "Clean out old boost headers and libraries"
rm -rf "$SAGE_LOCAL"/include/boost
rm -rf "$SAGE_LOCAL"/lib/libboost*

echo "Installing boost"
./b2 install --prefix="$SAGE_LOCAL"
if [[ $? -ne 0 ]]; then
    echo >&2 "Failed to install boost."
    exit 1
fi

