#!/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-vulkan-program..."
run "${CC:-cc}" "$SOURCE_DIR/debian/tests/simple-vulkan-program.c" -lvulkan -Wall -o simple-vulkan-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-vulkan-program (lavapipe)..."
EXIT=0
run ./simple-vulkan-program || EXIT=$?

exit "$EXIT"
