cmake_minimum_required(VERSION 3.16)

project(hatsune-miku-shoshitsu
        VERSION 0.1.0
        LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# --- dependencies -----------------------------------------------------------
find_package(PkgConfig REQUIRED)
find_package(Threads REQUIRED)

# Video decoding uses the FFmpeg C API (libavformat / libavcodec / libavutil /
# libswscale). On Debian: sudo apt install libavformat-dev libavcodec-dev \
# libavutil-dev libswscale-dev pkg-config
pkg_check_modules(FFMPEG REQUIRED IMPORTED_TARGET libavformat libavcodec libavutil libswscale)

# --- core library: video / lyrics / music modules ---------------------------
# MINIAUDIO_IMPLEMENTATION lives in musicPlayer.cpp, so it is compiled once.
add_library(hms-core STATIC
    modules/backgroundDisplay.cpp
    modules/lyricResolve.cpp
    modules/musicPlayer.cpp)

target_include_directories(hms-core PUBLIC modules third_party)

target_link_libraries(hms-core PUBLIC
    PkgConfig::FFMPEG
    Threads::Threads
    ${CMAKE_DL_LIBS})

# miniaudio audio backends. Disable backends whose dev headers are missing so
# the build still succeeds; at runtime the device will just be unavailable.
if(WIN32)
    # WASAPI / WinMM on Windows (MSYS2 / MSVC)
    target_link_libraries(hms-core PUBLIC ole32 winmm)
elseif(UNIX AND NOT APPLE)
    pkg_check_modules(ALSA QUIET IMPORTED_TARGET alsa)
    pkg_check_modules(PULSE QUIET IMPORTED_TARGET libpulse-simple)

    if(ALSA_FOUND)
        target_link_libraries(hms-core PUBLIC PkgConfig::ALSA)
        # Lets musicPlayer suppress ALSA's own error chatter (which would
        # otherwise corrupt the TUI when no sound card exists).
        target_compile_definitions(hms-core PUBLIC HMS_HAS_ALSA=1)
    else()
        target_compile_definitions(hms-core PUBLIC MA_NO_ALSA)
    endif()

    if(PULSE_FOUND)
        target_link_libraries(hms-core PUBLIC PkgConfig::PULSE)
    else()
        target_compile_definitions(hms-core PUBLIC
            MA_NO_PULSEAUDIO MA_NO_JACK MA_NO_OSS MA_NO_SNDIO)
    endif()

    target_link_libraries(hms-core PUBLIC m)
endif()

# --- the uninstall-entry binary ---------------------------------------------
# Placeholder main() for now (uninstall.cpp); will become the real uninstall
# hook that runs the show.
add_executable(hatsune-miku-shoshitsu uninstall.cpp)
target_link_libraries(hatsune-miku-shoshitsu PRIVATE hms-core)

# Installed to /usr/bin — the Debian packaging relies on this rule.
install(TARGETS hatsune-miku-shoshitsu
        RUNTIME DESTINATION bin)

# --- demo driver: previews the show (video + music + lyrics) ----------------
# Default resource paths are baked in as string-literal macros. TO_CMAKE_PATH
# normalises to forward slashes so the literals stay valid on Windows builds.
file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/asciivision/demo-videos/demo.mp4" HMS_DEMO_VIDEO)
file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/res/music.flac" HMS_DEMO_MUSIC)
file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/res/lyric.lrc" HMS_DEMO_LYRICS)

add_executable(hatsune-miku-demo tools/demo.cpp)
target_compile_definitions(hatsune-miku-demo PRIVATE
    HMS_DEMO_VIDEO="${HMS_DEMO_VIDEO}"
    HMS_DEMO_MUSIC="${HMS_DEMO_MUSIC}"
    HMS_DEMO_LYRICS="${HMS_DEMO_LYRICS}")
target_link_libraries(hatsune-miku-demo PRIVATE hms-core)
