#!/bin/sh

: ${R_HOME=`R RHOME`}
: ${CC=gcc}
FP_FLAG=""
if echo "int main(void){return 0;}" | ${CC} -ffp-contract=off -x c -c - -o NUL 2>/dev/null; then
  FP_FLAG="-ffp-contract=off"
fi

# Detect OpenMP support for SCIP's thread pool interface (TPI).
# R-exts recommends $(SHLIB_OPENMP_CXXFLAGS) in Makevars when linking is by
# C++. OPENMP_FLAG is a fallback for when R's variable is empty.
OPENMP_FLAG=""
R_HAS_OPENMP=no

# Method 1: R's own OpenMP configuration (preferred, per R-exts 1.2.1.1).
if test -n "${R_HOME}"; then
  # Use CXXFLAGS since linking is by C++ (CXX_STD = CXX17).
  OPENMP_CXXFLAGS=`"${R_HOME}/bin/R" CMD config SHLIB_OPENMP_CXXFLAGS 2>/dev/null` || OPENMP_CXXFLAGS=""
  if test -n "${OPENMP_CXXFLAGS}"; then
    R_HAS_OPENMP=yes
  fi
fi

# Method 2: Compile test fallback — try multiple flags for different compilers.
if test "${R_HAS_OPENMP}" = "no"; then
  CONFTEST=conftest_omp_$$
  cat > ${CONFTEST}.c <<EOF
#include <omp.h>
int main(void) { return omp_get_num_threads(); }
EOF
  for omp_flag in -fopenmp -qopenmp -xopenmp; do
    if ${CC} ${omp_flag} -o ${CONFTEST} ${CONFTEST}.c 2>/dev/null; then
      OPENMP_FLAG="${omp_flag}"
      break
    fi
  done
  rm -f ${CONFTEST}.c ${CONFTEST} ${CONFTEST}.exe
fi

if test "${R_HAS_OPENMP}" = "yes" || test -n "${OPENMP_FLAG}"; then
  TPI_MODE=omp
  echo "OpenMP supported: using TPI=omp for SCIP thread pool"
else
  TPI_MODE=none
  OPENMP_FLAG=""
  echo "OpenMP not available: using TPI=none (no SCIP parallelism)"
fi
export TPI_MODE

# Build SCIP + SoPlex from source via CMake
R_SCIP_PKG_HOME=`pwd`
bash inst/build_scip.sh || { echo "SCIP build failed!"; exit 1; }
cd ${R_SCIP_PKG_HOME}

# After CMake build, remove source trees if installing from tarball
if test -d inst/doc; then
    rm -rf inst/scip inst/soplex inst/config
fi

R_SCIP_LIB_DIR=${R_SCIP_PKG_HOME}/src/sciplib
R_SOPLEX_LIB_DIR=${R_SCIP_PKG_HOME}/src/soplexlib

RSCIP_PKG_LIBS="-L${R_SCIP_LIB_DIR}/lib -lscip -L${R_SOPLEX_LIB_DIR}/lib -lsoplex"
if test -d "${R_SCIP_LIB_DIR}/lib64"; then
    RSCIP_PKG_LIBS="-L${R_SCIP_LIB_DIR}/lib64 -lscip ${RSCIP_PKG_LIBS}"
fi
if test -d "${R_SOPLEX_LIB_DIR}/lib64"; then
    RSCIP_PKG_LIBS="${RSCIP_PKG_LIBS} -L${R_SOPLEX_LIB_DIR}/lib64 -lsoplex"
fi

sed -e "s|@FP_CONTRACT_FLAG@|${FP_FLAG}|g" \
    -e "s|@OPENMP_FLAG@|${OPENMP_FLAG}|g" \
    -e "s|@RSCIP_LIB_DIR@|${R_SCIP_LIB_DIR}|g" \
    -e "s|@RSCIP_PKG_LIBS@|${RSCIP_PKG_LIBS}|g" \
    src/Makevars.in > src/Makevars.win

if [ -z "$(ls ${R_SCIP_LIB_DIR} 2>/dev/null | grep 'include')" ]; then
    echo "SCIP libraries could not be found!"
    exit 1
fi
