# Maintainer: Ryan Hughes <ryan@omarchy.org>
# Contributor: Bart De Vries <bart at mogwai dot be>
# Contributor: Dan Johansen <strit@manjaro.org>
#
# Runs x86_64-only Linux programs (dropbox) on AArch64. Pinned past v0.4.5-1:
# that release crashes in its glib wrapper as soon as Dropbox starts its tray
# icon. Move to a release tag once one includes the pinned commit.

pkgname=box64
pkgver=0.4.5.1.r309.gd58d619
pkgrel=1
_commit=d58d619e84e03282326e8a26c2cbfe749931b5f8
pkgdesc='Linux userspace x86_64 emulator with native library wrapping'
arch=('aarch64')
url='https://github.com/ptitSeb/box64'
license=('MIT')
depends=('gcc-libs' 'glibc')
makedepends=('cmake' 'python')
backup=('etc/box64.box64rc')
options=('!strip' '!emptydirs')
source=("$pkgname-$_commit.tar.gz::$url/archive/$_commit.tar.gz")
sha256sums=('f7615a3c4ac5412a983a3fd26be79311e6e2702884d8bdfde954b23bad39fff8')

build() {
  # ARM64 is box64's generic AArch64 profile, so the binary does not depend
  # on the CPU of the machine that built it.
  cmake -S "$pkgname-$_commit" -B build \
    -DARM64=ON \
    -DARM_DYNAREC=ON \
    -DNOGIT=ON \
    -DCMAKE_BUILD_TYPE=RelWithDebInfo \
    -DCMAKE_INSTALL_PREFIX=/usr
  cmake --build build --parallel
}

package() {
  DESTDIR="$pkgdir" cmake --install build

  # Packages call box64 explicitly. A binfmt handler would compete with
  # qemu-user-static-binfmt for every x86_64 executable on the system.
  rm -r "$pkgdir/etc/binfmt.d"
  # The Qt rcfile editor needs PySide, and its launcher entry would show up
  # on every system that installs box64 only as a dependency.
  rm "$pkgdir/usr/bin/box64-configurator" \
    "$pkgdir/usr/share/applications/box64-configurator.desktop"

  install -Dm644 "$pkgname-$_commit/LICENSE" -t "$pkgdir/usr/share/licenses/$pkgname"
}
