#!/bin/sh
# configure -- prepare the native C++ sources in src/. The R package builds the
# CPU backend of the engine (pure C++17, OpenMP where available), which
# implements the full C interface; the CUDA backend of the same interface is
# built separately from the native/ sources and is not part of this build.
#
# src/ carries copies of the three engine sources, so the package directory and
# its tarball are self-contained. Inside the monorepo, native/ is the source of
# truth: when it is found next to the package the copies are refreshed from it
# (normalised to LF), and a changed copy then shows up in git.
set -e
SRC=src
NATIVE=""
for d in ../native ../../native; do
  if [ -f "$d/libcusna_saom_cpu.cpp" ]; then NATIVE="$d"; break; fi
done
if [ -n "$NATIVE" ]; then
  for f in libcusna.h libcusna_saom_cpu.cpp libcusna_ergm_cpu.cpp; do
    tr -d '\r' < "$NATIVE/$f" > "$SRC/$f.tmp"
    if [ -f "$SRC/$f" ] && cmp -s "$SRC/$f.tmp" "$SRC/$f"; then
      rm -f "$SRC/$f.tmp"
    else
      mv "$SRC/$f.tmp" "$SRC/$f"
      echo "cusna: refreshed src/$f from $NATIVE"
    fi
  done
elif [ ! -f "$SRC/libcusna_saom_cpu.cpp" ]; then
  echo "cusna: ERROR -- native sources not found in src/ or ../native/." >&2
  exit 1
fi
echo "cusna: building the CPU backend (C++17, OpenMP where available)."
exit 0
