# m1n1 for linux-aurora. Aurora's device trees name the USB4 aliases
# usb4-N-acio, usb4-N-nhi and usb4-N-pcie-adapter; m1n1 v1.6.1 only looks up
# the usb4_N_* names, so without the alias fallback the USB4 tunables are
# never applied. The Asahi m1n1 stays with linux-asahi.
pkgname=m1n1-aurora
pkgver=1.6.1.aurora1
pkgrel=3
pkgdesc='Omarchy Aurora m1n1 with compatible USB4 tunable aliases'
arch=('aarch64')
url='https://github.com/aurora-silicon/m1n1'
license=('MIT' 'CC0-1.0' 'OFL-1.1-RFN' 'Zlib' 'BSD-2-Clause OR GPL-2.0-or-later' 'BSD-3-Clause OR GPL-2.0-or-later' 'MIT OR Apache-2.0')
provides=("m1n1=$pkgver")
conflicts=('m1n1')
makedepends=('clang' 'lld' 'llvm' 'rustup' 'make' 'patch')
options=('!strip' '!debug')
_commit=06a4601a351ebfd1abb6abba9a44c34e40d94776
source=("m1n1-${_commit}.tar.gz::https://github.com/aurora-silicon/m1n1/archive/${_commit}.tar.gz"
        aurora-cio-aliases.patch
        bootlogo_48.bin
        bootlogo_128.bin
        bootlogo_256.bin
)
sha256sums=('9d6972d3edaff856e985f2b1feb64fcf9b42fc5eecda7be64a242b49bbbe636d'
            '4345a4e431642c4316595bdf023bc67d5c72e0374f3c8d59be81318710138f32'
            '6668050653645711ad7523fe2abeb4cbe85a92c875f3eec754b6db23cea2191c'
            'b19cb017645c7a9068ea0be9b6bb394131d7ca11b28093cf6caa1ffca74b0a4e'
            '9c659b392aacfa31c62c638003d2257abfdbe17919b5c90cde4c4e62f87addab')

prepare() {
  cd "m1n1-$_commit"
  patch -p1 < "$srcdir/aurora-cio-aliases.patch"
  for size in 48 128 256; do
    cp "$srcdir/bootlogo_$size.bin" "data/bootlogo_$size.bin"
  done
  rustup toolchain install 1.89.0 --profile minimal --target aarch64-unknown-none-softfloat
}

build() {
  cd "m1n1-$_commit"
  RUSTUP_TOOLCHAIN=1.89.0 make RELEASE=1 USE_CLANG=1 ARCH=aarch64-linux-gnu \
    M1N1_VERSION_TAG=v1.6.1-omarchy.aurora1
}

package() {
  # asahi-scripts (update-m1n1 and its pacman hook) only exists in the
  # asahi-alarm repository, which the builder does not configure. Declared
  # here so makepkg does not try to install it at build time.
  depends=('asahi-scripts>=20260127.1')

  cd "m1n1-$_commit"
  install -Dm644 build/m1n1.bin "$pkgdir/usr/lib/asahi-boot/m1n1.bin"
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  install -m644 3rdparty_licenses/* "$pkgdir/usr/share/licenses/$pkgname/"
}
