#!/usr/bin/env sh

# Windows configuration. See `configure` for the full rationale. The core is
# compiled directly into the package DLL; Rtools provides OpenMP through
# $(SHLIB_OPENMP_CXXFLAGS), so there is no OpenMP probing to do here.

set -e

# Stage core sources when building from the monorepo.
if [ ! -d src/core ]; then
  if [ -d ../../core ]; then
    echo "* Monorepo detected: staging core sources into src/core"
    mkdir -p src/core
    cp -R ../../core/include src/core/include
    cp -R ../../core/src src/core/src
  else
    echo "ERROR: Cannot find core source. Expected src/core (tarball) or ../../core (monorepo)."
    exit 1
  fi
fi

# Generate the OBJECTS list (see configure for which sources are included).
OBJECTS="main.o RcppExports.o"
for src in $(cd src && find core/src -name '*.cpp' \
    ! -name '*.test.cpp' \
    ! -name 'test.cpp' \
    ! -path 'core/src/cli/*' \
    ! -path 'core/src/io/*' \
    ! -path 'core/src/golden/*'); do
  OBJECTS="${OBJECTS} ${src%.cpp}.o"
done

sed -e "s|@OBJECTS@|${OBJECTS}|" \
    src/Makevars.win.in > src/Makevars.win
