# SPDX-License-Identifier: GPL-2.0-only
# Builds the working tree of the checkout this file lives in, uncommitted
# changes included. snapshot.sh publishes that tree; the git source is pinned
# to it. An unpinned source checks out the mirror's origin/HEAD, which stays
# on the branch the mirror was first cloned from.
#   cd packaging/arch && makepkg -si
# For a published release, point the first source at the tagged tarball.
pkgbase=gpu-unlock
pkgname=(gpu-unlock-dkms gpu-unlock-firmware)
pkgver=2.0.0
pkgrel=1
_nv=610.43.03
_installer=NVIDIA-Linux-x86_64-$_nv-no-compat32.run
pkgdesc='Ampere pre-driver hardware unlock'
arch=('x86_64')
url=''
license=('GPL-2.0-only')
makedepends=('git' 'python' 'zstd' 'openssl')
options=('!strip' '!debug')
_root=${startdir%/packaging/arch}
_commit=$("$_root/packaging/snapshot.sh")
source=("$pkgbase::git+file://${_root}#commit=${_commit}"
        "https://download.nvidia.com/XFree86/NVIDIA-kernel-module-source/NVIDIA-kernel-module-source-$_nv.tar.xz"
        "https://download.nvidia.com/XFree86/Linux-x86_64/$_nv/$_installer")
sha256sums=('SKIP'
            '7e118923c7a23edc36114d63273a46e3e04e9af98695a42203e7ac2dfe9fc1dc'
            'e084451db730a6df29d3d74f655749c4d236e3c945f7d94b5205e84318fe2f90')

prepare() {
  cd $pkgbase
  got=$(cat VERSION)
  [ "$got" = "$pkgver" ] || {
    printf 'VERSION is %s, pkgver is %s (checked out %s)\n' \
      "$got" "$pkgver" "$(git rev-parse --short HEAD)" >&2
    return 1
  }
}

build() {
  cd $pkgbase
  python firmware/extract_gsp.py "$(realpath "$srcdir/$_installer")" "$srcdir/assets"
  python firmware/extract_boot_assets.py "$srcdir/NVIDIA-kernel-module-source-$_nv" "$srcdir/assets"
}

package_gpu-unlock-dkms() {
  arch=('any')
  depends=('dkms' "gpu-unlock-firmware=$pkgver-$pkgrel")
  backup=('etc/mkinitcpio.conf.d/zz-gpu-unlock.conf')
  conflicts=('gpu-unlock-mkinitcpio')
  replaces=('gpu-unlock-mkinitcpio')
  install=gpu-unlock.install
  cd $pkgbase
  make DESTDIR="$pkgdir" install-dkms install-mkinitcpio install-dracut
  make DESTDIR="$pkgdir" PKG=$pkgname install-licenses
}

package_gpu-unlock-firmware() {
  pkgdesc='Locally generated, pinned NVIDIA 610.43.03 firmware assets for gpu-unlock'
  license=('LicenseRef-NVIDIA' 'GPL-2.0-only')
  cd $pkgbase
  make DESTDIR="$pkgdir" ASSETS="$srcdir/assets" install-firmware
}
