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

if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
  set(BUILDING_WITH_CATKIN TRUE)
  message(STATUS "Building mkt_algorithm with Catkin")

else()
  set(BUILDING_WITH_CATKIN FALSE)
  message(STATUS "Building mkt_algorithm 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 system)
find_package(Eigen3 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
    score_algorithm
    robot_nav_2d_msgs
    robot_nav_2d_utils
    kalman
    angles
    nav_grid
    robot_costmap_2d
    robot_sensor_msgs
    robot_std_msgs
    robot_cpp
  )

else()

# ========================================================
# Catkin specific configuration
# ========================================================
  find_package(catkin REQUIRED COMPONENTS
    geometry_msgs
    score_algorithm
    robot_nav_2d_msgs
    robot_nav_2d_utils
    kalman
    angles
    nav_grid
    robot_costmap_2d
    robot_sensor_msgs
    robot_std_msgs
    robot_cpp
  )

  catkin_package(
    INCLUDE_DIRS include
    LIBRARIES ${PROJECT_NAME}_diff
    CATKIN_DEPENDS geometry_msgs score_algorithm robot_nav_2d_msgs robot_nav_2d_utils kalman angles nav_grid robot_costmap_2d robot_sensor_msgs robot_std_msgs robot_cpp
    DEPENDS Boost Eigen3
  )

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

# ========================================================
# Libraries
# ========================================================
add_library(${PROJECT_NAME}_diff SHARED
  src/diff/diff_predictive_trajectory.cpp
  src/diff/diff_rotate_to_goal.cpp
  src/diff/diff_go_straight.cpp
)

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

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

  target_link_libraries(${PROJECT_NAME}_diff
    PUBLIC ${catkin_LIBRARIES}
    PRIVATE Boost::system Eigen3::Eigen
  )

else()

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

  target_link_libraries(${PROJECT_NAME}_diff
    PUBLIC
      ${PACKAGES_DIR}
    PRIVATE
      Boost::system Eigen3::Eigen
  )

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

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}_diff
    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
  )

  ## Install plugins.xml if it exists
  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/plugins.xml)
    install(FILES plugins.xml
      DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
    )
  endif()

else()

  install(TARGETS ${PROJECT_NAME}_diff
    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
  )

  ## Install plugins.xml if it exists
  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/plugins.xml)
    install(FILES plugins.xml
      DESTINATION share/${PROJECT_NAME}
    )
  endif()

  # 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}_diff")
  message(STATUS "Dependencies: geometry_msgs, score_algorithm, robot_nav_2d_msgs, robot_nav_2d_utils, kalman, angles, nav_grid, robot_costmap_2d, robot_sensor_msgs, robot_std_msgs, robot_cpp, Boost, Eigen3")
  message(STATUS "=================================")
endif()
