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 all 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 {
	private static final int EXPECTED_KERNELS = 41; // 40 (through rung D4) + erase_clt_task_tiles (margin rung M1: erase-by-task-list in the pose chain) // By Claude on 07/17/2026
    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 == EXPECTED_KERNELS ?
				"RESULT: PASS ("+EXPECTED_KERNELS+"/"+EXPECTED_KERNELS+" via JNA)" :
				("RESULT: FAIL (" + n + "/"+EXPECTED_KERNELS+")"));
        System.exit(n == EXPECTED_KERNELS ? 0 : 2);
    }
}
