cmake_minimum_required(VERSION 3.16)
project(deltacast-bridge C)
set(CMAKE_C_STANDARD 17)

set(SDK_ROOT "/sdk" CACHE PATH "Path to extracted VideoMaster SDK")

# Legacy FIFO mode — set LEGACY_FIFO=ON to disable framecache shm writes
# and fall back to the original named-FIFO path.
option(LEGACY_FIFO "Use named FIFOs instead of framecache shm" OFF)

# Primary binary: deltacast-bridge (shared multi-port daemon)
add_executable(deltacast-bridge main.c fc_writer.c)

if(LEGACY_FIFO)
    target_compile_definitions(deltacast-bridge PRIVATE LEGACY_FIFO=1)
    message(STATUS "deltacast-bridge: LEGACY_FIFO mode enabled (shm disabled)")
else()
    message(STATUS "deltacast-bridge: framecache shm mode enabled")
endif()

target_include_directories(deltacast-bridge PRIVATE
    ${SDK_ROOT}/include/videomaster
)

target_link_directories(deltacast-bridge PRIVATE
    ${SDK_ROOT}/lib
)

target_link_libraries(deltacast-bridge PRIVATE
    videomasterhd
    videomasterhd_audio
    pthread
    rt      # shm_open, sem_open
)

# Embed the SDK RPATH so the binary finds the .so at runtime
set_target_properties(deltacast-bridge PROPERTIES
    INSTALL_RPATH "/usr/local/lib/deltacast"
    BUILD_WITH_INSTALL_RPATH TRUE
)

# Compat symlink: deltacast-capture -> deltacast-bridge
# (node-agent and any legacy scripts that reference the old name still work)
add_custom_command(TARGET deltacast-bridge POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E create_symlink
        $<TARGET_FILE:deltacast-bridge>
        $<TARGET_FILE_DIR:deltacast-bridge>/deltacast-capture
    COMMENT "Creating deltacast-capture compat symlink"
)
