cmake_minimum_required(VERSION 3.10)

project(robot_nav_2d_utils VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Enable Position Independent Code
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Find dependencies
find_package(console_bridge REQUIRED)
find_package(Boost REQUIRED COMPONENTS system thread)
find_package(robot_xmlrpcpp QUIET)
if(NOT robot_xmlrpcpp_FOUND)
    # Try alternative package name
    find_package(XmlRpcCpp QUIET)
endif()

# Libraries
add_library(conversions src/conversions.cpp)
target_include_directories(conversions
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
)
target_link_libraries(conversions
    PUBLIC
        robot_nav_2d_msgs
        robot_geometry_msgs
        robot_nav_msgs
        nav_grid
        nav_core2
        robot_cpp
    PRIVATE
        console_bridge::console_bridge
        Boost::system
        Boost::thread
)

set_target_properties(conversions PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

add_library(path_ops src/path_ops.cpp)
target_include_directories(path_ops
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
)
target_link_libraries(path_ops
    PUBLIC
        robot_nav_2d_msgs
        robot_geometry_msgs
        robot_cpp
)

add_library(polygons src/polygons.cpp src/footprint.cpp)
target_include_directories(polygons
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
)

if(robot_xmlrpcpp_FOUND)
    target_include_directories(polygons PRIVATE ${robot_xmlrpcpp_INCLUDE_DIRS})
    target_link_libraries(polygons
        PUBLIC robot_nav_2d_msgs robot_geometry_msgs robot_cpp
        PRIVATE ${robot_xmlrpcpp_LIBRARIES})
elseif(XmlRpcCpp_FOUND)
    target_include_directories(polygons PRIVATE ${XmlRpcCpp_INCLUDE_DIRS})
    target_link_libraries(polygons 
        PUBLIC robot_nav_2d_msgs robot_geometry_msgs robot_cpp 
        PRIVATE ${XmlRpcCpp_LIBRARIES})
else()
    target_include_directories(polygons PRIVATE ${robot_xmlrpcpp_INCLUDE_DIRS})
    target_link_libraries(polygons
        PUBLIC robot_nav_2d_msgs robot_geometry_msgs robot_cpp
        PRIVATE ${robot_xmlrpcpp_LIBRARIES})
endif()

set_target_properties(polygons PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

add_library(bounds src/bounds.cpp)
target_include_directories(bounds
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
)
target_link_libraries(bounds
    PUBLIC
        nav_grid
        nav_core2
        robot_cpp
)

set_target_properties(bounds PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

add_library(tf_help src/tf_help.cpp)
target_include_directories(tf_help
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
)
target_link_libraries(tf_help
    PUBLIC
        robot_nav_2d_msgs
        robot_geometry_msgs
        nav_core2
        tf3
        robot_cpp
)

set_target_properties(tf_help PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

# Create an INTERFACE library that represents all robot_nav_2d_utils libraries
add_library(robot_nav_2d_utils INTERFACE)
target_include_directories(robot_nav_2d_utils
    INTERFACE
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
)
target_link_libraries(robot_nav_2d_utils
    INTERFACE
        conversions
        path_ops
        polygons
        bounds
        tf_help
        robot_cpp
)

# Install header files
install(DIRECTORY include/robot_nav_2d_utils 
        DESTINATION include
        FILES_MATCHING PATTERN "*.h")

# Install mapbox headers
install(DIRECTORY include/mapbox
        DESTINATION include
        FILES_MATCHING PATTERN "*.hpp")

# Install targets
install(TARGETS robot_nav_2d_utils conversions path_ops polygons bounds tf_help
        EXPORT robot_nav_2d_utils-targets
        ARCHIVE DESTINATION lib
        LIBRARY DESTINATION lib
        RUNTIME DESTINATION bin)

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

# 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: conversions, path_ops, polygons, bounds, tf_help")
message(STATUS "Dependencies: robot_nav_2d_msgs, robot_geometry_msgs, robot_nav_msgs, nav_grid, nav_core2, tf3, console_bridge, Boost")
message(STATUS "=================================")
