#!/bin/sh
# configure -- vendor the native C++ host sources into src/ and report whether a
# CUDA toolkit is available. Modelled on R 'torch': the CPU path (the validated
# host statistics) is pure C++ and always builds; the GPU path is compiled from
# source only when nvcc is present. The exposed A5 functions are all CPU, so the
# default CPU-only build is fully functional.
set -e
SRC=src
# source-of-truth native sources live in the monorepo native/ dir; a standalone
# tarball must have them vendored already (see tools/vendor.sh / configure copy)
NATIVE=""
for d in ../native ../../native; do
  if [ -f "$d/libcusna_saom_cpu.cpp" ]; then NATIVE="$d"; break; fi
done
if [ ! -f "$SRC/libcusna_saom_cpu.cpp" ]; then
  if [ -n "$NATIVE" ]; then
    echo "cusna: vendoring native sources from $NATIVE (normalising to LF)"
    tr -d '\r' < "$NATIVE/libcusna.h" > "$SRC/libcusna.h"
    tr -d '\r' < "$NATIVE/libcusna_saom_cpu.cpp" > "$SRC/libcusna_saom_cpu.cpp"
    tr -d '\r' < "$NATIVE/libcusna_ergm_cpu.cpp" > "$SRC/libcusna_ergm_cpu.cpp"
  else
    echo "cusna: ERROR -- native sources not found (expected src/libcusna_saom_cpu.cpp" >&2
    echo "       or ../native/); vendor them before building." >&2
    exit 1
  fi
fi
if command -v nvcc >/dev/null 2>&1; then
  echo "cusna: nvcc detected -- a GPU build is possible (see src/Makevars notes)."
else
  echo "cusna: no nvcc on PATH -- building CPU-only (the CRAN default)."
fi
exit 0
