#!/bin/sh
set -e

# Create a temporary directory for compilation
WORKDIR=$(mktemp -d)
trap 'rm -rf "$WORKDIR"' EXIT

# Write a Java sanity test class
cat <<EOF > "$WORKDIR/Test.java"
import one.profiler.AsyncProfiler;

public class Test {
    public static void main(String[] args) {
        try {
            System.out.println("Attempting to load AsyncProfiler...");
            AsyncProfiler profiler = AsyncProfiler.getInstance();
            System.out.println("AsyncProfiler JNI loaded successfully!");
            System.out.println("Upstream Version: " + profiler.getVersion());
        } catch (Throwable t) {
            t.printStackTrace();
            System.exit(1);
        }
    }
}
EOF

# Compile the test class using the installed system Jar
javac -cp /usr/share/java/async-profiler.jar -d "$WORKDIR" "$WORKDIR/Test.java"

# Run the test class, ensuring the JNI library path is resolved natively
java --enable-native-access=ALL-UNNAMED -cp /usr/share/java/async-profiler.jar:"$WORKDIR" Test
