# Maintainer: DeepChirp <deepchirp@archlinuxcn.org>
# Contributor: Aleksej Kovura <aur-b1a3 at mekboy dot ru>

pkgname=organicmaps
_appid=app.organicmaps.desktop
pkgver=2026.07.23_6
_pkgver=2026.07.23-6-android
pkgrel=1
pkgdesc="A privacy-focused offline maps & GPS app for hiking, cycling, biking, and driving"
arch=('x86_64')
# Reference: https://github.com/organicmaps/organicmaps/blob/master/docs/INSTALL.md
depends=('glibc' 'hicolor-icon-theme' 'libxcursor' 'libxi' 'libxinerama' 'libxrandr' 'libgcc' 'libglvnd' 'libstdc++' 'qt6-base' 'qt6-positioning' 'qt6-svg' 'zlib'
         # WITH_SYSTEM_PROVIDED_3PARTY
         'expat' 'freetype2' 'gflags' 'harfbuzz' 'icu' 'protobuf' 'pugixml')
makedepends=('cmake' 'ninja' 'git' 'jq' 'librsvg' 'python'
             # WITH_SYSTEM_PROVIDED_3PARTY
             'boost' 'fast_float' 'glaze' 'glm' 'utf8cpp' 'vulkan-headers')
checkdepends=('gtest')
optdepends=('qt6-wayland: for Wayland users')
license=('Apache-2.0 AND LicenseRef-OrganicMapsData')
url="https://organicmaps.app"
source=("${pkgname}::git+https://github.com/organicmaps/organicmaps.git#tag=${_pkgver}"
        "git+https://github.com/organicmaps/osmctools.git"
        "git+https://github.com/organicmaps/kothic.git"
        "git+https://github.com/organicmaps/just_gtfs.git"
        "git+https://github.com/thisistherk/fast_obj"
        "git+https://github.com/glfw/glfw.git"
        "git+https://github.com/dpogue/CMake-MetalShaderSupport.git"
        "git+https://github.com/ocornut/imgui"
        "git+https://github.com/organicmaps/twine.git"
        "0001-build-respect-WITH_SYSTEM_PROVIDED_3PARTY-for-glaze.patch"
        "0002-build-do-not-hardcode-compile-and-link-flags.patch"
        "0003-build-guard-test-support-and-quality-libraries.patch")
sha256sums=('0c0278907cf2768deadefb7a4e48be2987163c64347600cf4a8ada30a8287bd5'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'ca79329e3614981c1ff1753e736baab876bb96692bea5008c1c318d8bea00b3c'
            'aa0f3396e483198f827f3d7584e89027a2ae3741510444f3412bd8eeffce904e'
            'ed4bb6d8cad4717a38c5a5e5f509b6d3ae7e4bcd542bb40cfc206efb2874022c')

prepare() {
    cd "${pkgname}"

    # Use system's glaze instead of the bundled one.
    # However, upstream doesn't accept this patch because they think some distributions don't have a recent enough glaze version.
    # https://github.com/organicmaps/organicmaps/pull/12811
    patch -p1 -i "${srcdir}/0001-build-respect-WITH_SYSTEM_PROVIDED_3PARTY-for-glaze.patch"

    # Use the system's flags to prevent them from being overwritten.
    # Fix LDFLAGS overwriting and "Could NOT find Threads" errors.
    # https://github.com/organicmaps/organicmaps/pull/12790
    patch -p1 -i "${srcdir}/0002-build-do-not-hardcode-compile-and-link-flags.patch"

    # Guard test support and quality assessment libraries.
    # Skip them when BUILD_TESTING and SKIP_TOOLS are disabled to prevent compilation errors caused by broken transitive dependencies.
    patch -p1 -i "${srcdir}/0003-build-guard-test-support-and-quality-libraries.patch"

    # Unbundle outdated Protobuf generated headers/sources.
    # We must manually regenerate them using the system's protoc to prevent version mismatches.
    while read -r proto_file; do
        protoc -I. --cpp_out=. "${proto_file}"
    done < <(find libs -name "*.proto")

    # See `if (NOT WITH_SYSTEM_PROVIDED_3PARTY)` in CMakeLists.txt
    local _disabled_submodules=(
        "3party/boost"
        "3party/expat"
        "3party/fast_float"
        "3party/freetype/freetype"
        "3party/gflags"
        "3party/glaze"
        "3party/googletest"
        "3party/glm"
        "3party/harfbuzz/harfbuzz"
        "3party/icu/icu"
        "3party/protobuf/protobuf"
        "3party/pugixml/pugixml"
        "3party/utfcpp"
        "3party/Vulkan-Headers"
    )
    for sub in "${_disabled_submodules[@]}"; do
        git config "submodule.${sub}.update" none
    done

    git submodule init

    git config --file .gitmodules --get-regexp url | while read -r key url; do
        local path="${key#submodule.}"
        path="${path%.url}"

        local _folder=$(basename "${url%.git}")

        if [[ -d "${srcdir}/${_folder}" ]]; then
            git config "submodule.$path.url" "${srcdir}/${_folder}"
        fi
    done

    git -c protocol.file.allow=always submodule update

    sed -i '/-data_path/! s|^Exec=OMaps|Exec=OMaps -data_path /usr/share/organicmaps/data|' qt/res/linux/app.organicmaps.desktop.desktop
}

