#!/usr/bin/bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
FEATURES="${FEATURES:?set FEATURES=...}"
PROFILE="${PROFILE:-release}"
LIB="$ROOT/target/$PROFILE/libreadcon_core.so"
# shellcheck disable=SC2086
cargo build --"$PROFILE" --features ${FEATURES}

has() { nm "$LIB" 2>/dev/null | grep -E "[[:space:]]T[[:space:]]+$1(@@|$)" >/dev/null; }

echo "== matrix check FEATURES=$FEATURES LIB=$LIB =="

HEADER="$ROOT/include/readcon-core.h"
# Shipped header is the C ABI (metatensor/metatomic model). cbindgen is
# not invoked. Each symbol must be declared in the header and exported.
for s in create_writer_from_path_c create_writer_gzip_c create_writer_zstd_c \
         rkr_dlpack_delete rkr_frame_builder_positions_dlpack \
         rkr_frame_metatensor_positions_block rkr_mts_block_free \
         rkr_pack_rcso rkr_unpack_rcso_natoms rkr_unpack_rcso_positions \
         rkr_unpack_rcso_forces; do
  grep -q "$s" "$HEADER" || {
    echo "shipped header missing $s"
    exit 1
  }
  if has "$s"; then
    echo "  ok $s"
  else
    echo "MISSING $s"
    nm "$LIB" | grep -F "$s" | head -3 || true
    exit 1
  fi
done

if [[ "$FEATURES" == *metatensor* ]]; then
  if [[ ! -f "$ROOT/target/$PROFILE/readcon-metatensor.env" ]]; then
    # Cold builds race build.rs against metatensor-sys; force a build.rs
    # rerun now that metatensor-sys artifacts exist so the env file lands.
    touch "$ROOT/build.rs"
    # shellcheck disable=SC2086
    cargo build --"$PROFILE" --features ${FEATURES}
  fi
  test -f "$ROOT/target/$PROFILE/readcon-metatensor.env"
  echo "  ok readcon-metatensor.env"
fi

echo "OK matrix $FEATURES (shipped header + nm, no cbindgen)"
