cmake_minimum_required(VERSION 3.22)
project(mpq_extractor VERSION 1.0.0)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# ── Paths ──────────────────────────────────────────────────────────────────────
set(GODOT_CPP_DIR "${CMAKE_SOURCE_DIR}/../../third_party/godot-cpp" CACHE PATH "Path to godot-cpp")
set(STORMLIB_DIR  "${CMAKE_SOURCE_DIR}/../../third_party/StormLib"  CACHE PATH "Path to StormLib")

# ── godot-cpp ──────────────────────────────────────────────────────────────────
add_subdirectory(${GODOT_CPP_DIR} godot-cpp)

# ── StormLib ───────────────────────────────────────────────────────────────────
# Disable StormLib's own tests/examples
set(BUILD_SHARED_LIBS    OFF CACHE BOOL "" FORCE)
set(STORMLIB_BUILD_TESTS OFF CACHE BOOL "" FORCE)
add_subdirectory(${STORMLIB_DIR} StormLib)

# ── Extension library ──────────────────────────────────────────────────────────
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "src/*.cpp")

add_library(mpq_extractor SHARED ${SOURCES})

target_include_directories(mpq_extractor PRIVATE
    src
    ${STORMLIB_DIR}/src
)

target_link_libraries(mpq_extractor PRIVATE
    godot-cpp
    storm
)

# ── Output: addons/mpq_extractor/bin/ ─────────────────────────────────────────
set(OUTPUT_DIR "${CMAKE_SOURCE_DIR}/../../addons/mpq_extractor/bin")

set_target_properties(mpq_extractor PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY         "${OUTPUT_DIR}"
    RUNTIME_OUTPUT_DIRECTORY         "${OUTPUT_DIR}"
    LIBRARY_OUTPUT_DIRECTORY_DEBUG   "${OUTPUT_DIR}"
    LIBRARY_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIR}"
    RUNTIME_OUTPUT_DIRECTORY_DEBUG   "${OUTPUT_DIR}"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIR}"
)

# Platform-specific naming (Godot convention: lib<name>.<platform>.<arch>.dll/so)
if(WIN32)
    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
        set(PLATFORM_SUFFIX "windows.x86_64")
    else()
        set(PLATFORM_SUFFIX "windows.x86_32")
    endif()
    set_target_properties(mpq_extractor PROPERTIES
        PREFIX ""
        OUTPUT_NAME "libmpq_extractor.${PLATFORM_SUFFIX}"
        SUFFIX ".dll"
    )
elseif(UNIX AND NOT APPLE)
    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
        set(PLATFORM_SUFFIX "linux.x86_64")
    else()
        set(PLATFORM_SUFFIX "linux.x86_32")
    endif()
    set_target_properties(mpq_extractor PROPERTIES
        PREFIX ""
        OUTPUT_NAME "libmpq_extractor.${PLATFORM_SUFFIX}"
        SUFFIX ".so"
    )
elseif(APPLE)
    set_target_properties(mpq_extractor PROPERTIES
        PREFIX ""
        OUTPUT_NAME "libmpq_extractor.macos.universal"
        SUFFIX ".dylib"
    )
endif()

# ── Install (optional) ─────────────────────────────────────────────────────────
message(STATUS "mpq_extractor → ${OUTPUT_DIR}")
