local engine_root = os.scriptdir()

add_moduledirs(path.join(engine_root, "modules"))

function add_frostbite2d_target()
    target("Frostbite2D")
        set_kind("static")
        add_files(path.join(engine_root, "Frostbite2D/src/**.cpp"))
        add_files(path.join(engine_root, "Frostbite2D/src/glad/*.c"))
        add_includedirs(path.join(engine_root, "Frostbite2D/include"), {public = true})
        add_includedirs(path.join(engine_root, "Frostbite2D/third_party/quickjspp"), {public = true})

        local quickjs_dir = path.join(engine_root, "Frostbite2D/third_party/quickjspp/quickjs")
        local quickjs_defines = {
            "CONFIG_VERSION=\"2024-01-13\"",
            "_GNU_SOURCE"
        }
        local enable_quickjs_bignum = not is_plat("windows")
        if enable_quickjs_bignum then
            table.insert(quickjs_defines, "CONFIG_BIGNUM")
        end
        if is_plat("mingw") or is_plat("windows") then
            table.insert(quickjs_defines, "__USE_MINGW_ANSI_STDIO")
        end
        add_files(path.join(quickjs_dir, "quickjs.c"), {defines = quickjs_defines})
        add_files(path.join(quickjs_dir, "cutils.c"), {defines = quickjs_defines})
        if enable_quickjs_bignum then
            add_files(path.join(quickjs_dir, "libbf.c"), {defines = quickjs_defines})
        end
        add_files(path.join(quickjs_dir, "libregexp.c"), {defines = quickjs_defines})
        add_files(path.join(quickjs_dir, "libunicode.c"), {defines = quickjs_defines})

        if is_plat("switch") then
            local devkitpro = os.getenv("DEVKITPRO") or "C:/devkitPro"
            local devkita64 = os.getenv("DEVKITA64") or path.join(devkitpro, "devkitA64")
            local portlibs = path.join(devkitpro, "portlibs/switch")
            local libnx = path.join(devkitpro, "libnx")

            add_cxflags("-D__SWITCH__", "-march=armv8-a+crc+crypto", "-mtune=cortex-a57", "-mtp=soft", {public = true})
            add_cxflags("-ffunction-sections", "-fdata-sections", "-fPIE", {public = true})
            add_cxflags("-specs=" .. path.join(libnx, "switch.specs"), {public = true, force = true})
            add_ldflags("-specs=" .. path.join(libnx, "switch.specs"), {public = true, force = true})

            add_includedirs(path.join(portlibs, "include"), {public = true})
            add_includedirs(path.join(portlibs, "include/SDL2"), {public = true})
            add_includedirs(path.join(portlibs, "include/freetype2"), {public = true})
            add_includedirs(path.join(libnx, "include"), {public = true})
            add_linkdirs(path.join(portlibs, "lib"), path.join(libnx, "lib"), {public = true})
            add_links("SDL2_ttf", "freetype", "SDL2_mixer", "vorbisidec", "opusfile", "opus", "modplug", "mpg123", "mikmod", "FLAC", "ogg", {public = true})
            add_links("SDL2_image", "png", "jpeg", "webp", "z", "SDL2", "EGL", "GLESv2", "glapi", "drm_nouveau", "nx", "m", "pthread", "stdc++", {public = true})
        else
            add_packages("libsdl2", {public = true})
            add_packages("libsdl2_image", {public = true})
            add_packages("libsdl2_mixer", {public = true})
            add_packages("libsdl2_ttf", {public = true})
            add_packages("glm", {public = true})
            add_packages("zlib", {public = true})
        end

        if is_plat("mingw") or is_plat("windows") then
            add_syslinks("imm32", {public = true})
        elseif is_plat("linux") then
            add_syslinks("m", {public = true})
        end
    target_end()
end

add_frostbite2d_target()
