# SPDX-FileCopyrightText: 2024 Petros Koutsolampros
#
# SPDX-License-Identifier: GPL-3.0-only

if test -z "$CMAKE"; then CMAKE="`which cmake`"; fi
if test -z "$CMAKE"; then CMAKE=/Applications/CMake.app/Contents/bin/cmake; fi
if test ! -f "$CMAKE"; then echo "no ‘cmake’ command found"; exit 1; fi

: ${R_HOME=`R RHOME`}
if test -z "${R_HOME}"; then
  echo "could not determine R_HOME"
  exit 1
fi
CC=`"${R_HOME}/bin/R" CMD config CC`
CFLAGS=`"${R_HOME}/bin/R" CMD config CFLAGS`
CPPFLAGS=`"${R_HOME}/bin/R" CMD config CPPFLAGS`
LDFLAGS=`"${R_HOME}/bin/R" CMD config LDFLAGS`

"${R_HOME}/bin/Rscript" -e 'library(Rcpp); compileAttributes(".")'

cd src
mkdir -p build-Release && cd build-Release
$CMAKE ../libs \
  -DCMAKE_VERBOSE_MAKEFILE=ON \
  -DCMAKE_BUILD_TYPE=Release \
  -DBUILD_SHARED_LIBS:bool=OFF \
  -DCMAKE_POSITION_INDEPENDENT_CODE:bool=ON

# remove .NOTPARALLEL to stop CMD from complaining
unamestr=$(uname)
if [ "$unamestr" = Darwin ]; then
  find . -name Makefile -exec sed -i '' -e '/^.NOTPARALLEL:$/s/^/#/' {} \;
else
  find . -name Makefile -exec sed -i '/^.NOTPARALLEL:$/s/^/#/' {} \;
fi

${MAKE}

