# aarch64-only build: Arch Linux ARM builds qemu without the static user-mode
# binaries, so the Omarchy default set (qemu-user-static-binfmt, used for
# foreign-architecture containers) repackages Debian's statically linked
# qemu-user build. On x86_64 both packages come from the Arch extra repository.

pkgbase=qemu-user-static
pkgname=('qemu-user-static' 'qemu-user-static-binfmt')
# One-time epoch moves from upstream-only to full Debian revision ordering.
# pkgver records Debian epoch.upstream.revision. Updates are reviewed pins:
# bump _debver, pkgver, _snapshot and the checksum together.
epoch=1
pkgver=1.10.0.13+ds.0+deb13u1
_debver=1:10.0.13+ds-0+deb13u1
_snapshot=02b553a4532192bc999d15408d1e56d7b254906f
pkgrel=1
pkgdesc="A generic and open source machine emulator and virtualizer - static user-mode emulation"
arch=('aarch64')
url="https://www.qemu.org"
license=('GPL-2.0-only' 'LGPL-2.1-only')
source=("qemu-binfmt-conf.sh")
# Debian removes superseded pool files. Pin the official archive snapshot so
# the same checksum-verified binary remains rebuildable. _snapshot is the
# content-addressed snapshot SHA1; SHA256 below remains the integrity check.
source_aarch64=("qemu-user_${pkgver}_arm64.deb::https://snapshot.debian.org/file/$_snapshot")
sha256sums=('5e12ef484b8364e1ba7bae4acda837d2e6320ffbeb7fab341034124553cdb137')
sha256sums_aarch64=('c73711af02b97cd5e2667e735d9c06890c57165517110ca4e646c95a7083158d')
noextract=("qemu-user_${pkgver}_arm64.deb")

prepare() {
  mkdir -p deb
  bsdtar -C deb -xf "qemu-user_${pkgver}_arm64.deb" data.tar.xz
  bsdtar -C deb -xf deb/data.tar.xz ./usr/bin ./usr/share/doc/qemu-user/copyright
}

package_qemu-user-static() {
  pkgdesc="A generic and open source machine emulator and virtualizer - static user-mode emulation"
  depends=('glibc')
  optdepends=('qemu-user-static-binfmt: binary format rules for foreign-architecture binaries')
  provides=('qemu-user-static')
  conflicts=('qemu-user-static-bin')

  install -dm755 "$pkgdir/usr/bin"
  local binary
  for binary in deb/usr/bin/qemu-*; do
    install -Dm755 "$binary" "$pkgdir/usr/bin/$(basename "$binary")-static"
  done
  install -Dm644 deb/usr/share/doc/qemu-user/copyright "$pkgdir/usr/share/licenses/$pkgname/copyright"
}

package_qemu-user-static-binfmt() {
  pkgdesc="Binary format rules for the static QEMU user-mode emulators"
  depends=('qemu-user-static')
  provides=('qemu-user-binfmt-provider')
  conflicts=('qemu-user-binfmt-provider' 'binfmt-qemu-static')

  install -dm755 "$pkgdir/usr/lib/binfmt.d"
  # HOST_ARCH keeps the native architecture out of the rule set regardless
  # of the build host, so the package is identical under native and QEMU builds.
  # --ignore-family keeps 32-bit ARM: the script groups it with aarch64, but
  # Apple Silicon cannot execute AArch32 natively.
  HOST_ARCH=aarch64 sh "$srcdir/qemu-binfmt-conf.sh" \
    --systemd ALL \
    --exportdir "$pkgdir/usr/lib/binfmt.d/" \
    --qemu-path /usr/bin \
    --qemu-suffix -static \
    --persistent yes \
    --preserve-argv0 yes \
    --ignore-family yes
  local conf
  for conf in "$pkgdir"/usr/lib/binfmt.d/*.conf; do
    mv "$conf" "${conf%.conf}-static.conf"
  done
}
