#!/bin/bash

#
# install-boost
#
# Copyright (C) 2009-11 by RStudio, Inc.
#
# This program is licensed to you under the terms of version 3 of the
# GNU Affero General Public License. This program is distributed WITHOUT
# ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
# AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
#
#

set -e

# install dir 
INSTALL_DIR=`pwd`

# determine platform
PLATFORM=`uname`

# vars
BOOST_VERSION_NUMBER=1.44.0
BOOST_VERSION=boost_1_44_0
BOOST_TAR=$BOOST_VERSION.tar.bz2
BOOST_BUILD_DIR=boost-build

# install if we aren't already installed
if ! test -e /usr/local/include/boost
then
   # download boost
   BOOST_URL=http://sourceforge.net/projects/boost/files/boost/$BOOST_VERSION_NUMBER/$BOOST_TAR/download?use_mirror=autoselect
   if [ "$PLATFORM" == "Darwin" ]
   then
      curl -L $BOOST_URL > $BOOST_TAR
   else
      wget $BOOST_URL -O $BOOST_TAR
   fi
   
   # remove existing boost installation
   sudo rm -rf /usr/local/include/boost
   sudo rm -rf /usr/local/lib/libboost_*

   # untar source (remove existing)
   rm -rf $BOOST_BUILD_DIR
   mkdir -p $BOOST_BUILD_DIR
   cd $BOOST_BUILD_DIR
   tar --bzip2 -xf ../$BOOST_TAR

   # bootstrap boost
   cd $BOOST_VERSION
   ./bootstrap.sh  
   
   # special variation of build for osx (multi-arch, tied to 10.5)
   if [ "$PLATFORM" == "Darwin" ]
   then
     sudo ./bjam variant=release threading=multi link=static install
   else
     sudo ./bjam variant=release install
   fi

   # go back to the original install dir and remove build dir
   cd $INSTALL_DIR
   sudo rm -rf $BOOST_TAR
   sudo rm -rf $BOOST_BUILD_DIR

else

   echo "boost already installed in /usr/local (remove /usr/local/include/boost to force re-install)"

fi

# back to install dir
cd $INSTALL_DIR
