#!/bin/bash
set -e

# Create a temporary standalone project directory to isolate from Bazel's own MODULE.bazel
TEST_DIR=$(mktemp -d)
trap 'rm -rf "$TEST_DIR"' EXIT

# Copy the gen examples to the isolated test directory
mkdir -p "$TEST_DIR/examples/gen"
cp examples/gen/* "$TEST_DIR/examples/gen/"

# Create a clean, minimal WORKSPACE and MODULE.bazel for the test project
touch "$TEST_DIR/WORKSPACE"
touch "$TEST_DIR/MODULE.bazel"

# Execute the tests in the isolated directory
cd "$TEST_DIR"

bazel --batch clean --expunge --define=distribution=debian

# Non-amd64 architectures are having issues with ProcessUtils.getpid() UnsatisfiedLinkError
if [[ $(dpkg --print-architecture) == "amd64" ]]; then
    bazel --batch info --define=distribution=debian
fi

bazel --batch build --define=distribution=debian --verbose_failures //examples/gen:genrule
