// LICENSE NOTICE ==================================================================================

/**
 * netzhaut - Web Browser Engine
 * Copyright (C) 2020 The netzhaut Authors
 * Published under MIT
 */

// INCLUDE =========================================================================================

#include "Initialize.h"
#include "Config.h"
#include "IndexMap.h"
#include "Macros/Macros.h"

#include NH_FLOW
#include NH_CUSTOM_CHECK

#include "../System/System.h"
#include "../System/Memory.h"
#include "../System/Process.h"
#include "../System/Thread.h"
#include "../System/Logger.h"
#include "../Loader/Loader.h"

#include <dlfcn.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// INIT ============================================================================================

static NH_RESULT nh_startInternalWorkloads()
{
NH_BEGIN()

    nh_activateWorkload(
        nh_initSystemUpdater, nh_runSystemUpdater, NULL, NH_FALSE
    );

NH_DIAGNOSTIC_END(NH_SUCCESS)
}

nh_Loader *nh_initialize(
    NH_LOADER_SCOPE scope, NH_BOOL fallback, NH_BOOL install)
{
    _nh_initConfig();

    NH_CHECK(NULL, nh_initLogger())
    NH_CHECK(NULL, nh_initMemory())
    NH_CHECK(NULL, nh_initProcessPool())
    NH_CHECK(NULL, nh_createIndexMap())
    NH_CHECK(NULL, nh_initSystem())
    NH_CHECK(NULL, nh_startInternalWorkloads())

    return nh_initLoader(scope, fallback, install);
}

