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

: ${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}

