cmake_minimum_required(VERSION 3.20)
project(NexusPCControlLinux VERSION 0.5.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

find_package(Threads REQUIRED)
find_package(Qt6 COMPONENTS Widgets QUIET)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBCURL REQUIRED libcurl)
# Prefer the distro's shared libcurl when available. Some developer environments
# expose a custom static libcurl.pc under /usr/local with extra transitive deps.
find_library(NEXUS_SHARED_CURL NAMES curl
    PATHS
        "/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}"
        "/lib/${CMAKE_LIBRARY_ARCHITECTURE}"
        /usr/lib64
        /lib64
    NO_DEFAULT_PATH
)

add_executable(nexus-pc-control
    src/main.cpp
    src/config.cpp
    src/system_info.cpp
    src/telegram.cpp
    src/json.cpp
)

target_include_directories(nexus-pc-control PRIVATE src)
if(NEXUS_SHARED_CURL)
    target_include_directories(nexus-pc-control PRIVATE ${LIBCURL_INCLUDE_DIRS})
    target_link_libraries(nexus-pc-control PRIVATE ${NEXUS_SHARED_CURL} Threads::Threads)
else()
    pkg_check_modules(LIBCURL_LINK REQUIRED IMPORTED_TARGET libcurl)
    target_link_libraries(nexus-pc-control PRIVATE PkgConfig::LIBCURL_LINK Threads::Threads)
endif()
target_compile_definitions(nexus-pc-control PRIVATE
    NEXUS_VERSION="${PROJECT_VERSION}"
    _FILE_OFFSET_BITS=64
)

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    target_compile_options(nexus-pc-control PRIVATE
        -Wall -Wextra -Wpedantic -Wconversion -Wshadow
        -Wformat=2 -Wnull-dereference
    )
endif()

include(GNUInstallDirs)
include(CTest)
if(BUILD_TESTING)
    add_executable(nexuspc-json-tests
        tests/json_roundtrip.cpp
        src/json.cpp
    )
    target_include_directories(nexuspc-json-tests PRIVATE src)
    target_compile_options(nexuspc-json-tests PRIVATE -Wall -Wextra -Wpedantic)
    add_test(NAME json-roundtrip COMMAND nexuspc-json-tests)
endif()

if(Qt6Widgets_FOUND)
    add_executable(nexus-pc-control-gui src/gui_main.cpp src/config.cpp src/json.cpp src/system_info.cpp)
    target_include_directories(nexus-pc-control-gui PRIVATE src)
    target_link_libraries(nexus-pc-control-gui PRIVATE Qt6::Widgets Threads::Threads)
    target_compile_definitions(nexus-pc-control-gui PRIVATE NEXUS_VERSION="${PROJECT_VERSION}" _FILE_OFFSET_BITS=64)
    if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
        target_compile_options(nexus-pc-control-gui PRIVATE -Wall -Wextra -Wpedantic -Wconversion -Wshadow -Wformat=2)
    endif()
    install(TARGETS nexus-pc-control-gui RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
    install(FILES ../share/applications/nexus-pc-control.desktop DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)
    install(FILES ../share/icons/hicolor/scalable/apps/nexus-pc-control.svg DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/scalable/apps)
else()
    message(STATUS "Qt 6 Widgets not found: GUI target disabled; CLI agent remains available")
endif()

install(TARGETS nexus-pc-control RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(FILES ../LICENSE DESTINATION ${CMAKE_INSTALL_DATADIR}/licenses/nexus-pc-control)

install(FILES
    ../README.md
    ../README_RU.md
    ../CHANGELOG.md
    ../SECURITY.md
    ../PRIVACY.md
    DESTINATION ${CMAKE_INSTALL_DOCDIR}
)
