// Meshy launcher - native executable that spawns Python
// This ensures macOS shows "Meshy" instead of "python 3.14" in permission dialogs
// Uses posix_spawn instead of exec to keep launcher process active

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <mach-o/dyld.h>
#include <spawn.h>
#include <sys/wait.h>

extern char **environ;

int main(int argc, char *argv[]) {
    // Get path to this executable
    char exec_path[1024];
    uint32_t size = sizeof(exec_path);
    if (_NSGetExecutablePath(exec_path, &size) != 0) {
        fprintf(stderr, "Error: executable path too long\n");
        return 1;
    }

    // Get directory containing this executable (Contents/MacOS)
    char exec_path_copy[1024];
    strncpy(exec_path_copy, exec_path, sizeof(exec_path_copy));
    char *macos_dir = dirname(exec_path_copy);

    // Build paths
    char contents_dir[1024], resources_dir[1024], python_path[1024], script_path[1024];
    snprintf(contents_dir, sizeof(contents_dir), "%s/..", macos_dir);
    snprintf(resources_dir, sizeof(resources_dir), "%s/Resources", contents_dir);
    snprintf(python_path, sizeof(python_path), "/opt/homebrew/bin/python3.14");
    snprintf(script_path, sizeof(script_path), "%s/lib/python/meshy/__main__.py", resources_dir);

    // Set environment variables
    char pythonhome[1024], pythonpath[2048];
    snprintf(pythonhome, sizeof(pythonhome),
             "/opt/homebrew/opt/python@3.14/Frameworks/Python.framework/Versions/3.14");
    snprintf(pythonpath, sizeof(pythonpath),
             "%s/lib/python-packages:%s/lib/python", resources_dir, resources_dir);

    setenv("PYTHONHOME", pythonhome, 1);
    setenv("PYTHONPATH", pythonpath, 1);
    setenv("PYTHONNOUSERSITE", "1", 1);

    // GTK environment
    char dyld_lib[1024], gi_typelib[1024], xdg_data[2048];
    snprintf(dyld_lib, sizeof(dyld_lib), "/opt/homebrew/lib");
    snprintf(gi_typelib, sizeof(gi_typelib), "/opt/homebrew/lib/girepository-1.0");
    snprintf(xdg_data, sizeof(xdg_data),
             "/opt/homebrew/share:%s/share:/usr/local/share:/usr/share", resources_dir);

    setenv("DYLD_LIBRARY_PATH", dyld_lib, 1);
    setenv("GI_TYPELIB_PATH", gi_typelib, 1);
    setenv("XDG_DATA_DIRS", xdg_data, 1);
    setenv("GTK_DATA_PREFIX", "/opt/homebrew", 1);
    setenv("GTK_EXE_PREFIX", "/opt/homebrew", 1);
    setenv("GDK_BACKEND", "macos", 1);
    setenv("GDK_PIXBUF_MODULEDIR", "/opt/homebrew/lib/gdk-pixbuf-2.0/2.10.0/loaders", 1);
    setenv("GDK_PIXBUF_MODULE_FILE", "/opt/homebrew/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache", 1);
    setenv("PANGOCAIRO_BACKEND", "fc", 1);

    char meshy_pkgdatadir[1024];
    snprintf(meshy_pkgdatadir, sizeof(meshy_pkgdatadir), "%s/share/meshy", resources_dir);
    setenv("MESHY_PKGDATADIR", meshy_pkgdatadir, 1);

    // Build argument array for Python
    char **python_argv = malloc(sizeof(char*) * (argc + 3));
    python_argv[0] = python_path;
    python_argv[1] = "-S";  // Skip site-packages
    python_argv[2] = script_path;

    // Copy remaining arguments
    for (int i = 1; i < argc; i++) {
        python_argv[i + 2] = argv[i];
    }
    python_argv[argc + 2] = NULL;

    // Spawn Python process (keeps launcher active so macOS shows "Meshy" in permissions)
    pid_t pid;
    int status;
    if (posix_spawn(&pid, python_path, NULL, NULL, python_argv, environ) != 0) {
        perror("Failed to spawn Python");
        return 1;
    }

    // Wait for Python to exit
    if (waitpid(pid, &status, 0) == -1) {
        perror("Failed to wait for Python");
        return 1;
    }

    // Return Python's exit code
    if (WIFEXITED(status)) {
        return WEXITSTATUS(status);
    }
    return 1;
}
