cmake_minimum_required(VERSION 3.22)
project(Mindball VERSION 0.0.5 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_OSX_DEPLOYMENT_TARGET 12.0 CACHE STRING "Minimum macOS deployment target")

if(CMAKE_BUILD_TYPE STREQUAL "" AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Release)
endif()

set(MINDBALL_FORMATS "AU VST3 Standalone" CACHE STRING "Plugin formats to build (space separated)")
separate_arguments(_formats UNIX_COMMAND "${MINDBALL_FORMATS}")

set(JUCE_ROOT "" CACHE PATH "Path to a local JUCE source tree (default: fetched from GitHub)")
if(JUCE_ROOT)
    add_subdirectory(${JUCE_ROOT} juce)
else()
    include(FetchContent)
    FetchContent_Declare(juce
        GIT_REPOSITORY https://github.com/juce-framework/JUCE.git
        GIT_TAG        8.0.9
        GIT_SHALLOW    TRUE)
    FetchContent_MakeAvailable(juce)
endif()

# A JUCE GUI plugin needs the core system headers to compile on Linux, and
# juce_gui_extra (pulled in transitively via juce_audio_processors) unconditionally
# includes <gtk/gtk.h>. Detect these up front so a missing package produces a
# clear error instead of a confusing "gtk/gtk.h: No such file or directory"
# compile failure. libcurl is not required.
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    set(_mindball_headers  "X11/Xlib.h"                "freetype2/freetype/freetype.h"
                           "fontconfig/fontconfig.h"   "alsa/asoundlib.h")
    set(_mindball_packages "libx11-dev"                "libfreetype-dev"
                           "libfontconfig-dev"         "libasound2-dev")
    set(_mindball_missing "")

    foreach(_i RANGE 0 3)
        list(GET _mindball_headers  ${_i} _header)
        list(GET _mindball_packages ${_i} _package)
        find_path(_mindball_hdr_${_i} NAMES "${_header}" QUIET)
        if(NOT _mindball_hdr_${_i})
            string(APPEND _mindball_missing "  ${_header}   (package: ${_package})\n")
        endif()
    endforeach()

    # juce_gui_extra unconditionally includes <gtk/gtk.h> on Linux.
    find_package(PkgConfig QUIET)
    if(NOT PkgConfig_FOUND)
        string(APPEND _mindball_missing "  pkg-config            (package: pkg-config)\n")
    else()
        pkg_check_modules(_mindball_gtk gtk+-3.0 QUIET)
        if(NOT _mindball_gtk_FOUND)
            string(APPEND _mindball_missing "  gtk/gtk.h             (package: libgtk-3-dev)\n")
        endif()
    endif()

    if(_mindball_missing)
        message(FATAL_ERROR
            "Missing required system packages for the Linux build:\n${_mindball_missing}"
            "Install them with:\n"
            "  # Debian/Ubuntu:\n"
            "  sudo apt-get install pkg-config libx11-dev libfreetype-dev libfontconfig-dev libasound2-dev libgtk-3-dev\n"
            "  # Arch Linux:\n"
            "  sudo pacman -S pkgconf libx11 freetype2 fontconfig alsa-lib gtk3\n"
            "  # Fedora:\n"
            "  sudo dnf install pkgconf-pkg-config libX11-devel freetype-devel fontconfig-devel alsa-lib-devel gtk3-devel\n")
    endif()
endif()

juce_add_plugin(Mindball
    PRODUCT_NAME            "Mindball"
    VENDOR_NAME             "Mindball"
    VERSION                 "0.0.5"
    DESCRIPTION             "Mindball - a minimalist delay plugin"
    FORMATS                 ${_formats}
    PLUGIN_MANUFACTURER_CODE Mind
    PLUGIN_CODE             Mbll
    COPY_PLUGIN_AFTER_BUILD TRUE
    COMPANY_COPYRIGHT       "2026 Mindball")

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

target_include_directories(Mindball PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/buildinfo)
target_link_libraries(Mindball PRIVATE
    juce::juce_audio_processors
    juce::juce_audio_utils
    juce::juce_audio_devices
    juce::juce_gui_basics)

# New VST3-only plugin: no VST2 has ever been released, so the parameter-ID
# migration warning is irrelevant.
target_compile_definitions(Mindball PRIVATE JUCE_IGNORE_VST3_MISMATCHED_PARAMETER_ID_WARNING)

# JUCE 8 defaults JUCE_VST3_CAN_REPLACE_VST2 to 1, which compiles VST2-style
# class IDs into the plugin while the generated moduleinfo.json uses the modern
# IDs, so hosts cannot match the class and fail to load the plugin.
target_compile_definitions(Mindball PRIVATE JUCE_VST3_CAN_REPLACE_VST2=0)

# juce_core enables libcurl by default on Linux; without NEEDS_CURL the JUCE
# helper targets don't link -lcurl, so that would fail at link time with
# undefined references to curl_easy_setopt. We use no network features, so
# disable it.
target_compile_definitions(Mindball PRIVATE JUCE_USE_CURL=0)

# juce_gui_extra unconditionally #includes <gtk/gtk.h> (and webkit) on Linux
# only when JUCE_WEB_BROWSER is enabled. JUCE only adds the GTK include path
# via JUCE_BROWSER_LINUX_DEPS when NEEDS_WEB_BROWSER is set, which in turn
# requires the webkit2gtk dev package. We don't use a web browser, so disable
# JUCE_WEB_BROWSER entirely - this drops the GTK/webkit includes and avoids
# needing webkit2gtk installed just to compile the plugin.
target_compile_definitions(Mindball PRIVATE JUCE_WEB_BROWSER=0)

# ---- git / build info -----------------------------------------------------
find_package(Git QUIET)
set(MINDBALL_GIT_HASH   "n/a")
set(MINDBALL_GIT_BRANCH "n/a")
set(MINDBALL_GIT_DIRTY  false)
if(Git_FOUND)
    execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short=8 HEAD
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        RESULT_VARIABLE _git_hash_res
        OUTPUT_VARIABLE _git_hash
        ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
    if(_git_hash_res EQUAL 0 AND _git_hash)
        set(MINDBALL_GIT_HASH "${_git_hash}")
        execute_process(COMMAND ${GIT_EXECUTABLE} branch --show-current
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
            RESULT_VARIABLE _git_branch_res
            OUTPUT_VARIABLE _git_branch
            ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
        if(_git_branch_res EQUAL 0 AND _git_branch)
            set(MINDBALL_GIT_BRANCH "${_git_branch}")
        endif()
        execute_process(COMMAND ${GIT_EXECUTABLE} status --porcelain
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
            RESULT_VARIABLE _git_status_res
            OUTPUT_VARIABLE _git_status
            ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
        string(LENGTH "${_git_status}" _status_len)
        if(_status_len GREATER 0)
            set(MINDBALL_GIT_DIRTY true)
        endif()
    endif()
endif()
string(TIMESTAMP MINDBALL_BUILD_TIME "%Y-%m-%d %H:%M:%S")

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Source/BuildInfo.h.in
               ${CMAKE_CURRENT_BINARY_DIR}/buildinfo/BuildInfo.h @ONLY)

juce_generate_juce_header(Mindball)
