cmake_minimum_required(VERSION 3.22)
project(Trommelkiste VERSION 1.0.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(build/JUCE)

juce_add_plugin(Trommelkiste
    PRODUCT_NAME "Trommelkiste"
    COMPANY_NAME "Trommelkiste"
    PLUGIN_MANUFACTURER_CODE D909
    PLUGIN_CODE Ds90
    FORMATS VST3 Standalone AU
    IS_SYNTH TRUE
    NEEDS_MIDI_INPUT TRUE
    IS_MIDI_EFFECT FALSE
    EDITOR_WANTS_KEYBOARD_FOCUS FALSE
    COPY_PLUGIN_AFTER_BUILD TRUE
)

target_sources(Trommelkiste PRIVATE
    Source/PluginProcessor.cpp
    Source/PluginEditor.cpp
)

# Embed all WAV samples into the binary
file(GLOB SAMPLE_WAVS "Samples/*.WAV")
juce_add_binary_data(TrommelkisteBinaryData
    HEADER_NAME "BinaryData.h"
    NAMESPACE "BinaryData"
    SOURCES ${SAMPLE_WAVS}
)

# Generate sample manifest mapping names -> binary data pointers
set(_manifest "// Auto-generated - do not edit\n#pragma once\n")
set(_manifest "${_manifest}struct EmbeddedSample { const char* name; const char* data; int size; };\n")
set(_manifest "${_manifest}static const EmbeddedSample embeddedSamples[] = {\n")
foreach(_f ${SAMPLE_WAVS})
    get_filename_component(_we ${_f} NAME_WE)
    string(REGEX REPLACE "[^A-Za-z0-9]" "_" _var ${_we})
    set(_manifest "${_manifest}    { \"${_we}\", BinaryData::${_var}_WAV, BinaryData::${_var}_WAVSize },\n")
endforeach()
set(_manifest "${_manifest}};\n")
set(_manifest "${_manifest}static const int numEmbeddedSamples = sizeof(embeddedSamples) / sizeof(embeddedSamples[0]);\n")
file(WRITE "${CMAKE_BINARY_DIR}/SampleLibrary.h" "${_manifest}")

target_include_directories(TrommelkisteBinaryData PUBLIC "${CMAKE_BINARY_DIR}")

target_compile_definitions(Trommelkiste PRIVATE
    JUCE_WEB_BROWSER=0
    JUCE_USE_CURL=0
    JUCE_VST3_CAN_REPLACE_VST2=0

)

target_link_libraries(Trommelkiste
    PRIVATE
        TrommelkisteBinaryData
        juce::juce_audio_basics
        juce::juce_audio_devices
        juce::juce_audio_formats
        juce::juce_audio_processors
        juce::juce_audio_utils
        juce::juce_core
        juce::juce_data_structures
        juce::juce_dsp
        juce::juce_events
        juce::juce_graphics
        juce::juce_gui_basics
    PUBLIC
        juce::juce_recommended_config_flags
)
