#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <libgen.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    char path[PATH_MAX];
    char dir[PATH_MAX];
    char script_path[PATH_MAX];
    char target_app[PATH_MAX];
    char command[PATH_MAX * 3];

    realpath(argv[0], path);
    char *parent_dir = dirname(path);
    strncpy(dir, parent_dir, sizeof(dir) - 1);

    if (geteuid() != 0) {
        snprintf(command, sizeof(command), "osascript -e \"do shell script \\\"%s\\\" with prompt \\\"SplatTool needs root for reading Cemu's memory.\\\" with administrator privileges\"", path);
        
        int status = system(command);
        return WEXITSTATUS(status);
    }
    
    snprintf(target_app, sizeof(target_app), "%s/SplatTool", dir);
    chdir(dir);

    extern char **environ;
    char *new_argv[] = { target_app, NULL };
    
    execve(target_app, new_argv, environ);

    perror("execve failed");
    return 1;
}