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

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

else()
  set(BUILDING_WITH_CATKIN FALSE)
  message(STATUS "Building angles 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)

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

else()

# ========================================================
# Catkin specific configuration
# ========================================================
  find_package(catkin REQUIRED)

  catkin_package(
    INCLUDE_DIRS include
    # LIBRARIES không cần vì đây là header-only library
  )

  include_directories(
    include
    ${catkin_INCLUDE_DIRS}
  )
endif()

# Tìm tất cả header files
file(GLOB_RECURSE HEADERS "include/angles/*.h")

# Tạo INTERFACE library (header-only)
add_library(${PROJECT_NAME} INTERFACE)

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

  # Set include directories
  target_include_directories(${PROJECT_NAME}
    INTERFACE 
      $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
      $<INSTALL_INTERFACE:include>
  )

  # Link dependencies (header-only, chỉ cần include paths)
  target_link_libraries(${PROJECT_NAME}
    INTERFACE
      ${catkin_LIBRARIES}
  )

else()

  # Set include directories
  target_include_directories(${PROJECT_NAME}
    INTERFACE 
      $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
      $<INSTALL_INTERFACE:include>
  )

endif()

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}
    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}
    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} (Header-Only)")
  message(STATUS "Version: ${PROJECT_VERSION}")
  message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
  message(STATUS "Headers found:")
  foreach(hdr ${HEADERS})
    message(STATUS "  - ${hdr}")
  endforeach()
  message(STATUS "Dependencies: None")
  message(STATUS "=================================")
endif()
