cmake_minimum_required(VERSION 3.0.2)

# ========================================================
# 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 robot_cpp with Catkin")
else()
  set(BUILDING_WITH_CATKIN FALSE)
  message(STATUS "Building robot_cpp with Standalone CMake")
endif()

project(robot_cpp)

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

# ========================================================
# Find Packages
# ========================================================

if(BUILDING_WITH_CATKIN)
  ## Find catkin macros and libraries
  find_package(catkin REQUIRED COMPONENTS
    robot_xmlrpcpp
  )
endif()

## System dependencies
# Find yaml-cpp shared library first, prioritizing system installation over /usr/local
# This avoids using static library from /usr/local/lib

# First, try pkg-config to get the correct library path
find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
  pkg_check_modules(YAMLCPP_PKG yaml-cpp QUIET)
  if(YAMLCPP_PKG_FOUND AND YAMLCPP_PKG_LIBRARIES)
    set(YAML_CPP_TARGET ${YAMLCPP_PKG_LIBRARIES})
    message(STATUS "Using yaml-cpp from pkg-config: ${YAML_CPP_TARGET}")
  endif()
endif()

# If pkg-config didn't work, find shared library directly, prioritizing /usr/lib
if(NOT YAML_CPP_TARGET)
  # First try system paths (avoid /usr/local which may have static library)
  find_library(YAML_CPP_SHARED_LIB
    NAMES yaml-cpp
    PATHS /usr/lib
    PATH_SUFFIXES x86_64-linux-gnu
    NO_DEFAULT_PATH
  )
  
  if(YAML_CPP_SHARED_LIB AND EXISTS "${YAML_CPP_SHARED_LIB}")
    # Verify it's actually a shared library (not a symlink to static)
    get_filename_component(YAML_CPP_REAL_LIB "${YAML_CPP_SHARED_LIB}" REALPATH)
    if(YAML_CPP_REAL_LIB MATCHES "\\.so")
      set(YAML_CPP_TARGET ${YAML_CPP_SHARED_LIB})
      message(STATUS "Found yaml-cpp shared library in /usr/lib: ${YAML_CPP_SHARED_LIB}")
      # Also find include directory
      find_path(YAML_CPP_INCLUDE_DIR
        NAMES yaml-cpp/yaml.h
        PATHS /usr/include
        NO_DEFAULT_PATH
      )
      if(YAML_CPP_INCLUDE_DIR)
        message(STATUS "Found yaml-cpp include directory: ${YAML_CPP_INCLUDE_DIR}")
      endif()
    endif()
  endif()
endif()

# Now use find_package, but temporarily exclude /usr/local to avoid static library
set(CMAKE_PREFIX_PATH_SAVED ${CMAKE_PREFIX_PATH})
list(REMOVE_ITEM CMAKE_PREFIX_PATH "/usr/local")
find_package(yaml-cpp QUIET)
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH_SAVED})

# Use target if available and we haven't found a library yet
if(NOT YAML_CPP_TARGET)
  if(TARGET yaml-cpp::yaml-cpp)
    set(YAML_CPP_TARGET yaml-cpp::yaml-cpp)
    message(STATUS "Using yaml-cpp target: yaml-cpp::yaml-cpp")
  elseif(TARGET yaml-cpp)
    set(YAML_CPP_TARGET yaml-cpp)
    message(STATUS "Using yaml-cpp target: yaml-cpp")
  else()
    # Last resort: try to find any library, but check if it's static
    find_library(YAML_CPP_ANY_LIB
      NAMES yaml-cpp
      PATHS /usr/lib /usr/local/lib /lib
      PATH_SUFFIXES x86_64-linux-gnu
    )
    
    if(YAML_CPP_ANY_LIB)
      if(YAML_CPP_ANY_LIB MATCHES "\\.a$")
        message(FATAL_ERROR 
          "yaml-cpp static library found at: ${YAML_CPP_ANY_LIB}\n"
          "This cannot be linked into a shared library.\n"
          "Solution: Remove static library or use system shared library:\n"
          "  sudo rm /usr/local/lib/libyaml-cpp.a\n"
          "Or ensure /usr/lib/x86_64-linux-gnu/libyaml-cpp.so is found first."
        )
      else()
        set(YAML_CPP_TARGET ${YAML_CPP_ANY_LIB})
        message(STATUS "Found yaml-cpp library: ${YAML_CPP_TARGET}")
      endif()
    else()
      set(YAML_CPP_TARGET yaml-cpp)
      message(WARNING "yaml-cpp library not found. Using library name 'yaml-cpp'")
    endif()
  endif()
endif()

find_package(console_bridge REQUIRED)

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

if(BUILDING_WITH_CATKIN)
  ## The catkin_package macro generates cmake config files for your package
  ## Note: yaml-cpp and console_bridge are system dependencies, not declared in DEPENDS
  ## as they don't provide the required INCLUDE_DIRS/LIBRARIES variables for catkin
  catkin_package(
    INCLUDE_DIRS include
    LIBRARIES robot_cpp
    CATKIN_DEPENDS robot_xmlrpcpp
  )
