cmake_minimum_required(VERSION 3.0.2)
project(move_base VERSION 1.0.0 LANGUAGES CXX)

if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
  set(BUILDING_WITH_CATKIN TRUE)
  message(STATUS "Building move_base with Catkin")
  
else()
  set(BUILDING_WITH_CATKIN FALSE)
  message(STATUS "Building move_base with Standalone CMake")
endif()

# C++ Standard - must be set before find_package
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Find dependencies
find_package(Boost REQUIRED COMPONENTS filesystem system)
find_package(Threads REQUIRED)
find_package(yaml-cpp REQUIRED)
find_package(console_bridge REQUIRED)

if (NOT BUILDING_WITH_CATKIN)

  # Enable Position Independent Code
  set(CMAKE_POSITION_INDEPENDENT_CODE ON)

  # Cấu hình RPATH để tránh cycle trong runtime search path
  set(CMAKE_BUILD_RPATH_USE_ORIGIN TRUE)
  set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
  set(CMAKE_BUILD_RPATH "${CMAKE_BINARY_DIR}")

  set(PACKAGES_DIR  
    geometry_msgs
    robot_std_msgs
    move_base_core
    robot_nav_core
    robot_costmap_2d
    plugins
    tf3
    robot_tf3_sensor_msgs
    robot_tf3_geometry_msgs
    data_convert
    robot_nav_2d_utils
    robot_cpp
    robot_move_base_msgs
    laser_filter
  )
  find_library(TF3_LIBRARY NAMES tf3 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
else()

# ========================================================
# Catkin specific configuration
# ========================================================
  find_package(catkin REQUIRED COMPONENTS         
    geometry_msgs
    robot_std_msgs
    move_base_core
    robot_nav_core
    robot_costmap_2d
    robot_tf3_sensor_msgs
    robot_tf3_geometry_msgs
    data_convert
    robot_nav_2d_utils
    robot_cpp
    robot_move_base_msgs
  )
  find_library(TF3_LIBRARY NAMES tf3 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
  catkin_package(
    INCLUDE_DIRS include
    LIBRARIES ${PROJECT_NAME}
    CATKIN_DEPENDS geometry_msgs robot_std_msgs move_base_core robot_nav_core robot_costmap_2d robot_tf3_sensor_msgs robot_tf3_geometry_msgs data_convert robot_nav_2d_utils robot_cpp robot_move_base_msgs
    DEPENDS Boost yaml-cpp
  )

  include_directories(
    include
    ${catkin_INCLUDE_DIRS}
    ${Boost_INCLUDE_DIRS}
    ${TF3_INCLUDE_DIR}
  )
endif()

# ========================================================
# Libraries
# ========================================================
add_library(${PROJECT_NAME} SHARED
  src/move_base.cpp
  src/convert_data.cpp
  src/simple_action_server.cpp
)

if(BUILDING_WITH_CATKIN)
  add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

  target_compile_definitions(${PROJECT_NAME}
    PUBLIC BUILD_WITH_ROS
  )

  target_include_directories(${PROJECT_NAME}
    PUBLIC
      $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
      $<INSTALL_INTERFACE:include>
    ${TF3_INCLUDE_DIR}
  )

  target_link_libraries(${PROJECT_NAME}
    PUBLIC ${catkin_LIBRARIES}
    PRIVATE Boost::filesystem Boost::system
    dl
    ${TF3_LIBRARY}
    ${console_bridge_LIBRARIES}
  )

else()

  target_include_directories(${PROJECT_NAME}
    PUBLIC
      $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
      $<INSTALL_INTERFACE:include>
    ${TF3_INCLUDE_DIR}
  )

  target_link_libraries(${PROJECT_NAME}
    PUBLIC
      ${PACKAGES_DIR}
      ${TF3_LIBRARY}
      ${console_bridge_LIBRARIES}
    PRIVATE
      Boost::filesystem Boost::system
      yaml-cpp
      dl
  )

  set_target_properties(${PROJECT_NAME} PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
    BUILD_RPATH "${CMAKE_BINARY_DIR}"
    INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib"
  )

endif()

# ========================================================
# Executable
# ========================================================
add_executable(${PROJECT_NAME}_main src/move_base_main.cpp)

if(BUILDING_WITH_CATKIN)
  target_link_libraries(${PROJECT_NAME}_main PRIVATE
    ${PROJECT_NAME}
    robot_cpp
    Boost::filesystem Boost::system
    dl
    ${TF3_LIBRARY}
    ${console_bridge_LIBRARIES}
  )
else()
  target_link_libraries(${PROJECT_NAME}_main PRIVATE
    ${PROJECT_NAME}
    robot_cpp
    Boost::filesystem Boost::system
    dl
    ${TF3_LIBRARY}
    ${console_bridge_LIBRARIES}
  )

  # Configure RPATH to find libraries in devel space
  set_target_properties(${PROJECT_NAME}_main PROPERTIES
    BUILD_WITH_INSTALL_RPATH FALSE
    INSTALL_RPATH_USE_LINK_PATH TRUE
    BUILD_RPATH "${CMAKE_BINARY_DIR}"
  )
endif()

# ========================================================
# Install
# ========================================================
if(BUILDING_WITH_CATKIN)
  ## Mark libraries for installation
  ## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
  install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_main
    ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
    LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
    RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
  )

  ## Mark cpp header files for installation
  install(DIRECTORY include/${PROJECT_NAME}/
    DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
    FILES_MATCHING PATTERN "*.h"
    PATTERN ".svn" EXCLUDE
  )

else()
  install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_main  
    EXPORT ${PROJECT_NAME}-targets
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib
    RUNTIME DESTINATION bin
  )

  # Export targets
  install(EXPORT ${PROJECT_NAME}-targets
    FILE ${PROJECT_NAME}-targets.cmake
    NAMESPACE ${PROJECT_NAME}::
    DESTINATION lib/cmake/${PROJECT_NAME}
  )

  ## Mark cpp header files for installation
  install(DIRECTORY include/${PROJECT_NAME}/
    DESTINATION include
    FILES_MATCHING PATTERN "*.h"
    PATTERN ".svn" EXCLUDE
  )

  # Print configuration info
  message(STATUS "=================================")
  message(STATUS "Project: ${PROJECT_NAME}")
  message(STATUS "Version: ${PROJECT_VERSION}")
  message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
  message(STATUS "Libraries: ${PROJECT_NAME}")
  message(STATUS "Executables: ${PROJECT_NAME}_main")
    message(STATUS "Dependencies: geometry_msgs, robot_std_msgs, move_base_core, robot_nav_core, robot_costmap_2d, robot_tf3_sensor_msgs, robot_tf3_geometry_msgs, data_convert, robot_nav_2d_utils, robot_cpp, robot_move_base_msgs, Boost, yaml-cpp")
  message(STATUS "=================================")
endif()
