# Flutter Linux plugin — captchala.

cmake_minimum_required(VERSION 3.13)

set(PROJECT_NAME "captchala")
project(${PROJECT_NAME} LANGUAGES CXX C)

set(PLUGIN_NAME "captchala_plugin")

# Plugin source files compiled directly into the consumer app
add_library(${PLUGIN_NAME} STATIC
    "captchala_plugin.cc"
)

apply_standard_settings(${PLUGIN_NAME})

set_target_properties(${PLUGIN_NAME} PROPERTIES
    CXX_VISIBILITY_PRESET hidden
)

target_compile_definitions(${PLUGIN_NAME} PRIVATE
    FLUTTER_PLUGIN_IMPL
)

target_include_directories(${PLUGIN_NAME}
    INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include"
)

target_link_libraries(${PLUGIN_NAME} PRIVATE
    flutter
    PkgConfig::GTK
    ${CMAKE_DL_LIBS}   # for dlopen / dlsym
)

# Resolve target architecture.
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64")
    set(_CA_ARCH "arm64")
    set(_CA_SHA256 "907684dac51e84045efb2efd38f6f38cac29cf319677174433c6ad2ba15532bd")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64")
    set(_CA_ARCH "x64")
    set(_CA_SHA256 "f3d7e173c03d7bac7813aa3d7a562bc51c08b2ed7dc50c7161968ad4b6dbc8b1")
else()
    message(FATAL_ERROR
        "captchala: unsupported architecture '${CMAKE_SYSTEM_PROCESSOR}'. "
        "Supported: x86_64 (x64), aarch64 (arm64).")
endif()

# Resolve where the prebuilt .so files come from:
#   1. If env var CAPTCHALA_LOCAL_PREBUILDS is set and points to a directory
#      containing libcaptchala_{ui,client}.so, use those (offline / CI / dev).
#   2. Otherwise download from CDN, cache under build/captchala/, verify SHA256.
set(_CA_CACHE_DIR "${CMAKE_BINARY_DIR}/captchala/${_CA_ARCH}")
set(_CA_TARBALL   "${CMAKE_BINARY_DIR}/captchala/linux-${_CA_ARCH}.tar.gz")
set(_CA_UI_SO     "${_CA_CACHE_DIR}/libcaptchala_ui.so")
set(_CA_CLIENT_SO "${_CA_CACHE_DIR}/libcaptchala_client.so")
set(_CA_URLS
    "https://pkg.captcha-pkg.net/flutter/linux-${_CA_ARCH}.tar.gz"
    "https://pkg.captcha-cdn.net/flutter/linux-${_CA_ARCH}.tar.gz"
)

# Local override via env var: bypass CDN entirely.
if(DEFINED ENV{CAPTCHALA_LOCAL_PREBUILDS})
    set(_CA_LOCAL_DIR "$ENV{CAPTCHALA_LOCAL_PREBUILDS}")
    if(EXISTS "${_CA_LOCAL_DIR}/libcaptchala_ui.so" AND EXISTS "${_CA_LOCAL_DIR}/libcaptchala_client.so")
        message(STATUS "captchala: using local prebuilds from ${_CA_LOCAL_DIR}")
        file(MAKE_DIRECTORY "${_CA_CACHE_DIR}")
        file(COPY "${_CA_LOCAL_DIR}/libcaptchala_ui.so" "${_CA_LOCAL_DIR}/libcaptchala_client.so"
             DESTINATION "${_CA_CACHE_DIR}")
    else()
        message(FATAL_ERROR
            "captchala: CAPTCHALA_LOCAL_PREBUILDS=${_CA_LOCAL_DIR} but "
            "libcaptchala_{ui,client}.so not found there.")
    endif()
endif()

if(NOT EXISTS "${_CA_UI_SO}" OR NOT EXISTS "${_CA_CLIENT_SO}")
    file(MAKE_DIRECTORY "${_CA_CACHE_DIR}")
    set(_CA_DL_OK FALSE)
    foreach(_url IN LISTS _CA_URLS)
        message(STATUS "captchala: downloading SDK for ${_CA_ARCH} from ${_url}")
        file(DOWNLOAD "${_url}" "${_CA_TARBALL}"
            SHOW_PROGRESS
            STATUS _CA_DL_STATUS)
        list(GET _CA_DL_STATUS 0 _CA_DL_CODE)
        if(_CA_DL_CODE EQUAL 0)
            file(SHA256 "${_CA_TARBALL}" _CA_ACTUAL_SHA)
            if(_CA_ACTUAL_SHA STREQUAL "${_CA_SHA256}")
                set(_CA_DL_OK TRUE)
                break()
            endif()
            message(WARNING "captchala: SHA256 mismatch from ${_url} "
                "(expected ${_CA_SHA256}, got ${_CA_ACTUAL_SHA}); trying next mirror.")
            file(REMOVE "${_CA_TARBALL}")
        else()
            message(WARNING "captchala: download failed from ${_url} "
                "(${_CA_DL_STATUS}); trying next mirror.")
        endif()
    endforeach()
    if(NOT _CA_DL_OK)
        message(FATAL_ERROR "captchala: all CDN mirrors failed for linux-${_CA_ARCH}.tar.gz.")
    endif()
    file(ARCHIVE_EXTRACT INPUT "${_CA_TARBALL}" DESTINATION "${_CA_CACHE_DIR}")
endif()

set(captchala_bundled_libraries "${_CA_UI_SO};${_CA_CLIENT_SO}" PARENT_SCOPE)