endif()

###########
## Build ##
###########

## Compiler flags
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  # Warning flags - disabled to suppress warnings during build
  # add_compile_options(-Wall -Wextra -Wpedantic -fPIC)
  add_compile_options(-w -fPIC)  # -w suppresses all warnings
endif()

## Include directories
include_directories(
  include
)

# yaml-cpp and console_bridge are linked via targets, so we don't need to add their include dirs
# if they provide targets (modern CMake), or we handle them via target_link_libraries

if(BUILDING_WITH_CATKIN)
  include_directories(${catkin_INCLUDE_DIRS})
endif()

# Create unified robot_cpp library combining console and node_handle
add_library(${PROJECT_NAME} SHARED
  src/console.cpp
  src/node_handle.cpp
  src/plugin_loader_helper.cpp
)

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

## Link libraries
# Use the determined yaml-cpp target/library
target_link_libraries(${PROJECT_NAME}
  PUBLIC
    ${YAML_CPP_TARGET}
    robot_xmlrpcpp
    robot_time
  PRIVATE
    dl  # Required for dladdr() function used in plugin_loader_helper.cpp
)

# Check if we're using static library and warn user
if(YAML_CPP_TARGET MATCHES "\\.a$" OR YAML_CPP_TARGET MATCHES "libyaml-cpp\\.a")
  message(FATAL_ERROR 
    "yaml-cpp static library detected at: ${YAML_CPP_TARGET}\n"
    "Static libraries cannot be linked into shared libraries without -fPIC.\n"
    "Solution: Install shared library version:\n"
    "  sudo apt-get install libyaml-cpp-dev\n"
    "Or rebuild yaml-cpp with -fPIC flag."
  )
endif()


## Add cmake target dependencies
if(BUILDING_WITH_CATKIN)
  add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
endif()

set_target_properties(${PROJECT_NAME} PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

# Define CMAKE_BINARY_DIR as compile definition so it's available at runtime
target_compile_definitions(${PROJECT_NAME} 
  PRIVATE 
    PNKX_NAV_CORE_BUILD_DIR="${CMAKE_BINARY_DIR}"
)

# ========================================================
# Installation (Standalone CMake only)
# ========================================================

# 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 ${PROJECT_NAME}
  EXPORT ${PROJECT_NAME}-targets
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin)

if(NOT BUILDING_WITH_CATKIN)
  install(DIRECTORY include/
    DESTINATION include
    FILES_MATCHING PATTERN "*.h")

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

  # ========================================================
  # Install CMake Config File for find_package()
  # ========================================================
  # Generate config file from template
  configure_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/robot_cppConfig.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/robot_cppConfig.cmake
    @ONLY
  )

  # Install config file
  install(FILES
    ${CMAKE_CURRENT_BINARY_DIR}/robot_cppConfig.cmake
    DESTINATION lib/cmake/${PROJECT_NAME}
  )
else()
  # Khi build với Catkin, vẫn cần export để các target khác có thể export
  install(EXPORT ${PROJECT_NAME}-targets
    # NAMESPACE robot::
    DESTINATION lib/cmake/${PROJECT_NAME})
endif()

# ========================================================
# Install CMake Config File for find_package()
# ========================================================
# Generate config file from template
configure_file(
  ${CMAKE_CURRENT_SOURCE_DIR}/robot_cppConfig.cmake.in
  ${CMAKE_CURRENT_BINARY_DIR}/robot_cppConfig.cmake
  @ONLY
)

# Install config file
install(FILES
  ${CMAKE_CURRENT_BINARY_DIR}/robot_cppConfig.cmake
  DESTINATION lib/cmake/${PROJECT_NAME}
)

# ========================================================
# Test executable
# ========================================================
option(BUILD_TESTS "Build test programs" OFF)

if(BUILD_TESTS)
  add_executable(test_node_handle
    test/test_node_handle.cpp
  )
  
  target_include_directories(test_node_handle
    PRIVATE
      ${CMAKE_CURRENT_SOURCE_DIR}/include
  )
  
  target_link_libraries(test_node_handle
    PRIVATE
      robot_cpp
      yaml-cpp
  )
  
  set_target_properties(test_node_handle PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
  )
  
  # Link filesystem library if needed
  if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0")
    target_link_libraries(test_node_handle PRIVATE stdc++fs)
  elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    find_library(FILESYSTEM_LIB c++fs)
    if(FILESYSTEM_LIB)
      target_link_libraries(test_node_handle PRIVATE ${FILESYSTEM_LIB})
    endif()
  endif()
  
  # Add test to CTest
  add_test(NAME NodeHandleTest COMMAND test_node_handle)
  
  message(STATUS "Test executable 'test_node_handle' will be built")
endif()
