# SPDX-FileCopyrightText: 2026 Project Tick
# SPDX-License-Identifier: GPL-3.0-or-later
#
# PackUpdater — MeshMC plugin (.mmco)
#
# Records where an instance's modpack was sourced from and pulls
# newer pack releases from Modrinth / CurseForge on demand.
#
# All Qt and MeshMC types come through the SDK header (mmco_cxx_sdk.hpp).
# Source files do NOT directly #include Qt or MeshMC headers — Qt
# pulls in transitively through the SDK target, which is enough.
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
    cmake_minimum_required(VERSION 3.21)
    project(PackUpdater LANGUAGES CXX)

    set(CMAKE_AUTOMOC ON)
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)

    find_package(Qt6 REQUIRED COMPONENTS Core Widgets Gui Network)
    find_package(MeshMC_SDK REQUIRED)

    set(MESHMC_PLUGIN_STAGING_DIR "${CMAKE_BINARY_DIR}/mmcmodules" CACHE PATH
        "Directory where built .mmco plugins are placed")
    set(MMCO_MODULES_DEST_DIR "lib/mmcmodules" CACHE PATH
        "Install directory for .mmco plugins")
    set(MMCO_PLUGIN_DATA_DEST_DIR "share/meshmc/mmcmodules" CACHE PATH
        "Install directory for plugin data files")
endif()

set(PACKUPDATER_SOURCES
    PackUpdaterPlugin.cpp
    PackMetadata.h
    PackMetadata.cpp
    PackUpdaterPage.h
    PackUpdaterPage.cpp
    UpdateSource.h
    UpdateSource.cpp
    ModrinthSource.cpp
    CurseForgeSource.cpp
    AttachDialog.h
    AttachDialog.cpp
    UpdateApplier.h
    UpdateApplier.cpp
    ApplyProgressDialog.h
    ApplyProgressDialog.cpp
)

add_library(PackUpdater MODULE ${PACKUPDATER_SOURCES})

# CurseForgeSource.cpp #includes BuildConfig.h directly to read the
# baked-in CURSEFORGE_API_KEY. The SDK already exports
# buildconfig/ as a public include path and ships the BuildConfig
# implementation inside every .mmco (see PluginLoader.cpp's
# RTLD_NODELETE rationale: each plugin gets its own copy of the
# non-trivially-destructible BuildConfig singleton, so no separate
# link step is needed).
target_link_libraries(PackUpdater PRIVATE
    MeshMC::SDK
)

target_include_directories(PackUpdater PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_BINARY_DIR}
)

set_target_properties(PackUpdater PROPERTIES
    PREFIX ""
    SUFFIX ".mmco"
    LIBRARY_OUTPUT_DIRECTORY "${MESHMC_PLUGIN_STAGING_DIR}"
)

foreach(CFG ${CMAKE_CONFIGURATION_TYPES})
    string(TOUPPER "${CFG}" CFG_UPPER)
    set_target_properties(PackUpdater PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY_${CFG_UPPER} "${MESHMC_PLUGIN_STAGING_DIR}"
    )
endforeach()

install(TARGETS PackUpdater
    LIBRARY DESTINATION "${MMCO_MODULES_DEST_DIR}"
)

if(MeshMC_PLUGIN_SIGN_ALL)
    mmco_sign_plugin(PackUpdater)
endif()
