cmake_minimum_required(VERSION 3.10)
project(sbpl_recovery)

# C++17 theo quy ước workspace cho code mới.
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(catkin REQUIRED COMPONENTS
  recovery_core
  robot_cpp
  robot_costmap_2d
  robot_nav_core
  robot_geometry_msgs
  robot_nav_msgs
  robot_tf3_geometry_msgs
)

find_package(Boost REQUIRED COMPONENTS
  system
  filesystem
  thread
)

find_package(Threads REQUIRED)

# tf3 cài sẵn ngoài workspace (xem CLAUDE.md) — không phải package catkin.
find_library(TF3_LIBRARY
  NAMES tf3

  PATHS
    /usr/lib
    /usr/local/lib
    /usr/lib/x86_64-linux-gnu
)

if(NOT TF3_LIBRARY)
  message(FATAL_ERROR
    "sbpl_recovery: libtf3 not found. Install tf3 (see the auto_setup.sh scripts under "
    "Controllers/, Devices/, Localizations/) or add its location to CMAKE_PREFIX_PATH.")
endif()

catkin_package(
  INCLUDE_DIRS
    include

  LIBRARIES
    sbpl_recovery

  CATKIN_DEPENDS
    recovery_core
    robot_cpp
    robot_costmap_2d
    robot_nav_core
    robot_geometry_msgs
    robot_nav_msgs
    robot_tf3_geometry_msgs

  DEPENDS
    Boost
)

# ==================================================================================================
# Plugin: libsbpl_recovery.so — export symbol `DetourPathRecovery` (Boost.DLL)
# ==================================================================================================

add_library(sbpl_recovery SHARED
  plugins/detour_path_recovery.cpp
)

target_compile_options(sbpl_recovery PRIVATE -Wall -Wextra)

# Header dependency vào dạng SYSTEM để -Wall -Wextra của chính gói không bị warning của
# robot_cpp/costmap_2d nhấn chìm — cùng lý do với recovery_core.
target_include_directories(sbpl_recovery SYSTEM
  PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${catkin_INCLUDE_DIRS}
    ${Boost_INCLUDE_DIRS}
)

target_link_libraries(sbpl_recovery
  PRIVATE
    ${catkin_LIBRARIES}
    Boost::system
    Boost::filesystem
    Boost::thread
    Threads::Threads
    ${CMAKE_DL_LIBS}
    ${TF3_LIBRARY}
)

add_dependencies(sbpl_recovery ${catkin_EXPORTED_TARGETS})

install(TARGETS sbpl_recovery
  ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
)

# ==================================================================================================
# Test
# ==================================================================================================

if(CATKIN_ENABLE_TESTING)
  # Planner giả cho unit test — nạp qua đúng đường Boost.DLL mà runtime đi.
  add_library(sbpl_recovery_test_line_planner SHARED
    test/plugins/test_line_planner.cpp
  )
  target_compile_options(sbpl_recovery_test_line_planner PRIVATE -Wall -Wextra)
  target_include_directories(sbpl_recovery_test_line_planner SYSTEM
    PRIVATE
      ${catkin_INCLUDE_DIRS}
      ${Boost_INCLUDE_DIRS}
  )
  target_link_libraries(sbpl_recovery_test_line_planner
    PRIVATE
      ${catkin_LIBRARIES}
      Boost::system
      Boost::filesystem
      Threads::Threads
      ${CMAKE_DL_LIBS}
  )

  catkin_add_gtest(detour_path_recovery_test test/detour_path_recovery_test.cpp)
  if(TARGET detour_path_recovery_test)
    target_compile_options(detour_path_recovery_test PRIVATE -Wall -Wextra)
    target_compile_definitions(detour_path_recovery_test PRIVATE
      SBPL_RECOVERY_TEST_CONFIG_DIR="${CMAKE_CURRENT_SOURCE_DIR}/test/config"
      SBPL_RECOVERY_TEST_LIBRARY_DIR="${CATKIN_DEVEL_PREFIX}/lib"
    )
    target_include_directories(detour_path_recovery_test SYSTEM
      PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/include
        ${catkin_INCLUDE_DIRS}
        ${Boost_INCLUDE_DIRS}
    )
    # catkin_add_gtest dùng target_link_libraries dạng plain, nên phần bổ sung cũng phải plain.
    target_link_libraries(detour_path_recovery_test
      ${catkin_LIBRARIES}
      ${Boost_LIBRARIES}
      pthread
      ${CMAKE_DL_LIBS}
      ${TF3_LIBRARY}
    )
    # Plugin KHÔNG link thẳng vào test: chúng được nạp qua RecoveryRegistry lúc chạy, đúng đường
    # runtime — nhưng phải build xong trước khi test chạy.
    add_dependencies(detour_path_recovery_test
      sbpl_recovery
      sbpl_recovery_test_line_planner
    )
  endif()
endif()
