# streamer-tools OBS Camera Plugin - OBS adapter
#
# Thin glue only, per the design doc: source registration, the properties UI,
# and pushing frames into OBS. All real logic lives in ../core. Only added to
# the build when find_package(libobs) succeeds (see top-level CMakeLists.txt).

set(STPLUGIN_PROJECT_NAME "streamer-tools-camera")
set(STPLUGIN_PROJECT_VERSION "${PROJECT_VERSION}")

configure_file(
    src/plugin-support.c.in
    ${CMAKE_CURRENT_BINARY_DIR}/plugin-support.c
    @ONLY
)

add_library(${STPLUGIN_PROJECT_NAME} MODULE
    src/plugin-main.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/plugin-support.c
)

target_include_directories(${STPLUGIN_PROJECT_NAME}
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/src
        ${CMAKE_CURRENT_BINARY_DIR}
)

target_link_libraries(${STPLUGIN_PROJECT_NAME}
    PRIVATE
        OBS::libobs
        stplugin_core
)

set_target_properties(${STPLUGIN_PROJECT_NAME} PROPERTIES
    PREFIX ""
    OUTPUT_NAME ${STPLUGIN_PROJECT_NAME}
)

# The module has to find liblivekit / liblivekit_ffi next to itself once it is
# installed into an OBS plugin directory, not at the build-tree path CMake's
# default RPATH would bake in.
# BUILD_WITH_INSTALL_RPATH is ON deliberately: the artifact that ships is a
# straight copy of the built module (see the staging step below), so the
# build-tree RPATH must never be baked in -- it would work on the build
# machine and nowhere else.
if(APPLE)
    set_target_properties(${STPLUGIN_PROJECT_NAME} PROPERTIES
        BUILD_WITH_INSTALL_RPATH ON
        INSTALL_RPATH "@loader_path"
    )
elseif(UNIX)
    set_target_properties(${STPLUGIN_PROJECT_NAME} PROPERTIES
        BUILD_WITH_INSTALL_RPATH ON
        INSTALL_RPATH "$ORIGIN"
    )
endif()

# --- staged, runnable layout ------------------------------------------------
# Everything a human needs to copy into an OBS plugin directory ends up under
# build/package/, with the LiveKit shared libraries and the licence files
# beside the module. Without this the module loads on the build machine only,
# via the build-tree RPATH.
set(STPLUGIN_PACKAGE_DIR "${CMAKE_BINARY_DIR}/package")

# The binary subdirectory matches what OBS actually searches. From
# AddExtraModulePaths() in obs-studio's UI/window-basic-main.cpp, the
# per-user plugin layout on Linux and Windows is
#   <config>/obs-studio/plugins/<name>/bin/64bit/<name>.{so,dll}
#   <config>/obs-studio/plugins/<name>/data/
# so staging into bin/64bit makes build/package/ a straight drop-in.
#
# macOS is NOT this shape -- there OBS looks for a
# <name>.plugin/Contents/MacOS bundle -- and this build does not produce one.
# See the macOS packaging gap in README.md; the flat bin/ here is honest
# about being unfinished rather than pretending to be installable.
if(APPLE)
    set(STPLUGIN_PACKAGE_BIN_DIR "${STPLUGIN_PACKAGE_DIR}/bin")
else()
    set(STPLUGIN_PACKAGE_BIN_DIR "${STPLUGIN_PACKAGE_DIR}/bin/64bit")
endif()

add_custom_command(TARGET ${STPLUGIN_PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory "${STPLUGIN_PACKAGE_BIN_DIR}"
    COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:${STPLUGIN_PROJECT_NAME}>" "${STPLUGIN_PACKAGE_BIN_DIR}/"
    COMMAND ${CMAKE_COMMAND} -E copy ${LIVEKIT_SDK_RUNTIME_LIBS} "${STPLUGIN_PACKAGE_BIN_DIR}/"
    COMMAND ${CMAKE_COMMAND} -E make_directory "${STPLUGIN_PACKAGE_DIR}/data/locale"
    COMMAND ${CMAKE_COMMAND} -E copy
        "${CMAKE_CURRENT_SOURCE_DIR}/data/locale/en-US.ini"
        "${STPLUGIN_PACKAGE_DIR}/data/locale/"
    # Redistributing LiveKit's prebuilt binaries means shipping their licence
    # and notice with them. See third_party/livekit/README.md -- including
    # what upstream does NOT ship, which is an open question, not a solved one.
    COMMAND ${CMAKE_COMMAND} -E make_directory "${STPLUGIN_PACKAGE_DIR}/licenses/livekit"
    COMMAND ${CMAKE_COMMAND} -E copy
        "${CMAKE_SOURCE_DIR}/third_party/livekit/LICENSE"
        "${CMAKE_SOURCE_DIR}/third_party/livekit/NOTICE"
        "${CMAKE_SOURCE_DIR}/third_party/livekit/README.md"
        "${STPLUGIN_PACKAGE_DIR}/licenses/livekit/"
    COMMAND ${CMAKE_COMMAND} -E copy
        "${CMAKE_SOURCE_DIR}/LICENSE"
        "${STPLUGIN_PACKAGE_DIR}/licenses/"
    COMMENT "Staging plugin + LiveKit runtime libraries + licences into ${STPLUGIN_PACKAGE_DIR}"
    VERBATIM
)
