#!/bin/sh
set -eu

# fd 3 is a stable copy of the real stderr, so run()'s trace always reaches
# the terminal even if a caller locally redirects 2>&1 to capture output.
exec 3>&2

# Echoes the command before running it, so logs show exactly what was executed
run() {
    echo "+ $*" >&3
    "$@"
}

SOURCE_DIR="$(pwd)"

WORKDIR="${AUTOPKGTEST_TMP:-$(mktemp -d)}"
if [ -z "${AUTOPKGTEST_TMP:-}" ]; then
    trap 'rm -rf "$WORKDIR"' EXIT
fi

cd "$WORKDIR"

echo "Building simple-dawn-program..."
run "${CXX:-g++}" -std=c++20 "$SOURCE_DIR/debian/tests/simple-dawn-program.cpp" \
    -lwebgpu_dawn -Wall -o simple-dawn-program

# Force lavapipe; otherwise any installed hardware Vulkan ICD could be picked instead.
export VK_DRIVER_FILES=/usr/share/vulkan/icd.d/lvp_icd.json

echo "Running simple-dawn-program (lavapipe)..."
EXIT=0
run ./simple-dawn-program || EXIT=$?

exit "$EXIT"
