cmake_minimum_required(VERSION 3.20)

set(PSXRECOMP_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/psxrecomp" CACHE PATH
    "Path to the pinned PSXRecomp framework")
if(NOT EXISTS "${PSXRECOMP_ROOT}/runtime/runtime.cmake")
    message(FATAL_ERROR
        "PSXRecomp runtime is missing at ${PSXRECOMP_ROOT}. "
        "Initialize the psxrecomp submodule before configuring.")
endif()

project(KTDIR LANGUAGES C CXX)

# Kileak is a single-player title. The launcher provides controller discovery,
# remapping/testing, and graphics settings before entering the game.
set(PSX_RECOMP_UI ON CACHE BOOL "Build the PSXRecomp launcher UI" FORCE)
set(RECOMP_UI_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/recomp-ui" CACHE PATH
    "Path to the pinned recomp-ui checkout" FORCE)
set(PSX_SETUP_WIZARD ON CACHE BOOL
    "Enable legal-disc generation and local rebuild from the launcher" FORCE)
set(PSX_REWIND OFF CACHE BOOL "Rewind requires the optional rbengine backend" FORCE)
# SDL 3.4.10 does not classify the PowerA Switch 2 controller (20d6:a720).
# KTDIR builds the pinned SDL source so the narrowly scoped, reviewed device
# classification patch below is present even when a toolchain ships prebuilt SDL.
set(PSX_SDL3_FORCE_FETCH ON CACHE BOOL
    "Build patched SDL3 source for KTDIR controller compatibility" FORCE)
include("${PSXRECOMP_ROOT}/runtime/runtime.cmake")

psxrecomp_add_game_runtime(psx-runtime
    WINDOW_TITLE "Kileak: The DNA Imperative Recompiled"
    DEFAULT_GAME_CONFIG_PATH "game.toml"
    MAX_PLAYERS 1
    GEN_MARKER "generated/SCUS-941.02_dispatch.c"
    GEN_FULL_GLOB "generated/SCUS-941.02_full_*.c"
    CODEGEN_SETUP_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/codegen_setup.c"
    ENABLE_SETUP_WIZARD
)

target_sources(psx-runtime PRIVATE src/ktdir_enhancements.c)
target_include_directories(psx-runtime PRIVATE
    "${PSXRECOMP_ROOT}/runtime/include")

# Title-owned, data-free enhancement manifests. The framework catalog is
# staged first; this merges Kileak's verified optional features afterwards.
add_custom_command(TARGET psx-runtime POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${CMAKE_CURRENT_SOURCE_DIR}/mods"
        "$<TARGET_FILE_DIR:psx-runtime>/mods"
    COMMENT "Staging Kileak enhancement catalog"
    VERBATIM)

foreach(_KTDIR_CONFIG game.toml game_options.toml input.ini keybinds.ini)
    if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_KTDIR_CONFIG}")
        add_custom_command(TARGET psx-runtime POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
                "${CMAKE_CURRENT_SOURCE_DIR}/${_KTDIR_CONFIG}"
                "$<TARGET_FILE_DIR:psx-runtime>/${_KTDIR_CONFIG}"
            VERBATIM)
    endif()
endforeach()

# MinGW's C/C++ runtimes and zlib remain dynamic even though SDL3 is linked
# statically. Stage exactly the imported DLLs so the build runs on a clean
# Windows 10/11 machine without an MSYS2 PATH dependency.
if(MINGW)
    get_filename_component(_KTDIR_TOOLCHAIN_BIN "${CMAKE_CXX_COMPILER}" DIRECTORY)
    foreach(_KTDIR_DLL libgcc_s_seh-1.dll libstdc++-6.dll libwinpthread-1.dll zlib1.dll)
        if(EXISTS "${_KTDIR_TOOLCHAIN_BIN}/${_KTDIR_DLL}")
            add_custom_command(TARGET psx-runtime POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy_if_different
                    "${_KTDIR_TOOLCHAIN_BIN}/${_KTDIR_DLL}"
                    "$<TARGET_FILE_DIR:psx-runtime>/${_KTDIR_DLL}"
                VERBATIM)
        endif()
    endforeach()
endif()
