cmake_minimum_required(VERSION 3.22)

project(LiuXingTimecodeTool VERSION 2.6.21)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# JUCE 源码位置（项目内的 libs/JUCE）
add_subdirectory(libs/JUCE juce_build)

juce_add_plugin(LiuXingTimecodeTool
    IS_SYNTH FALSE
    PLUGIN_MANUFACTURER_CODE LXMS
    PLUGIN_CODE TCOD
    FORMATS AU VST3 Standalone
    MICROPHONE_PERMISSION_ENABLED TRUE
    MICROPHONE_PERMISSION_TEXT "LiuXing Timecode Tool needs microphone/audio input access to decode incoming LTC timecode."
    PRODUCT_NAME "LiuXing Timecode Tool"
    NEEDS_WEB_BROWSER FALSE
    NEEDS_CURL FALSE)

juce_generate_juce_header(LiuXingTimecodeTool)

target_sources(LiuXingTimecodeTool
    PRIVATE
        Source/PluginProcessor.cpp
        Source/PluginEditor.cpp
        Source/StandaloneApp.cpp
        Source/OscManager.cpp
        Source/UpdateChecker.cpp)

target_compile_definitions(LiuXingTimecodeTool
    PUBLIC
        JUCE_WEB_BROWSER=0
        JUCE_USE_CURL=0
        JUCE_VST3_CAN_REPLACE_VST2=0
        JUCE_USE_CUSTOM_PLUGIN_STANDALONE_APP=1)

target_link_libraries(LiuXingTimecodeTool
    PRIVATE
        juce::juce_audio_utils
        juce::juce_osc
    PUBLIC
        juce::juce_recommended_config_flags
        juce::juce_recommended_lto_flags
        juce::juce_recommended_warning_flags)

# SCNetworkInterface（网卡友好名）需要 SystemConfiguration 框架；隔离在 .mm 避免 MacTypes Point 冲突
if(APPLE)
    enable_language(OBJCXX)
    find_library(LIB_SystemConfiguration SystemConfiguration REQUIRED)
    target_link_libraries(LiuXingTimecodeTool PUBLIC "${LIB_SystemConfiguration}")
    target_sources(LiuXingTimecodeTool PRIVATE Source/NetInterfaceNames.mm)
endif()

# 独立应用加 ATS 例外（允许明文 HTTP 连更新服务器）+ 声明中文本地化（让网卡友好名跟随系统语言）
if(APPLE)
    add_custom_command(TARGET LiuXingTimecodeTool_Standalone POST_BUILD
        COMMAND /usr/libexec/PlistBuddy
            -c "Add :NSAppTransportSecurity dict"
            -c "Add :NSAppTransportSecurity:NSExceptionDomains dict"
            -c "Add :NSAppTransportSecurity:NSExceptionDomains:8.138.156.225 dict"
            -c "Add :NSAppTransportSecurity:NSExceptionDomains:8.138.156.225:NSExceptionAllowsInsecureHTTPLoads bool true"
            -c "Add :CFBundleDevelopmentRegion string en"
            -c "Add :CFBundleLocalizations array"
            -c "Add :CFBundleLocalizations:0 string en"
            -c "Add :CFBundleLocalizations:1 string zh-Hans"
            "$<TARGET_BUNDLE_CONTENT_DIR:LiuXingTimecodeTool_Standalone>/Info.plist"
        COMMAND codesign --force -s - "$<TARGET_BUNDLE_DIR:LiuXingTimecodeTool_Standalone>"
        COMMENT "Adding ATS exception + localizations to standalone app Info.plist")
endif()
