# We make sure that the R and Armadillo bindings are compatible (i.e., messages)
# this is why we add the R and cpp4r path
cpp4r_INCLUDE_PATH=`${R_HOME}/bin/Rscript -e "cat(system.file('include', package = 'cpp4r'))"`
INST_INCLUDE_PATH="./inst/include/armadillo4r"

# Check if cpp4r_INCLUDE_PATH is empty
if [ -z "$cpp4r_INCLUDE_PATH" ]; then
  echo "Error: cpp4r include path is empty. Please ensure the cpp4r package is installed."
  exit 1
fi

PKG_CPPFLAGS="-I${INST_INCLUDE_PATH} -I${cpp4r_INCLUDE_PATH}"

# Debugging: Print the values of the variables
echo "=================================="
echo " Compiler Configuration Variables "
echo " "
echo "cpp4r_INCLUDE_PATH: ${cpp4r_INCLUDE_PATH}"
echo "INST_INCLUDE_PATH: ${INST_INCLUDE_PATH}"
echo "PKG_CPPFLAGS: ${PKG_CPPFLAGS}"

# Create a temporary C++ file to test the compatibility with Armadillo
cat <<EOF> conftest.cpp
#include <armadillo.hpp>
using namespace arma;
int main() {
  Mat<int> A(2, 2, fill::ones);
  return 0;
}
EOF

# Test Armadillo using R CMD SHLIB
echo "==================================="
echo " Testing minimal Armadillo example "
echo " "
if ! PKG_CPPFLAGS="${PKG_CPPFLAGS}" "${R_HOME}/bin/R" CMD SHLIB conftest.cpp
then
  echo "Armadillo is not compatible with the C++ compiler used by R."
  rm -f conftest.cpp conftest.o conftest.so
  exit 1
else
  echo "Armadillo is compatible with the C++ compiler used by R."
  rm -f conftest.cpp conftest.o conftest.so
fi
