cmake_minimum_required(VERSION 3.10)
project(tf2_sensor_msgs)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Find dependencies
find_package(Boost COMPONENTS thread REQUIRED)
find_package(GTest QUIET)

# Finding Eigen3
find_package(Eigen3 QUIET)
if(NOT EIGEN3_FOUND)
  # Fallback: try to find Eigen via cmake_modules or pkg-config
  find_package(PkgConfig QUIET)
  if(PkgConfig_FOUND)
    pkg_check_modules(EIGEN3 QUIET eigen3)
  endif()
  # If still not found, try common include paths
  if(NOT EIGEN3_FOUND)
    find_path(EIGEN3_INCLUDE_DIRS 
      NAMES Eigen/Core
      PATHS /usr/include/eigen3 /usr/local/include/eigen3
    )
    if(EIGEN3_INCLUDE_DIRS)
      set(EIGEN3_FOUND TRUE)
    endif()
  endif()
endif()

# Note: eigen 3.2 (on older Ubuntu) only provides EIGEN3_INCLUDE_DIR,
# not EIGEN3_INCLUDE_DIRS, so we have to set the latter from the former.
if(NOT EIGEN3_INCLUDE_DIRS)
  set(EIGEN3_INCLUDE_DIRS ${EIGEN3_INCLUDE_DIR})
endif()

if(NOT EIGEN3_FOUND)
  message(FATAL_ERROR "Eigen3 not found. Please install libeigen3-dev")
endif()

# Include directories
include_directories(
  include
  ${EIGEN3_INCLUDE_DIRS}
)

# Install headers
install(DIRECTORY include/${PROJECT_NAME}/
  DESTINATION include/${PROJECT_NAME}
)

# Testing
if(GTest_FOUND OR GTEST_FOUND)
  enable_testing()
  find_package(Threads REQUIRED)
  
  add_executable(test_tf2_sensor_msgs test/test_tf2_sensor_msgs.cpp)
  target_include_directories(test_tf2_sensor_msgs PUBLIC 
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${EIGEN3_INCLUDE_DIRS}
    ${GTEST_INCLUDE_DIRS}
  )
  target_link_libraries(test_tf2_sensor_msgs 
    ${GTEST_LIBRARIES}
    Threads::Threads
  )
  add_test(NAME test_tf2_sensor_msgs COMMAND test_tf2_sensor_msgs)
else()
  message(STATUS "Google Test not found. Tests will not be built.")
endif()
