# CI package recipe for the Souveraine UPower fork.
#
# The archdev runner builds upower-souveraine natively per arch (meson +
# ninja), then makepkg assembles this package from the prebuilt tree. The
# package is folded into the same souveraine-{arch} pacman database as the
# souveraine agent substrate, so the phone installs it from the same edge
# release archive it already consumes.
#
# Provides/conflicts upower so installing this package replaces the stock
# distro upower. /etc/pacman.conf must list `upower` under IgnorePkg so a
# later stock upower from the distro can't clobber the fork. (IgnorePkg only
# skips automatic upgrades; an explicit -S upower-souveraine still wins.)
pkgname=upower-souveraine
pkgver="${SOUVERAINE_PKGVER:?CI must set SOUVERAINE_PKGVER}"
pkgrel=1
pkgdesc="UPower fork (Souveraine) — exposes ChargeType and PercentageTrusted over D-Bus"
arch=("${SOUVERAINE_PKGARCH:?CI must set SOUVERAINE_PKGARCH}")
url="https://gitea.wiuf.net/Fimeg/upower"
license=('GPL-2.0-or-later')
provides=("upower=1.91.3" 'libupower-glib.so=3-64')
conflicts=('upower')
depends=('glib2' 'glibc' 'libgcc' 'libgudev' 'libimobiledevice' 'libplist' 'polkit')
options=('!strip' '!libtool')
# Prebuilt install tree produced by `meson install --destdir` in CI, plus the
# introspection XML (so quickshell's UPower module can introspect the new
# ChargeType/PercentageTrusted properties).
source=('upower-tree.tar' 'org.freedesktop.UPower.Device.xml')
sha256sums=('SKIP' 'SKIP')

package() {
    bsdtar -xf "$srcdir/upower-tree.tar" -C "$pkgdir"

    # Detector, not a fixer — ci.yml is where a leaked cross-install tree gets
    # folded back. If one reaches here the tarball is wrong, and shipping it
    # would put /home/<user>/aarch64-sysroot/... on the device and leave udev
    # with no rules (measured on the phone 2026-07-27). Fail loudly instead.
    local stray
    # usr/, etc/ (UPower.conf) and var/ (var/lib/upower) are all legitimate.
    stray=$(find "$pkgdir" -mindepth 1 -maxdepth 1 \
              ! -name usr ! -name etc ! -name var -printf '%f\n')
    [[ -z $stray ]] || { error "upower tree ships outside usr/, etc/ and var/: %s" "$stray"; return 1; }
    # Keep the canonical interface XML discoverable for introspection.
    install -Dm644 "$srcdir/org.freedesktop.UPower.Device.xml" \
        "$pkgdir/usr/share/dbus-1/interfaces/org.freedesktop.UPower.Device.xml"
}
