include(GNUInstallDirs)

add_library(gldoze_remote_protocol STATIC
  src/protocol.cpp
  src/config.cpp
  src/services.cpp
  src/agent_session.cpp
  src/notification_channel.cpp)
target_include_directories(gldoze_remote_protocol PUBLIC include)
target_link_libraries(gldoze_remote_protocol PUBLIC Threads::Threads)
target_compile_definitions(gldoze_remote_protocol PUBLIC
  GLDOZE_REMOTE_VERSION="${PROJECT_VERSION}")
set_target_properties(gldoze_remote_protocol PROPERTIES POSITION_INDEPENDENT_CODE ON)

add_executable(gldoze-agent src/agent_main.cpp)
target_link_libraries(gldoze-agent PRIVATE gldoze_remote_protocol)

if(TARGET PkgConfig::GLDOZE_LIBSSH2)
  add_library(gldoze_remote_client STATIC src/client.cpp)
  target_include_directories(gldoze_remote_client PUBLIC include)
  target_link_libraries(gldoze_remote_client PUBLIC
    gldoze_remote_protocol PkgConfig::GLDOZE_LIBSSH2 Threads::Threads)
  set_target_properties(gldoze_remote_client PROPERTIES POSITION_INDEPENDENT_CODE ON)
  add_executable(gldoze-remote src/remote_cli.cpp)
  target_link_libraries(gldoze-remote PRIVATE gldoze_remote_client)
else()
  message(STATUS "libssh2 unavailable: gldoze-agent will be built without the remote client CLI")
endif()

install(TARGETS gldoze-agent
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT GLDozeDesktop)
if(TARGET gldoze-remote)
  install(TARGETS gldoze-remote
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT GLDozeDesktop)
endif()
install(FILES config/remote.conf
  DESTINATION ${CMAKE_INSTALL_DATADIR}/gldoze/remote
  COMPONENT GLDozeDesktop)
install(FILES config/gldoze-sshd.conf
  DESTINATION ${CMAKE_INSTALL_DATADIR}/gldoze/remote
  COMPONENT GLDozeDesktop)
install(FILES
  ${GLDOZE_REPO_ROOT}/docs/GLDOZE_REMOTE_PROTOCOL.md
  ${GLDOZE_REPO_ROOT}/docs/REMOTE_SETUP.md
  ${GLDOZE_REPO_ROOT}/docs/REMOTE_AGENT_PACKAGE.md
  DESTINATION ${CMAKE_INSTALL_DATADIR}/doc/gldoze-desktop
  COMPONENT GLDozeDesktop)

if(BUILD_TESTING)
  add_executable(gldoze_remote_protocol_test tests/protocol_test.cpp)
  target_link_libraries(gldoze_remote_protocol_test PRIVATE gldoze_remote_protocol)
  add_test(NAME gldoze_remote_protocol COMMAND gldoze_remote_protocol_test)

  add_executable(gldoze_remote_authorization_test tests/authorization_test.cpp)
  target_link_libraries(gldoze_remote_authorization_test PRIVATE gldoze_remote_protocol)
  add_test(NAME gldoze_remote_authorization COMMAND gldoze_remote_authorization_test)

  add_executable(gldoze_remote_agent_integration_test tests/agent_integration_test.cpp)
  target_link_libraries(gldoze_remote_agent_integration_test PRIVATE gldoze_remote_protocol)
  add_dependencies(gldoze_remote_agent_integration_test gldoze-agent)
  add_test(NAME gldoze_remote_agent_integration COMMAND gldoze_remote_agent_integration_test $<TARGET_FILE:gldoze-agent>)

  add_test(NAME gldoze_remote_agent_package_installer
    COMMAND bash
      ${CMAKE_CURRENT_SOURCE_DIR}/tests/agent_package_installer_test.sh
      ${GLDOZE_REPO_ROOT}
      $<TARGET_FILE:gldoze-agent>)

  add_executable(gldoze_remote_notification_channel_test tests/notification_channel_test.cpp)
  target_link_libraries(gldoze_remote_notification_channel_test PRIVATE gldoze_remote_protocol)
  add_test(NAME gldoze_remote_notification_channel COMMAND gldoze_remote_notification_channel_test)

  add_executable(gldoze_remote_parser_fuzz_smoke_test tests/parser_fuzz_smoke_test.cpp)
  target_link_libraries(gldoze_remote_parser_fuzz_smoke_test PRIVATE gldoze_remote_protocol)
  add_test(NAME gldoze_remote_parser_fuzz_smoke COMMAND gldoze_remote_parser_fuzz_smoke_test)
endif()
