#!/bin/sh

[ -f $(dirname $0)/colors.sh ] && source $(dirname $0)/colors.sh

PLATFORM="emscripten"

SRC_DIR="boost-sdk"
INSTALL_DIR="build/boost"

SRC_PATH="$(pwd)/$SRC_DIR"
INSTALL_PATH="$(pwd)/$INSTALL_DIR"
JAM_CONFIG_PATH="$(pwd)/configs/$PLATFORM.jam"



main() {
[ ! -d ${SRC_PATH} -o $# -ge 1 ] \
  && {
    case "$1" in
      "github"|"clone")
        shift
        [ -d ${SRC_PATH} -a "$1" != "force" ] \
        && {
          echo "${RED}Target directory exists.${WHITE} Will not proceed without ${YELLOW}'force'${RESTORE}"
          exit 1
        }
        [ ! -d ${SRC_PATH} -o "$1" = "force" ] \
        && {
          get_boost_github ${SRC_PATH} || exit 1
        }
        ;;
      "archive"|"source")
        shift
        [ -d ${SRC_PATH} -a "$1" != "force" ] \
        && {
          echo "${RED}Target directory exists.${WHITE} Will not proceed without ${YELLOW}'force'${RESTORE}"
          exit 1
        }
        [ ! -d ${SRC_PATH} -o "$1" = "force" ] \
        && {
          get_boost_source ${SRC_PATH} || exit 1
        }
        ;;
      "")
        [ -d ${SRC_PATH} ] \
        || {
          echo "* Missing $(basename ${SRC_PATH}) Downloading..."
          get_boost_source ${SRC_PATH} || exit 1
        }
        ;;
      *)
        echo "Unknown parameter: $1"
        exit 1
        ;;
    esac
  }

if [ ! -d "$SRC_PATH" ]; then
  echo "SOURCE NOT FOUND!"
  exit 1
fi

if [ -z "$EMSCRIPTEN" ]; then
  echo "EMSCRIPTEN MUST BE DEFINED!"
  exit -1  
fi

cd $EMSCRIPTEN; ./embuilder.py build zlib

# ---

cd "$SRC_PATH"

rm -rf bjam
rm -rf b2
rm -rf project-config.jam
rm -rf bootstrap.log
rm -rf bin.v2

export NO_BZIP2=1 #bc it's supplied by emscripten but b2 will fail to find it


#  --with-libraries=atomic,signals,timer,system,filesystem,thread,date_time,chrono,regex,serialization,program_options,locale \


./bootstrap.sh \
  --with-libraries=system,thread,chrono \
2>&1

if [ $? != 0 ]; then
  echo "ERROR: boostrap FAILED!"
  exit 1
fi

cat "$JAM_CONFIG_PATH" >> project-config.jam

# ---
# Clean 
rm -rf "$INSTALL_PATH"
mkdir "$INSTALL_PATH"


HOST_NCORES=$(shell nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 1)


# threading=single \
./b2 -q -a -j$HOST_NCORES    \
  toolset=clang-emscripten   \
  link=static                \
  optimization=space         \
  variant=release            \
  stage                      \
  --stagedir="$INSTALL_PATH" \
  2>&1

unset NO_BZIP2

if [ $? != 0 ]; then
  echo "ERROR: b2 FAILED!"
  exit 1
fi

# ---

cd "$INSTALL_PATH"
ln -s "$SRC_PATH" include

}


get_boost_github() {
  local SDK_PATH=$1
  [ -z ${SDK_PATH} ] && { echo "get_boost_github: Missing SDK_PATH parameter..."; return 1; }

  [ "$(basename ${SDK_PATH})" = "boost-sdk" ] \
  && {
    rm -fr ${SDK_PATH}
    SDK_PATH=$(dirname ${SDK_PATH})
  }
  
  [ -d ${SDK_PATH} ] || { echo "get_boost_github: Missing directory: ${SDK_PATH}"; return 1; }

  echo "Downloading boost from GitHub..."
  git clone --recursive https://github.com/boostorg/boost.git --branch "boost-1.72.0" "${SDK_PATH}/boost-sdk" \
  || {
    echo "Download failed"
    return 1
  }
  return 0
}


get_boost_source() {
  local SDK_PATH=$1
  [ -z ${SDK_PATH} ] && { echo "get_boost_source: Missing SDK_PATH parameter..."; return 1; }

  [ "$(basename ${SDK_PATH})" = "boost-sdk" ] \
  && {
    rm -fr ${SDK_PATH}
    SDK_PATH=$(dirname ${SDK_PATH})
  }
  
  [ -d ${SDK_PATH} ] || { echo "get_boost_source: Missing directory: ${SDK_PATH}"; return 1; }

  # Github source is missing stuff
  #local DL_URL="https://github.com/boostorg/boost/archive/boost-1.72.0.tar.gz"
  local DL_URL=" https://boostorg.jfrog.io/artifactory/main/release/1.72.0/source"
  local DL_FILE="boost_1_72_0.tar.gz"

  check_archive "${SDK_PATH}/${DL_FILE}" \
  && {
    echo "${DL_FILE} is already in ${SDK_PATH}"
  } \
  || {
    echo "Downloading boost source..."
    download_source ${DL_URL}/${DL_FILE} ${SDK_PATH} || return 1
  }

  mkdir ${SDK_PATH}/boost-sdk
  tar -C ${SDK_PATH}/boost-sdk --strip-components=1 -xvf "${SDK_PATH}/${DL_FILE}" || return 1

  return 0
}


download_source() { # url, destination
  local DL_URL=$1
  local SDK_PATH=$2

  which wget 2>&1 > /dev/null \
  && {
    wget -N -P ${SDK_PATH} ${DL_URL} \
    && {
      return 0
    } \
    || {
      echo "wget download failed... check download location..."
      return 1
    }
  }

  which curl 2>&1 > /dev/null \
  && {
    (
      cd ${SDK_PATH}
      curl -L -O ${DL_URL} \
      && {
      return 0
      } \
      || {
        echo "curl download failed... check download location..."
        return 1
      }
    )
  }

  [ -f "${SDK_PATH}/$(basename ${DL_URL})" ] \
  || {
    echo "Both wget and curl failed. Don't know how to proseed."
    return 1
  }
}


main "$@"