add_rules("mode.debug", "mode.release")

set_project("khistory")

set_languages("cxx17")

-- config common third-party libs
add_requires("imgui 1.88", {configs = {glfw_opengl3 = true}})
add_packages("imgui")
-- use dsvisual framework
add_includedirs("third-party/DSVisual")
add_files("third-party/DSVisual/dsvisual-core.cpp")
add_includedirs("third-party/DStruct")

-- config os platform
if is_host("windows") then
    add_ldflags("-subsystem:windows")
    add_ldflags("-entry:mainCRTStartup", {force = true})
    add_files("platform/win_platform_impl.cpp")
elseif is_host("macosx") then
    add_files("platform/mac_platform_impl.cpp")
else
    add_files("platform/linux_platform_impl.cpp")
    add_links("X11")
end

-- config khistory include path
add_includedirs("include")
add_includedirs("kplugin")
add_includedirs("kplugin/auto-register")

target("khistory")
    set_kind("binary")
    add_files("src/*.cpp")
    before_build(
        function () 
            local files = os.files("kplugin/auto-register" .. "/*.kplugin.hpp")
            local pluginFile = io.open("kplugin/plugin-auto-register-info.kplugin", "w")

            pluginFile:write('//Note: This file is automatically generated by a script. Please do not modify it.\n');
            pluginFile:write('//Date: ' .. os.date("%Y-%m-%d %H:%M:%S") .. '\n');

            -- detect head-file
            for _, file in ipairs(files) do
                local fileName = path.filename(file)
                pluginFile:write('#include "' .. fileName .. '"\n')
            end
            -- register plugin
            pluginFile:write('static void loadAutoRegisterPlugin() {')
            for _, file in ipairs(files) do
                local pluginName = path.filename(file):gsub("%.kplugin%.hpp$", "")
                pluginFile:write('\n    KPLUGIN_REGISTER(' .. pluginName .. ');')
            end
            pluginFile:write('\n}')

            pluginFile:close()
        end
    )

--[[
target("khistory-test")
    set_kind("binary")
    add_files("src/PAL.cpp")
    add_files("tests/test.cpp")
--]]