# EchoionStandalone — the daemon that runs Core without a TeamSpeak client.
#
# It reuses the plugin's (TeamSpeak-independent) CoreLoader/FileWatcher to load
# and hot-reload Core, but implements IPluginInterface with EchoionProtocol's
# native UDP client. Built with PLUGIN=1 so it shares the plugin's minimal-API
# ABI contract with Core (self-contained ServerConfig, no ConfigTypes).

cmake_minimum_required(VERSION 4.0.0)
project(EchoionStandalone VERSION 1.0.0 LANGUAGES CXX)

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

# dylib for the Core hot-reload loader (same version the plugin uses).
CPMAddPackage(
    NAME dylib
    GITHUB_REPOSITORY martin-olivier/dylib
    GIT_TAG "v3.0.1"
)

find_package(PkgConfig REQUIRED)
pkg_check_modules(SWRESAMPLE REQUIRED IMPORTED_TARGET libswresample libavutil)
pkg_check_modules(OPUS REQUIRED IMPORTED_TARGET opus)
find_package(Threads REQUIRED)

set(PLUGIN_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../EchoionPlugin")

add_executable(EchoionStandalone
    src/main.cpp
    src/StandaloneClientInterface.cpp
    src/AudioEncoder.cpp
    # Reused, TeamSpeak-independent loader from the plugin (transition shim;
    # a shared EchoionHostCommon lib can extract these later).
    "${PLUGIN_DIR}/src/CoreLoader.cpp"
    "${PLUGIN_DIR}/src/FileWatcher.cpp"
)

# PLUGIN=1 gives the self-contained ServerConfig + minimal API, matching the
# ABI Core was built against for the plugin path.
target_compile_definitions(EchoionStandalone PRIVATE PLUGIN=1)

target_include_directories(EchoionStandalone PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/include"
    "${PLUGIN_DIR}/include"
)

target_link_libraries(EchoionStandalone PRIVATE
    Echoion::Protocol
    Echoion::ApiBase
    Echoion::ApiPlugin
    Echoion::ApiOpaque
    Echoion::ApiPluginInterface
    Echoion::ApiCoreInterface
    Echoion::ApiLogger
    Void::Data
    Void::Log
    Void::Stacktrace
    dylib
    Threads::Threads
    PkgConfig::SWRESAMPLE
    PkgConfig::OPUS
)

set_target_properties(EchoionStandalone PROPERTIES
    OUTPUT_NAME "echoiond"
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)

install(TARGETS EchoionStandalone RUNTIME DESTINATION bin)
