#!/usr/bin/env bash

#
# install-boost
#
# Copyright (C) 2022 by RStudio, PBC
#
# Unless you have received this program directly from RStudio pursuant
# to the terms of a commercial license agreement with RStudio, then
# 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

source "$(dirname "${BASH_SOURCE[0]}")/../tools/rstudio-tools.sh"
section "Installing Boost"

# install dir
INSTALL_DIR=$(pwd)

# determine platform
PLATFORM=$(uname)

# constants
BOOST_VERSION_NUMBER=1.69.0
BOOST_VERSION=boost_1_69_0
BOOST_TAR=$BOOST_VERSION.tar.bz2
BOOST_BUILD_DIR=boost-build
BOOST_MODULES=(
   algorithm asio array bind build chrono circular_buffer config context crc
   date_time filesystem foreach format function interprocess iostreams
   lambda lexical_cast optional program_options predef property_tree random range ref
   regex scope_exit signals signals2 smart_ptr spirit string_algo system
   test thread tokenizer type_traits typeof unordered utility variant
)

# compute boost dir
BOOST_DIR="${RSTUDIO_TOOLS_ROOT}/boost/${BOOST_VERSION}"

if [ "${PLATFORM}" = "Darwin" ]; then
   TOOLSET="toolset=clang-darwin"
fi

# install if we aren't already installed
if [ -e "${BOOST_DIR}" ]; then
   info "Boost ${BOOST_VERSION_NUMBER} already installed at '${BOOST_DIR}'"
   exit 0
fi

# re-run as root if necessary
sudo-if-necessary-for "${RSTUDIO_TOOLS_ROOT}" "$@"

# move to tempdir for build
cd "$(mktemp -d)"

# download boost
BOOST_URL=https://s3.amazonaws.com/rstudio-buildtools/Boost/boost_1_69_0.tar.bz2
download "${BOOST_URL}" "${BOOST_TAR}"

# remove existing boost directory
rm -rf "${BOOST_DIR}"

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

# change to boost version folder
cd $BOOST_VERSION

# bootstrap boost
./bootstrap.sh

# build bcp helper
./b2 "${TOOLSET}" tools/bcp

# copy back to root
cp dist/bin/bcp bcp

# use bcp to copy to rstudio folder (use custom namespace)
mkdir -p rstudio

# shellcheck disable=SC2086
./bcp --namespace=rstudio_boost --namespace-alias ${BOOST_MODULES[*]} config build rstudio

# move to rstudio folder
cd rstudio

# bootstrap again
./bootstrap.sh

# special variation of build for osx
if [ "$PLATFORM" = "Darwin" ]; then

   BJAM_CXXFLAGS="cxxflags=-fPIC -std=c++11 -mmacosx-version-min=10.12"
   BJAM_LDFLAGS=""

   ./bjam                   \
      "${BOOST_BJAM_FLAGS}" \
      --prefix="$BOOST_DIR" \
      "${TOOLSET}"          \
      "${BJAM_CXXFLAGS}"    \
      "${BJAM_LDFLAGS}"     \
      variant=release       \
      threading=multi       \
      link=static           \
      install

else
   # set up cxxflags
   cxxflags="-fPIC -std=c++11"
   if grep -siq jammy /etc/os-release; then
      cxxflags="${cxxflags} -DPTHREAD_STACK_MIN=$(getconf PTHREAD_STACK_MIN)"
   fi

   # plain old build for other platforms
   ./bjam                    \
      "${BOOST_BJAM_FLAGS}"  \
      --prefix="$BOOST_DIR"  \
      variant=release        \
      cxxflags="${cxxflags}" \
      install

fi

# rename libraries in the boost install dir
cd "$BOOST_DIR/lib"
for file in librstudio*; do
   src=$file
   tgt="${src//rstudio_/}"
   mv "$src" "$tgt"
done

for file in libboost*.so; do
   ln -nfs "$file.$BOOST_VERSION_NUMBER" "$file"
done

# go back to the original install dir and remove build dir
cd "${INSTALL_DIR}"
rm -rf "${BOOST_BUILD_DIR}"

info "Boost ${BOOST_VERSION} installed to '${BOOST_DIR}'"

