cmake_minimum_required(VERSION 3.21)
project(Lyapunov VERSION 0.2.0 LANGUAGES CXX)
include(GNUInstallDirs)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(Qt6 6.4 REQUIRED COMPONENTS Widgets Network)
if(BUILD_TESTING OR NOT DEFINED BUILD_TESTING)
    find_package(Qt6 6.4 REQUIRED COMPONENTS Test)
endif()
# WIN32: a windowed program, so shortcuts open no console window.
qt_add_executable(lyapunov-desktop WIN32 desktop/main.cpp)
if(WIN32)
    target_sources(lyapunov-desktop PRIVATE desktop/lyapunov.rc)
endif()
target_link_libraries(lyapunov-desktop PRIVATE Qt6::Widgets Qt6::Network)
target_compile_definitions(lyapunov-desktop PRIVATE LYAPUNOV_VERSION="${PROJECT_VERSION}")
install(TARGETS lyapunov-desktop RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
if(UNIX AND NOT APPLE)
    set(LYAPUNOV_CLI_BINARY "${PROJECT_SOURCE_DIR}/target/release/lyapunov" CACHE FILEPATH
        "Path to the separately built Rust CLI")
    option(LYAPUNOV_INSTALL_GNOME_EXTENSION "Stage the GNOME Shell extension" ON)
    install(PROGRAMS "${LYAPUNOV_CLI_BINARY}" DESTINATION ${CMAKE_INSTALL_BINDIR})
    install(FILES LICENSE DESTINATION ${CMAKE_INSTALL_DATADIR}/licenses/lyapunov)
    if(LYAPUNOV_INSTALL_GNOME_EXTENSION)
        install(FILES
            integrations/gnome-shell/lyapunov@chaos-tic.github.io/extension.js
            integrations/gnome-shell/lyapunov@chaos-tic.github.io/metadata.json
            DESTINATION ${CMAKE_INSTALL_DATADIR}/gnome-shell/extensions/lyapunov@chaos-tic.github.io)
    endif()
    configure_file(packaging/linux/install-integration.cmake.in
        "${CMAKE_CURRENT_BINARY_DIR}/install-integration.cmake" @ONLY)
    install(SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/install-integration.cmake")
endif()
include(CTest)
if(BUILD_TESTING)
    qt_add_executable(lyapunov-cardstack-test tests/cardstack.cpp)
    target_link_libraries(lyapunov-cardstack-test PRIVATE Qt6::Widgets Qt6::Test)
    add_test(NAME card-stack COMMAND lyapunov-cardstack-test)
    set_tests_properties(card-stack PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen;LYAPUNOV_REDUCED_MOTION=1")
    qt_add_executable(lyapunov-update-test tests/update.cpp)
    target_link_libraries(lyapunov-update-test PRIVATE Qt6::Widgets Qt6::Network)
    add_test(NAME update-check COMMAND lyapunov-update-test)
    set_tests_properties(update-check PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
endif()