build() {
    cd "${pkgname}"

    # Reference: tools/unix/build_omim.sh
    local cmake_options=(
        -B build
        -G Ninja
        -D CMAKE_BUILD_TYPE=Release # `CMakeLists.txt` enforces type checking, so `None` cannot be used here; however, we have previously removed O3 via patch
        # Override CMake's default behavior to strip the built-in -O3 flag
        -D CMAKE_C_FLAGS_RELEASE="-DNDEBUG"
        -D CMAKE_CXX_FLAGS_RELEASE="-DNDEBUG"
        -D CMAKE_INSTALL_PREFIX=/usr
        -D WITH_SYSTEM_PROVIDED_3PARTY=ON
        -D BUILD_TESTING=OFF
        -D SKIP_QT_GUI=OFF
        -D SKIP_TOOLS=ON
    )
    cmake "${cmake_options[@]}"

    cmake --build build

    for res in 16 32 48 64 128 256 512 1024; do
        mkdir -p "icons-build/${res}x${res}"
        rsvg-convert -w "${res}" -h "${res}" "android/.idea/icon.svg" -o "icons-build/${res}x${res}/${pkgname}.png"
    done
}

check() {
    cd "${pkgname}"

    local cmake_options=(
        -B build
        -G Ninja
        -D CMAKE_BUILD_TYPE=Release
        -D CMAKE_C_FLAGS_RELEASE="-DNDEBUG"
        -D CMAKE_CXX_FLAGS_RELEASE="-DNDEBUG"
        -D CMAKE_INSTALL_PREFIX=/usr
        -D WITH_SYSTEM_PROVIDED_3PARTY=ON
        -D BUILD_TESTING=ON
        -D SKIP_QT_GUI=OFF
        -D SKIP_TOOLS=ON
    )
    cmake "${cmake_options[@]}"

    cmake --build build

    # From `build-cmake.yaml` "Run tests"
    # In addition, the following tests were also disabled:
    # - base_tests: `is_finite` logic fails due to `-ffast-math`
    # - osm_auth_tests: `server_api_test` fails due to no network access
    # - map_tests: Unable to read system language, therefore can't find `localize.json`
    ctest -j --test-dir build --stop-on-failure --output-on-failure -E "drape_tests|drape_frontend_tests|generator_integration_tests|opening_hours_integration_tests|opening_hours_supported_features_tests|routing_benchmarks|routing_integration_tests|routing_quality_tests|search_quality_tests|storage_integration_tests|shaders_tests|world_feed_integration_tests|base_tests|osm_auth_tests|map_tests"
}

package() {
    cd "${pkgname}"

    install -Dm755 "build/OMaps" -t "${pkgdir}/usr/bin"

    local _datadir="${pkgdir}/usr/share/${pkgname}/data"
    install -dm755 "${_datadir}"

    cp -r data/fonts "${_datadir}/"
    cp -r data/symbols "${_datadir}/"
    cp -r data/vulkan_shaders "${_datadir}/"     # Required for Vulkan rendering on Linux
    cp -r data/countries-strings "${_datadir}/"  # Geographic translations
    cp -r data/sound-strings "${_datadir}/"      # Navigation voice translations

    # Essential Configuration & Metadata
    install -Dm644 data/generator/timezone/timezone_info.json "${_datadir}/generator/timezone/timezone_info.json"
    install -Dm644 data/countries.json "${_datadir}/"
    install -Dm644 data/classificator.txt "${_datadir}/"
    install -Dm644 data/types.txt "${_datadir}/"
    install -Dm644 data/editor.config "${_datadir}/"
    install -Dm644 data/categories.txt "${_datadir}/"
    install -Dm644 data/categories_brands.txt "${_datadir}/"
    install -Dm644 data/categories_cuisines.txt "${_datadir}/"

    # Compiled Drawing Rules
    install -Dm644 data/drules_default.bin "${_datadir}/"
    install -Dm644 data/drules_outdoors.bin "${_datadir}/"
    install -Dm644 data/drules_vehicle.bin "${_datadir}/"

    # Geometry, Colors & Core Maps
    install -Dm644 data/packed_polygons.bin "${_datadir}/"
    install -Dm644 data/colors.txt "${_datadir}/"
    install -Dm644 data/patterns.txt "${_datadir}/"
    install -Dm644 data/transit_colors.txt "${_datadir}/"
    install -Dm644 data/World.mwm "${_datadir}/"
    install -Dm644 data/WorldCoasts.mwm "${_datadir}/"

    # UI & Legal
    install -Dm644 data/welcome.html "${_datadir}/"
    install -Dm644 data/copyright.html "${_datadir}/"

    local _data_version=$(jq -r '.v' data/countries.json)
    install -dm755 "${_datadir}/$_data_version"

    install -Dm644 "android/.idea/icon.svg" "${pkgdir}/usr/share/icons/hicolor/scalable/apps/${pkgname}.svg"
    for res in 16 32 48 64 128 256 512 1024; do
        install -Dm644 "icons-build/${res}x${res}/${pkgname}.png" \
            "${pkgdir}/usr/share/icons/hicolor/${res}x${res}/apps/${pkgname}.png"
    done

    install -Dm644 qt/res/linux/${_appid}.desktop -t "${pkgdir}/usr/share/applications"

    install -Dm644 LICENSE NOTICE DATA_LICENSE.txt -t "${pkgdir}/usr/share/licenses/$pkgname/"
    install -Dm644 packaging/${_appid}.metainfo.xml -t "${pkgdir}/usr/share/metainfo/"
}
