package com.elphel.imagej.gpu.jna;

import com.sun.jna.Native;
import com.sun.jna.Pointer;

/**
 * Stage-0b proof: from Java, load libtileproc.so via JNA, call tp_create_module, assert 19 kernels.
 * Same result as the C++ probe but across the JNA boundary on the RTX 5060 Ti. By Claude on 2026-06-25.
 *   java -Djna.library.path=<dir with libtileproc.so> -cp target/classes:<jna.jar> \
 *        com.elphel.imagej.gpu.jna.Stage0 [kernel_src_dir] [libcudadevrt.a]
 */
public class Stage0 {
    public static void main(String[] args) {
        String srcdir = (args.length > 0) ? args[0] : "/home/elphel/git/tile_processor_gpu/src";
        String devrt  = (args.length > 1) ? args[1] : "/usr/local/cuda/targets/x86_64-linux/lib/libcudadevrt.a";
        System.out.println("Stage0 (JNA): loading libtileproc, srcdir=" + srcdir);
        TpJna lib = Native.load("tileproc", TpJna.class);
        Pointer m = lib.tp_create_module(srcdir, devrt);
        if (m == null) {
            System.out.println("FAIL: tp_create_module returned null:\n" + lib.tp_last_error());
            System.exit(1);
        }
        int n = lib.tp_module_num_functions(m);
        String err = lib.tp_last_error();
        System.out.println("Stage0 (JNA): module created, functions loaded = " + n + (err != null && !err.isEmpty() ? ("  [warn: " + err + "]") : ""));
        lib.tp_destroy_module(m);
        System.out.println(n == 19 ? "RESULT: PASS (19/19 via JNA)" : ("RESULT: FAIL (" + n + "/19)"));
        System.exit(n == 19 ? 0 : 2);
    }
}
