# Reviewed upstream-source build, not an automatically executed AUR recipe.
# Release: https://codeberg.org/Calamares/calamares/releases/tag/v3.4.3
pkgname=calamares
pkgver=3.4.3
pkgrel=2
pkgdesc='Distribution-independent installer framework with Python modules for Meowven OS'
arch=('x86_64')
url='https://calamares.io/'
license=('GPL-3.0-or-later')
depends=('kcoreaddons' 'kconfig' 'ki18n' 'kwidgetsaddons' 'kpmcore'
         'libpwquality' 'libxcrypt' 'icu' 'parted' 'qt6-base'
         'qt6-declarative' 'qt6-svg' 'yaml-cpp' 'polkit-qt6'
         'python' 'python-yaml' 'python-jsonschema' 'rsync' 'squashfs-tools')
makedepends=('cmake>=3.24' 'extra-cmake-modules' 'ninja' 'qt6-tools' 'qt6-translations'
             'libglvnd' 'pybind11>=3.0')
source=("https://codeberg.org/Calamares/calamares/releases/download/v${pkgver}/calamares-${pkgver}.tar.gz")
sha256sums=('144cbbf6bdcebfb21685950db4f0777218519df095f62f4aa392f28348110d18')

# This is the complete set used by Meowven's settings.conf. Unused QML views,
# terminal integrations and package choosers must not add optional dependencies
# or change the package depending on what happens to be installed on the builder.
_meowven_modules=(welcome locale keyboard partition users summary mount unpackfs
                  shellprocess machineid localecfg fstab initcpiocfg initcpio
                  displaymanager networkcfg hwclock services-systemd grubcfg bootloader
                  umount finished)

prepare() {
    # Upstream 3.4.3 bundles pybind11 2.11.1, which predates Python 3.14 support.
    # Keep its Pybind backend but use Arch's maintained headers. Both libraries
    # link Python::Python explicitly; the imported headers target adds no runtime
    # dependency on the pybind11 package.
    cat > "calamares-${pkgver}/3rdparty/pybind11/CMakeLists.txt" <<'CMAKE'
find_package(pybind11 3.0 CONFIG REQUIRED GLOBAL)
CMAKE
}

build() {
    local directory module wanted skip_modules='' enabled
    for directory in "calamares-${pkgver}/src/modules/"*/; do
        module=${directory%/}
        module=${module##*/}
        enabled=false
        for wanted in "${_meowven_modules[@]}"; do
            if [[ $module == "$wanted" ]]; then
                enabled=true
                break
            fi
        done
        if [[ $enabled == false ]]; then
            skip_modules+="${module};"
        fi
    done
    cmake -S "calamares-${pkgver}" -B build -G Ninja \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_INSTALL_LIBDIR=lib \
        -DCMAKE_INSTALL_SYSCONFDIR=/etc \
        -DWITH_QT6=ON -DWITH_PYTHON=ON -DWITH_PYBIND11=ON \
        -Dpybind11_DIR="$(python -m pybind11 --cmakedir)" \
        -DWITH_QML=ON -DINSTALL_CONFIG=OFF -DINSTALL_POLKIT=ON \
        -DBUILD_TESTING=OFF -DBUILD_SCHEMA_TESTING=ON \
        -DBUILD_CRASH_REPORTING=OFF \
        -DSKIP_MODULES="$skip_modules" \
        -DUSE_services=systemd \
        -DDEBUG_PARTITION_UNSAFE=OFF -DDEBUG_PARTITION_SKIP=OFF
    cmake --build build --parallel "${CMAKE_BUILD_PARALLEL_LEVEL:-2}"
}

check() {
    # Exercise the compiled Python ABI, not a stub from upstream's ci/ directory.
    # These checks only load the API and display version information; no installer
    # jobs, including partitioning, are created or run.
    PYTHONPATH="$srcdir/build" python -c \
        'import libcalamares; assert libcalamares.VERSION_SHORT == "3.4.3"; assert hasattr(libcalamares.utils, "target_env_call")'
    QT_QPA_PLATFORM=offscreen "$srcdir/build/calamares" --version
}

package() {
    DESTDIR="$pkgdir" cmake --install build
    # Do not silently produce an installer with missing required jobs.
    local module
    for module in "${_meowven_modules[@]}"; do
        if [[ ! -f "$pkgdir/usr/lib/calamares/modules/$module/module.desc" ]]; then
            printf 'Required Calamares module missing: %s\n' "$module" >&2
            return 1
        fi
    done
    [[ -f "$pkgdir/usr/lib/calamares/modules/unpackfs/main.py" ]]
    [[ -f "$pkgdir/usr/lib/calamares/modules/bootloader/main.py" ]]
    [[ -f "$pkgdir/usr/share/polkit-1/actions/io.calamares.calamares.policy" ]]
    # Meowven supplies a launcher that checks live-media/session requirements.
    rm -f "$pkgdir/usr/share/applications/calamares.desktop"
}
