set_project("dps_manager_server")
set_version("0.1.0")
set_languages("c11", "cxx17")
set_warnings("all", "error")

add_rules("mode.debug", "mode.release")
add_cxxflags("-Wno-deprecated-declarations", {tools = {"gcc", "clang"}})
add_requires("openssl3", "zlib", "mariadb-connector-c")

if is_plat("linux") then
    add_syslinks("pthread")
elseif is_plat("mingw", "windows") then
    add_defines("DPS_WINDOWS")
    add_syslinks("ws2_32")
end

add_defines("OPENSSL_SUPPRESS_DEPRECATED")

target("dps_manager_server")
    set_kind("binary")
    add_includedirs("include")
    add_packages("openssl3", "zlib", "mariadb-connector-c")
    add_files("src/*.cpp")
    if is_plat("linux") then
        add_syslinks("crypt")
    elseif is_plat("mingw", "windows") then
        add_includedirs("third_party/libbcrypt/crypt_blowfish")
        add_files(
            "third_party/libbcrypt/crypt_blowfish/wrapper.c",
            "third_party/libbcrypt/crypt_blowfish/crypt_blowfish.c",
            "third_party/libbcrypt/crypt_blowfish/crypt_gensalt.c",
            {warnings = "none"}
        )
    end
    add_headerfiles("include/(dps/*.hpp)")
    after_build(function (target)
        local project_config_dir = path.join(os.projectdir(), "config")
        local output_config_dir = path.join(target:targetdir(), "config")
        local config_files = {
            "server.conf",
            "server.conf.example",
            "server.windows.conf"
        }

        os.mkdir(output_config_dir)
        for _, config_file in ipairs(config_files) do
            local source = path.join(project_config_dir, config_file)
            if os.isfile(source) then
                os.cp(source, path.join(output_config_dir, config_file))
            end
        end

        if not target:is_plat("windows") then
            return
        end
        local pkg = target:pkg("mariadb-connector-c")
        if not pkg then
            return
        end
        local plugin_dir = path.join(pkg:installdir(), "lib", "mariadb", "plugin")
        if os.isdir(plugin_dir) then
            local output_dir = path.join(target:targetdir(), "mariadb-plugin")
            if os.isfile(output_dir) then
                os.rm(output_dir)
            end
            os.mkdir(output_dir)
            os.cp(path.join(plugin_dir, "*.dll"), output_dir)
        end
    end)

target("json_smoke_test")
    set_kind("binary")
    set_default(false)
    add_includedirs("include")
    add_files("tests/json_smoke_test.cpp", "src/json.cpp")

target("crypto_smoke_test")
    set_kind("binary")
    set_default(false)
    add_includedirs("include")
    add_packages("openssl3", "zlib", "mariadb-connector-c")
    add_files(
        "tests/crypto_smoke_test.cpp",
        "src/app.cpp",
        "src/config.cpp",
        "src/fs_utils.cpp",
        "src/http.cpp",
        "src/json.cpp",
        "src/labels.cpp"
    )
    if is_plat("linux") then
        add_syslinks("crypt")
    elseif is_plat("mingw", "windows") then
        add_includedirs("third_party/libbcrypt/crypt_blowfish")
        add_files(
            "third_party/libbcrypt/crypt_blowfish/wrapper.c",
            "third_party/libbcrypt/crypt_blowfish/crypt_blowfish.c",
            "third_party/libbcrypt/crypt_blowfish/crypt_gensalt.c",
            {warnings = "none"}
        )
    end

target("fs_utils_smoke_test")
    set_kind("binary")
    set_default(false)
    add_includedirs("include")
    add_files("tests/fs_utils_smoke_test.cpp", "src/fs_utils.cpp")

target("labels_smoke_test")
    set_kind("binary")
    set_default(false)
    add_includedirs("include")
    add_files("tests/labels_smoke_test.cpp", "src/labels.cpp")
