# Maintainer: Adrian <adrian@mxlinux.org>
pkgname=mx-boot-repair
pkgver=${PKGVER:-26.03}
pkgrel=1
pkgdesc="GUI tool for repairing GRUB bootloader"
arch=('x86_64' 'i686')
url="https://mxlinux.org"
license=('GPL3')
depends=('efibootmgr' 'qt6-base' 'polkit')
makedepends=('cmake' 'ninja' 'qt6-tools')
source=()
sha256sums=()

build() {
    cd "${startdir}"

    # Clean any previous build artifacts
    rm -rf build

    # Configure with CMake
    cmake -G Ninja \
        -B build \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

    # Build
    cmake --build build --parallel
}

package() {
    cd "${startdir}"

    # Install binary
    install -Dm755 build/mx-boot-repair "${pkgdir}/usr/bin/mx-boot-repair"

    # Install translations
    install -dm755 "${pkgdir}/usr/share/mx-bootrepair/locale"
    install -Dm644 build/*.qm "${pkgdir}/usr/share/mx-bootrepair/locale/" 2>/dev/null || true

    # Install helper components
    install -dm755 "${pkgdir}/usr/lib/mx-boot-repair"
    install -Dm755 build/helper "${pkgdir}/usr/lib/mx-boot-repair/helper"

    # Install PolicyKit policy
    install -Dm644 scripts/org.mxlinux.pkexec.mxbr-helper.policy \
        "${pkgdir}/usr/share/polkit-1/actions/org.mxlinux.pkexec.mxbr-helper.policy"

    # Install desktop file
    install -Dm644 mx-boot-repair.desktop "${pkgdir}/usr/share/applications/mx-boot-repair.desktop"

    # Install icons
    install -Dm644 icons/mx-boot-repair.png "${pkgdir}/usr/share/icons/hicolor/256x256/apps/mx-boot-repair.png"
    install -Dm644 icons/mx-boot-repair.svg "${pkgdir}/usr/share/icons/hicolor/scalable/apps/mx-boot-repair.svg"

    # Install documentation
    install -dm755 "${pkgdir}/usr/share/doc/mx-bootrepair"
    if [ -d help ]; then
        cp -r help/* "${pkgdir}/usr/share/doc/mx-bootrepair/" 2>/dev/null || true
    fi

    # Install changelog
    if [ -f debian/changelog ]; then
        gzip -c debian/changelog > "${pkgdir}/usr/share/doc/mx-bootrepair/changelog.gz"
    fi
}
