# Copyright (c) French Touch Factory. All Rights Reserved.
#
# Builds the one preview server, ithaca_rgbd_server, against an already-built
# OrbbecSDK v2. Deliberately outside the SDK tree: reconfiguring the SDK on an
# older Jetson can break the recorder build that already works, so we link the
# library it produced instead of building inside it.
#
# Portable across Jetson generations by construction: every SDK path comes from a
# cache variable that build_server.sh fills from OB_SDK_ROOT, and nothing here is
# tied to one L4T, one home directory, or one arm64 build folder name.
#
# The retired GStreamer/RTSP target is gone — the direct route encodes no video,
# so GStreamer is not a dependency any more.

cmake_minimum_required(VERSION 3.10)
project(ithaca_node_server CXX)

# Where the SDK source (headers) and its build output (libs, generated headers,
# extensions) live. build_server.sh passes these; the defaults match the layout
# build_sdk.sh produces.
set(OB_SDK_ROOT "$ENV{HOME}/OrbbecSDK_v2" CACHE PATH
    "OrbbecSDK v2 source root (headers)")
set(OB_SDK_BUILD "" CACHE PATH
    "OrbbecSDK v2 build output (lib/, lib/extensions/)")
set(OB_SDK_GENERATED "" CACHE PATH
    "OrbbecSDK generated headers, where Export.h lands")

# The build folder is named per platform (linux_arm64, linux_aarch64, ...), so it
# is discovered rather than assumed when the caller did not pass it.
if(NOT OB_SDK_BUILD)
    file(GLOB _ob_build_candidates "${OB_SDK_ROOT}/build/linux_*")
    if(_ob_build_candidates)
        list(GET _ob_build_candidates 0 OB_SDK_BUILD)
    else()
        set(OB_SDK_BUILD "${OB_SDK_ROOT}/build")
    endif()
endif()
if(NOT OB_SDK_GENERATED)
    set(OB_SDK_GENERATED "${OB_SDK_ROOT}/build/src/generated")
endif()

if(NOT EXISTS "${OB_SDK_ROOT}/include/libobsensor/ObSensor.hpp")
    message(FATAL_ERROR "OrbbecSDK headers not found under ${OB_SDK_ROOT}/include "
                        "— set OB_SDK_ROOT or run scripts/build_sdk.sh first.")
endif()
if(NOT EXISTS "${OB_SDK_BUILD}/lib/libOrbbecSDK.so")
    message(FATAL_ERROR "libOrbbecSDK.so not found in ${OB_SDK_BUILD}/lib "
                        "— build the SDK first (scripts/build_sdk.sh).")
endif()
if(NOT EXISTS "${OB_SDK_GENERATED}/Export.h")
    message(FATAL_ERROR "Export.h not found in ${OB_SDK_GENERATED} "
                        "— the SDK is not fully built yet.")
endif()

# Directory-scoped rather than target_link_directories(): that one needs CMake
# >= 3.13 and the oldest supported Jetson (Nano, JetPack 4) ships 3.10.2. Must
# precede add_executable().
link_directories("${OB_SDK_BUILD}/lib")

add_executable(ithaca_rgbd_server ithaca_rgbd_server.cpp)
set_property(TARGET ithaca_rgbd_server PROPERTY CXX_STANDARD 14)
target_compile_options(ithaca_rgbd_server PRIVATE -Wall -Wextra -O2)

# Where the depth engine and filter plugins are loaded from at runtime.
target_compile_definitions(ithaca_rgbd_server PRIVATE
    OB_EXTENSIONS_DIR="${OB_SDK_BUILD}/lib/extensions")

# SYSTEM: the SDK's public headers are not warning-clean and this builds -Wall
# -Wextra. ObTypes.h includes Export.h, which the SDK generates into its build
# tree rather than shipping in include/, hence both directories.
target_include_directories(ithaca_rgbd_server SYSTEM PRIVATE
    "${OB_SDK_ROOT}/include"
    "${OB_SDK_GENERATED}")

# libjpeg by absolute path, not -ljpeg: the SDK ships its own libjpeg.a in the
# directory link_directories() adds above, built without its SIMD objects — the
# linker would pick it first and fail on jsimd_* symbols. Only C2D alignment
# needs an encoder at all.
find_library(SYSTEM_JPEG NAMES jpeg PATHS
    /usr/lib/aarch64-linux-gnu /usr/lib/arm-linux-gnueabihf NO_DEFAULT_PATH)
if(NOT SYSTEM_JPEG)
    message(FATAL_ERROR "system libjpeg not found — install libjpeg-turbo8-dev.")
endif()

target_link_libraries(ithaca_rgbd_server PRIVATE OrbbecSDK lz4 ${SYSTEM_JPEG} pthread)

# The SDK lives outside any standard search path; without an rpath the binary
# only runs from a shell that already exports LD_LIBRARY_PATH.
set_target_properties(ithaca_rgbd_server PROPERTIES
    BUILD_RPATH   "${OB_SDK_BUILD}/lib"
    INSTALL_RPATH "${OB_SDK_BUILD}/lib")

install(TARGETS ithaca_rgbd_server RUNTIME DESTINATION bin)
