#!/usr/bin/env bash

# A script to build and install the C++ library

set -eu -o pipefail

# parse arguments
# -------------------------------------------------------------------
arg() { echo "$1" | sed "s/^${2-[^=]*=}//" | sed "s/:/;/g"; }

build="Release"
prefix=""
tiledb=""
cmake_verbose="false"
no_tiledb_deprecated="false"
werror="false"

while test $# != 0; do
  case "$1" in
  --build=*) build=$(arg "$1");;
  --prefix=*) prefix=$(arg "$1");;
  --tiledb=*) tiledb=$(arg "$1");;
  --cmake-verbose=*) cmake_verbose=$(arg "$1");;
  --no-tiledb-deprecated=*) no_tiledb_deprecated=$(arg "$1");;
  --werror=*) werror=$(arg "$1");;
  esac
  shift
done

# find number of cpus
# -------------------------------------------------------------------
if [ "$(uname)" == "Darwin" ]; then
    nproc=$(sysctl -n hw.ncpu)
else
    nproc=$(nproc)
fi

# set extra cmake options
# -------------------------------------------------------------------
extra_opts=""
if [ "${build}" != "Release" ] && [ -z "${tiledb}" ]; then
  # Debug build of TileDB from source
  extra_opts+=" -DDOWNLOAD_TILEDB_PREBUILT=OFF"
fi

if [ "$(uname -m)" == "aarch64" ]; then
  # build TileDB from source on arm
  extra_opts+=" -DDOWNLOAD_TILEDB_PREBUILT=OFF"
fi



# NOTE: set to true to debug the cmake build
if [ "$cmake_verbose" = "true" ]; then
  # This is _incredibly_ helpful in that it reveals the actual compile lines etc which make itself
  # shows by default but which cmake-driven make hides by default. Use this for any non-trivial
  # cmake debugging.
  extra_opts+=" -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON"

  # TILEDB_WERROR=OFF is necessary to build core with XCode 14; doesn't hurt for XCode 13.
  extra_opts+=" -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DTILEDB_WERROR=OFF -DTILEDBSOMA_ENABLE_WERROR=OFF"

  # Debug cmake find commands
  extra_opts+=" --debug-find"

  # Also (pro-tip), set nproc=1 to get a more deterministic ordering of output lines.
  nproc=1
elif [ "$werror" = "true" ]; then
    extra_opts+=" -DTILEDBSOMA_ENABLE_WERROR=ON"

fi

if [ "$no_tiledb_deprecated" = "true" ]; then
    extra_opts+=" -DTILEDB_REMOVE_DEPRECATIONS=ON"
fi


# set installation path
if [ -n "${prefix}"  ]; then
  extra_opts+=" -DCMAKE_INSTALL_PREFIX=${prefix} -DOVERRIDE_INSTALL_PREFIX=OFF"
else
  extra_opts+=" -DOVERRIDE_INSTALL_PREFIX=ON"
fi

# build with custom tiledb
if [ -n "${tiledb}"  ]; then
  echo "Build with TileDB: $tiledb"
  extra_opts+=" -DFORCE_BUILD_TILEDB=OFF"
  export TileDB_DIR="${tiledb}"
  export LD_LIBRARY_PATH="${tiledb}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
  export DYLD_LIBRARY_PATH="${tiledb}${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}"
fi

# run cmake
# -------------------------------------------------------------------
echo "Building ${build} build"

# cd to the top level directory of the repo
cd "$(dirname "$0")/.."

rm -rf build
mkdir -p build

if [ -z ${TILEDBSOMA_COVERAGE+x} ]; then TILEDBSOMA_COVERAGE=""; fi

TILEDBSOMA_COVERAGE="${TILEDBSOMA_COVERAGE}" cmake -B build -S libtiledbsoma -DCMAKE_BUILD_TYPE=${build} ${extra_opts}
TILEDBSOMA_COVERAGE="${TILEDBSOMA_COVERAGE}" cmake --build build -j ${nproc}
TILEDBSOMA_COVERAGE="${TILEDBSOMA_COVERAGE}" cmake --build build --target install-libtiledbsoma
TILEDBSOMA_COVERAGE="${TILEDBSOMA_COVERAGE}" cmake --build build/libtiledbsoma --target build_tests -j ${nproc}
