cmake_minimum_required(VERSION 3.10)

# ========================================================
# Dual-mode CMakeLists.txt: Supports both Catkin and Standalone CMake
# ========================================================

# Detect if building with Catkin
if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
  set(BUILDING_WITH_CATKIN TRUE)
  message(STATUS "Building utils with Catkin")
  find_package(catkin REQUIRED)
else()
  set(BUILDING_WITH_CATKIN FALSE)
  message(STATUS "Building utils with Standalone CMake")
endif()

# --- Project riêng cho utils ---
project(utils LANGUAGES CXX)

# ========================================================
# Catkin specific configuration
# ========================================================

if(BUILDING_WITH_CATKIN)
  ## The catkin_package macro generates cmake config files for your package
  catkin_package(
    INCLUDE_DIRS include
    # LIBRARIES không cần vì đây là header-only library
  )
endif()

# --- Tạo INTERFACE library (header-only) ---
add_library(utils INTERFACE)

# --- Include directories ---
target_include_directories(utils
    INTERFACE
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>   # build nội bộ
        $<INSTALL_INTERFACE:include>                             # dùng khi install/export
)

# --- Cài đặt header files ---
if(NOT BUILDING_WITH_CATKIN)
  install(DIRECTORY include/
          DESTINATION include
          FILES_MATCHING PATTERN "*.h"
  )
endif()

# --- Cài đặt target INTERFACE để export ---
# Export target trong mọi trường hợp để các target khác có thể export và phụ thuộc vào nó
install(TARGETS utils
        EXPORT utils-targets
        INCLUDES DESTINATION include
)

# --- Export target file ---
install(EXPORT utils-targets
        FILE utils-targets.cmake
        NAMESPACE utils::
        DESTINATION lib/cmake/utils
)
