cmake_minimum_required(VERSION 3.18)

project(opendartboard_x86 VERSION 1.0.0 LANGUAGES CXX)

if(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
  message(FATAL_ERROR "OpenDartboard x86 supports Linux only")
endif()

string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" OPENDARTBOARD_PROCESSOR)
if(NOT OPENDARTBOARD_PROCESSOR MATCHES "^(x86_64|amd64)$")
  message(FATAL_ERROR "OpenDartboard x86 requires a 64-bit x86_64/amd64 machine (found ${CMAKE_SYSTEM_PROCESSOR})")
endif()

include(FetchContent)

FetchContent_Declare(
  nlohmann_json
  GIT_REPOSITORY https://github.com/nlohmann/json.git
  GIT_TAG v3.12.0
)

FetchContent_Declare(
  httplib
  GIT_REPOSITORY https://github.com/yhirose/cpp-httplib.git
  GIT_TAG v0.14.3
)

FetchContent_MakeAvailable(nlohmann_json httplib)

find_package(OpenCV REQUIRED)
find_package(Threads REQUIRED)

set(LEGACY_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../src")
set(CALIBRATION_ROOT "${LEGACY_ROOT}/detector/geometry/calibration")

set(OPENDARTBOARD_X86_SOURCES
  src/main.cpp
  src/application.cpp
  src/camera_manager.cpp
  src/detection_engine.cpp
  src/game_501.cpp
  src/http_server.cpp
  src/legacy_detection.cpp
  ${CALIBRATION_ROOT}/geometry_calibration.cpp
  ${CALIBRATION_ROOT}/color_processing.cpp
  ${CALIBRATION_ROOT}/roi_processing.cpp
  ${CALIBRATION_ROOT}/mask_processing.cpp
  ${CALIBRATION_ROOT}/contour_processing.cpp
  ${CALIBRATION_ROOT}/bull_processing.cpp
  ${CALIBRATION_ROOT}/ellipse_processing.cpp
  ${CALIBRATION_ROOT}/wire_processing.cpp
  ${CALIBRATION_ROOT}/perspective_processing.cpp
  ${CALIBRATION_ROOT}/orientation_processing.cpp
)

add_executable(opendartboard-x86 ${OPENDARTBOARD_X86_SOURCES})

target_include_directories(opendartboard-x86 PRIVATE
  src
  ${LEGACY_ROOT}
  ${LEGACY_ROOT}/utils
  ${OpenCV_INCLUDE_DIRS}
  ${httplib_SOURCE_DIR}
)

target_link_libraries(opendartboard-x86 PRIVATE
  ${OpenCV_LIBS}
  nlohmann_json::nlohmann_json
  Threads::Threads
  ${CMAKE_DL_LIBS}
)

target_compile_features(opendartboard-x86 PRIVATE cxx_std_17)
target_compile_definitions(opendartboard-x86 PRIVATE
  APP_VERSION="${PROJECT_VERSION}"
  OPENDARTBOARD_LINUX_X86_64=1
  OPENDARTBOARD_DEFAULT_WEB_ROOT="${CMAKE_CURRENT_SOURCE_DIR}/web"
  OPENDARTBOARD_DEFAULT_DATA_ROOT="${CMAKE_CURRENT_SOURCE_DIR}/data"
)
target_compile_options(opendartboard-x86 PRIVATE -Wall -Wextra -Wpedantic)

set_target_properties(opendartboard-x86 PROPERTIES
  RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)

include(GNUInstallDirs)
install(TARGETS opendartboard-x86 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(DIRECTORY web/ DESTINATION ${CMAKE_INSTALL_DATADIR}/opendartboard-x86/web)

include(CTest)
if(BUILD_TESTING)
  add_executable(game-501-tests tests/game_501_test.cpp src/game_501.cpp)
  target_include_directories(game-501-tests PRIVATE src)
  target_link_libraries(game-501-tests PRIVATE nlohmann_json::nlohmann_json Threads::Threads)
  target_compile_features(game-501-tests PRIVATE cxx_std_17)
  add_test(NAME game-501-tests COMMAND game-501-tests)
endif()
