cmake_minimum_required(VERSION 3.22)

project(VibeDAW VERSION 0.1.0 LANGUAGES C CXX)

# ------------------------------------------------------------------------------
#  Licensing
#
#  VibeDAW is GPLv3. That is a deliberate choice, not a default: it makes JUCE
#  free of charge in perpetuity (no revenue cap, no splash screen), and it makes
#  the whole GPL DSP ecosystem — Rubber Band, FFTW — available at no cost. GPL
#  does not prevent selling the result; Ardour is a GPL DAW sold commercially.
#
#  The obligation it carries: ship the source with any binary you distribute.
# ------------------------------------------------------------------------------

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

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

# MSVC reads sources in the system codepage unless told otherwise, so any
# non-ASCII byte in a narrow string literal is mangled — a UTF-8 arrow came out
# as three garbage characters. This makes it read and emit UTF-8 throughout.
if (MSVC)
    add_compile_options(/utf-8)
endif()

# ------------------------------------------------------------------------------
#  JUCE:  -DVIBEDAW_JUCE_LOCAL=/path/to/JUCE  for a local checkout
# ------------------------------------------------------------------------------
if (DEFINED VIBEDAW_JUCE_LOCAL)
    message(STATUS "VibeDAW: using local JUCE at ${VIBEDAW_JUCE_LOCAL}")
    add_subdirectory(${VIBEDAW_JUCE_LOCAL} ${CMAKE_BINARY_DIR}/JUCE)
else()
    include(FetchContent)
    FetchContent_Declare(
        juce
        GIT_REPOSITORY https://github.com/juce-framework/JUCE.git
        GIT_TAG        8.0.4
        GIT_SHALLOW    TRUE)
    FetchContent_MakeAvailable(juce)
endif()

# ------------------------------------------------------------------------------
#  Application
# ------------------------------------------------------------------------------
juce_add_gui_app(VibeDAW
    PRODUCT_NAME   "VibeDAW"
    COMPANY_NAME   "VibeDAW"
    VERSION        ${PROJECT_VERSION}
    MICROPHONE_PERMISSION_ENABLED TRUE)   # audio recording opens device inputs

juce_generate_juce_header(VibeDAW)

target_sources(VibeDAW PRIVATE
    Source/Main.cpp
    Source/MainComponent.cpp
    Source/Core/Transport.cpp
    Source/Core/Project.cpp
    Source/Audio/Engine.cpp
    Source/Audio/Track.cpp
    Source/Audio/BeatDetector.cpp
    Source/Devices/PluginDevice.cpp
    Source/Plugins/PluginManager.cpp
    Source/GUI/VibeLookAndFeel.cpp)

# ------------------------------------------------------------------------------
#  Optional embedded fonts. Drop any .ttf into Assets/Fonts and it is bundled;
#  an empty folder falls back to the system font.
# ------------------------------------------------------------------------------
file(GLOB VIBEDAW_FONTS CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/Assets/Fonts/*.ttf")
if (VIBEDAW_FONTS)
    juce_add_binary_data(VibeDAWData SOURCES ${VIBEDAW_FONTS})
    target_link_libraries(VibeDAW PRIVATE VibeDAWData)
    target_compile_definitions(VibeDAW PRIVATE VIBEDAW_HAS_EMBEDDED_FONT=1)
    message(STATUS "VibeDAW: embedding font(s): ${VIBEDAW_FONTS}")
else()
    message(STATUS "VibeDAW: no fonts in Assets/Fonts — using the system font.")
endif()

target_compile_definitions(VibeDAW PRIVATE
    JUCE_WEB_BROWSER=0
    JUCE_USE_CURL=0
    JUCE_STRICT_REFCOUNTEDPOINTER=1

    # Permitted under GPLv3 — the splash screen is a condition of the free
    # Personal tier, which we are not using.
    JUCE_DISPLAY_SPLASH_SCREEN=0

    # Plugin hosting. LV2 is ISC and carries no third-party terms at all; VST3 is
    # GPLv3-compatible, so it is free for this project too. VST2 stays off — its
    # SDK is no longer issued by Steinberg and cannot be legally obtained.
    JUCE_PLUGINHOST_LV2=1
    JUCE_PLUGINHOST_VST3=1
    JUCE_PLUGINHOST_VST=0)

target_link_libraries(VibeDAW PRIVATE
    juce::juce_audio_utils
    juce::juce_dsp
    PRIVATE
    juce::juce_recommended_config_flags
    juce::juce_recommended_lto_flags
    juce::juce_recommended_warning_flags)
