BareGit
cmake_minimum_required(VERSION 3.24)

if(POLICY CMP0169)
    cmake_policy(SET CMP0169 OLD)
endif()

project(probius LANGUAGES CXX)

include(FetchContent)
set(LIBMW_BUILD_SQLITE ON CACHE BOOL "Build libmw SQLite support")
set(LIBMW_BUILD_URL ON)
set(LIBMW_BUILD_HTTP_SERVER ON)
set(RYML_DEFAULT_CALLBACK_USES_EXCEPTIONS ON CACHE BOOL
    "Return YAML parse failures as exceptions" FORCE)
FetchContent_Declare(json
    GIT_REPOSITORY https://github.com/nlohmann/json.git
    GIT_TAG HEAD
)
FetchContent_Declare(inja
    GIT_REPOSITORY https://github.com/pantor/inja.git
    GIT_TAG main
)
FetchContent_Declare(ryml
    GIT_REPOSITORY https://github.com/biojppm/rapidyaml.git
    GIT_TAG master
)
FetchContent_Declare(libmw
    GIT_REPOSITORY https://github.com/MetroWind/libmw.git
    GIT_TAG HEAD
)
FetchContent_MakeAvailable(json libmw ryml)
FetchContent_GetProperties(inja)
if(NOT inja_POPULATED)
    FetchContent_Populate(inja)
endif()
set(CARES_SHARED OFF CACHE BOOL "Build shared c-ares library")
set(CARES_STATIC ON CACHE BOOL "Build static c-ares library")
set(CARES_BUILD_TOOLS OFF CACHE BOOL "Build c-ares tools")
FetchContent_Declare(cares
    GIT_REPOSITORY https://github.com/c-ares/c-ares.git
    GIT_TAG HEAD
)
FetchContent_MakeAvailable(cares)
find_package(CURL REQUIRED)
find_package(SQLite3 REQUIRED)
find_package(Threads REQUIRED)
if(NOT TARGET SQLite3::SQLite3)
    add_library(SQLite3::SQLite3 ALIAS SQLite::SQLite3)
endif()

set(STATIC_FILES
    "${CMAKE_CURRENT_SOURCE_DIR}/static/index.html"
    "${CMAKE_CURRENT_SOURCE_DIR}/static/style.css"
)
include(cmake/embed_assets.cmake)

set(SOURCE_FILES
    src/app.cpp
    src/configuration.cpp
    "${CMAKE_CURRENT_BINARY_DIR}/generated/embedded_assets.cpp"
    src/data_source_sqlite.cpp
    src/probe.cpp
    src/socket_probe.cpp
    src/scheduler.cpp
    src/service.cpp
    src/thread_pool.cpp
)
set(LIBS
    mw::http-server
    mw::sqlite
    mw::url
    ryml::ryml
    CURL::libcurl
    c-ares::cares
    SQLite3::SQLite3
    Threads::Threads
)
set(INCLUDES
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${libmw_SOURCE_DIR}/includes
    ${json_SOURCE_DIR}/single_include
    ${inja_SOURCE_DIR}/single_include
)

add_executable(probius ${SOURCE_FILES} src/main.cpp)
target_compile_features(probius PRIVATE cxx_std_23)
set_target_properties(probius PROPERTIES CXX_EXTENSIONS OFF)
target_include_directories(probius PRIVATE ${INCLUDES})
target_link_libraries(probius PRIVATE ${LIBS})

include(CTest)
if(BUILD_TESTING)
    FetchContent_Declare(googletest
        GIT_REPOSITORY https://github.com/google/googletest.git
        GIT_TAG HEAD
    )
    FetchContent_MakeAvailable(googletest)
    include(GoogleTest)

    set(TEST_FILES
        tests/configuration_test.cpp
        src/fake_probe.cpp
        tests/app_test.cpp
        tests/data_source_sqlite_test.cpp
        tests/fake_probe_test.cpp
        tests/probe_test.cpp
        tests/scheduler_test.cpp
        tests/service_test.cpp
        tests/thread_pool_test.cpp
    )
    add_executable(probius_test ${SOURCE_FILES} ${TEST_FILES})
    target_compile_features(probius_test PRIVATE cxx_std_23)
    set_target_properties(probius_test PROPERTIES CXX_EXTENSIONS OFF)
    target_include_directories(probius_test PRIVATE ${INCLUDES})
    target_link_libraries(probius_test PRIVATE
        ${LIBS} GTest::gtest_main
    )
    gtest_discover_tests(probius_test PROPERTIES TIMEOUT 30)
endif()