/**
 * @file Main.cpp
 * @author apio (cloudapio.eu)
 * @brief Alternative C++ main function.
 *
 * @copyright Copyright (c) 2023, the Luna authors.
 *
 */

#include <errno.h>
#include <os/Main.h>
#include <stdio.h>

int g_argc;
char** g_argv;

// This function will only be linked into the final binary if the user does not already define main(), which implies
// that they should have defined luna_main() instead. If not, good for them: linker errors!
__attribute__((weak)) int main(int argc, char** argv)
{
    g_argc = argc;
    g_argv = argv;

    int result;
    bool ok = luna_main(argc, argv).try_set_value_or_error(result, errno);
    if (!ok)
    {
        perror(g_argv[0]);
        return 1;
    }

    return result;
}