cmake_minimum_required(VERSION 3.20)
set(PSXRECOMP_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/psxrecomp" CACHE PATH
    "Path to psxrecomp")
if(NOT EXISTS "${PSXRECOMP_ROOT}/runtime/runtime.cmake")
    message(FATAL_ERROR
        "PSXRecomp runtime is missing: ${PSXRECOMP_ROOT}/runtime/runtime.cmake\n"
        "This project expects the psxrecomp framework checkout at ${PSXRECOMP_ROOT}.\n"
        "Run: git submodule update --init --recursive\n"
        "If psxrecomp/ is not a submodule, set -DPSXRECOMP_ROOT=<path-to-psxrecomp>.")
endif()

project(CrashBandicootPAL LANGUAGES C CXX)

# --- Opt-in product surfaces (OFF by default in the framework) -------------
# Scaffold prompts / flags fill these. Without recomp-ui, PSX_RECOMP_UI is OFF.
# This repo ships NO generated game C. The exe is a setup host: the first-run
# wizard asks for the player's own disc, generates BIOS + game C locally, then
# rebuilds into build-release/ (see psxrecomp/docs/LOCAL_CODEGEN_SDK.md).
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/recomp-ui/recomp_ui.cmake")
    set(PSX_RECOMP_UI ON CACHE BOOL "recomp-ui launcher present" FORCE)
else()
    set(PSX_RECOMP_UI OFF CACHE BOOL
        "No recomp-ui submodule in this scaffold" FORCE)
endif()
# Full netplay UI -- enable after adding recomp-ui + testing:
# if(EXISTS "${PSXRECOMP_ROOT}/lib/recomp-net/CMakeLists.txt")
#     set(PSX_NETPLAY ON CACHE BOOL "..." FORCE)
# endif()
# First-run setup wizard -- required: this title has no bundled game C, so the
# player generates and rebuilds from their own disc.
set(PSX_SETUP_WIZARD ON CACHE BOOL "First-run Generate & rebuild from the player's own disc" FORCE)
# Tell runtime.cmake whether this configure has game C. Without it, a setup
# host would still link whatever BIOS C the framework tree carries and claim it
# ships its own BIOS instead of opening the wizard. Recomputed every configure,
# so a checkout that runs Generate & rebuild later links everything normally.
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/generated/SCES_003.44_dispatch.c")
    set(PSXRECOMP_GAME_C_PRESENT ON)
else()
    set(PSXRECOMP_GAME_C_PRESENT OFF)
endif()
include("${PSXRECOMP_ROOT}/runtime/runtime.cmake")

# This project's own mod catalog. The FRAMEWORK stages it -- together with its
# builtin psx.* packages -- into <exe-dir>/mods/bundled, and refuses to
# configure if a catalog exists here but is not declared. Shared Internal
# Resolution is included even with a builtin allowlist, enabled at 4x on fresh
# profiles, with 1x..32x choices; do not supply a title-local copy of that id.
# Never copy the catalog by hand: five titles did, and framework 4cc04be3's rename of the staged
# directory broke all five silently (bead beads-eio.3.101).
set(PSX_PRELOADED_MODS "${CMAKE_CURRENT_SOURCE_DIR}/mods/preloaded")
if(NOT IS_DIRECTORY "${PSX_PRELOADED_MODS}")
    set(PSX_PRELOADED_MODS "NONE")
endif()

psxrecomp_add_game_runtime(psx-runtime
    PRELOADED_MODS_DIR "${PSX_PRELOADED_MODS}"
    # ENABLE_NETPLAY_IF_PRESENT
    # NETPLAY_LOBBY_URL ws://netplay.retcomm.net:8765
    ENABLE_SETUP_WIZARD
    WINDOW_TITLE "CrashBandicootPAL"
    DEFAULT_GAME_CONFIG_PATH "game.toml"
    MAX_PLAYERS 1
    # No LAUNCHER_BOXART: box art is third-party trademark art and is not shipped.
    APP_ICON "${CMAKE_CURRENT_SOURCE_DIR}/assets/psxrecomp.ico"
    # Game C is generated on the player's machine from their own disc and stays
    # out of the repo. A checkout with an empty generated/ builds a setup host;
    # after Generate & rebuild the same target links the real code.
    GEN_MARKER "generated/SCES_003.44_dispatch.c"
    GEN_FULL_GLOB "generated/SCES_003.44_full_*.c"
    # Compiles the codegen host the wizard drives (Generate then rebuild).
    CODEGEN_SETUP_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/codegen_setup.c"
)

# Stage runtime configs next to the exe for direct build/ launches. Only copy
# files that exist — never fail POST_BUILD when game_options.toml is absent.
foreach(_PSX_RUNTIME_CONFIG game.toml game_options.toml)
    if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_PSX_RUNTIME_CONFIG}")
        add_custom_command(TARGET psx-runtime POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
                "${CMAKE_CURRENT_SOURCE_DIR}/${_PSX_RUNTIME_CONFIG}"
                "$<TARGET_FILE_DIR:psx-runtime>/${_PSX_RUNTIME_CONFIG}"
            COMMENT "Staging ${_PSX_RUNTIME_CONFIG}"
            VERBATIM)
    endif()
endforeach()
