cmake_minimum_required(VERSION 3.8)
project(mtran)

set(prepared_runtime_files
  runtime/bin/mtranserver
  runtime/config/records.json
  models/en_zh-Hans/model.enzh.intgemm.alphas.bin
  models/en_zh-Hans/lex.50.50.enzh.s2t.bin
  models/en_zh-Hans/srcvocab.enzh.spm
  models/en_zh-Hans/trgvocab.enzh.spm
)
foreach(prepared_file IN LISTS prepared_runtime_files)
  if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${prepared_file}")
    message(FATAL_ERROR
      "Missing preparation artifact: ${prepared_file}\n"
      "Run ./dependencies/auto_install.sh from the mtran package root."
    )
  endif()
endforeach()

find_package(ament_cmake REQUIRED)
find_package(CURL REQUIRED)
find_package(nlohmann_json REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  "srv/TranslateEnglishToChinese.srv"
)
rosidl_get_typesupport_target(cpp_typesupport_target
  ${PROJECT_NAME} rosidl_typesupport_cpp)

add_library(mtran_core
  src/http_translation_client.cpp
  src/translator_node.cpp
)
target_compile_features(mtran_core PUBLIC cxx_std_17)
target_include_directories(mtran_core PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
)
target_link_libraries(mtran_core
  CURL::libcurl
  nlohmann_json::nlohmann_json
  "${cpp_typesupport_target}"
)
ament_target_dependencies(mtran_core rclcpp)

add_executable(mtran_bridge_node src/main.cpp)
target_compile_features(mtran_bridge_node PUBLIC cxx_std_17)
target_link_libraries(mtran_bridge_node mtran_core)
ament_target_dependencies(mtran_bridge_node rclcpp)

install(TARGETS mtran_core mtran_bridge_node
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION lib/${PROJECT_NAME}
)
install(DIRECTORY include/ DESTINATION include)
install(DIRECTORY launch config DESTINATION share/${PROJECT_NAME})
install(PROGRAMS runtime/bin/mtranserver DESTINATION lib/${PROJECT_NAME})
install(DIRECTORY models/en_zh-Hans DESTINATION share/${PROJECT_NAME}/models)
install(FILES runtime/config/records.json DESTINATION share/${PROJECT_NAME}/mtranserver_config)

ament_export_dependencies(rosidl_default_runtime)

if(BUILD_TESTING)
  find_package(ament_cmake_gtest REQUIRED)

  ament_add_gtest(test_http_translation_client
    test/test_http_translation_client.cpp
  )
  target_link_libraries(test_http_translation_client mtran_core)

  ament_add_gtest(test_translator_node
    test/test_translator_node.cpp
  )
  ament_target_dependencies(test_translator_node rclcpp)
  target_link_libraries(test_translator_node mtran_core)
  target_link_libraries(test_translator_node "${cpp_typesupport_target}")
endif()

ament_package()
