#!/bin/sh
set -euo pipefail

SRCDIR="$(pwd)"
WORKDIR=$(mktemp -d)
trap 'rm -rf "$WORKDIR"' EXIT

# ---------------------------------------------------------------------------
# TEST 1 — find_package(hipfort) consumer build + run (no ROCm runtime/GPU
# required -- hipfort_enums is constants-only, no linked HIP symbols needed).
# Configure, build and run a minimal Fortran consumer (debian/tests/consumer)
# against the installed libhipfort-dev, confirming: find_package(hipfort)
# resolves the package config from the install prefix; the
# hipfort::hipfort-amdgcn target's declared include directory actually
# contains hipfort_enums.mod; and its declared IMPORTED_LOCATION archive
# actually exists and links.
# ---------------------------------------------------------------------------
echo "[TEST 1] find_package(hipfort) consumer build..."

if cmake -S "$SRCDIR/debian/tests/consumer" -B "$WORKDIR/build" >"$WORKDIR/build.log" 2>&1 \
   && cmake --build "$WORKDIR/build" >>"$WORKDIR/build.log" 2>&1; then
    echo "[TEST 1] PASS"
else
    echo "[TEST 1] FAIL: see log below" >&2
    cat "$WORKDIR/build.log" >&2
    exit 1
fi

echo "[TEST 2] smoke binary runs..."

OUTPUT=$("$WORKDIR/build/smoke")
if echo "$OUTPUT" | grep -q "HIP_VERSION_MAJOR"; then
    echo "[TEST 2] PASS: $OUTPUT"
else
    echo "[TEST 2] FAIL: unexpected output: $OUTPUT" >&2
    exit 1
fi

echo "All hipfort smoke tests passed."
exit 0
