# Maintainer: David P. <megver83@parabola.nu>

# Based on linux-rt-lts package

_replacesarchkernel=('linux%') # '%' gets replaced with kernel suffix
_replacesoldkernels=() # '%' gets replaced with kernel suffix
_replacesoldmodules=() # '%' gets replaced with kernel suffix

pkgbase=linux-libre-rt-lts
pkgver=6.18.58.rt9.parabola1
pkgrel=1
pkgdesc='Linux-libre RT LTS'
url='https://linux-libre.fsfla.org/'
arch=(i686 x86_64 armv7h)
makedepends=(
  bc
  binutils
  cpio
  gettext
  glibc
  graphviz  # docs
  imagemagick  # docs
  libelf
  libgcc
  openssl
  pahole
  perl
  python
  python-sphinx  # docs
  python-yaml  # docs
  #rust
  #rust-bindgen
  #rust-src
  tar
  texlive-latexextra  # docs
  xxhash
  xz
  zlib
  zstd
)
makedepends_x86_64=(
  rust
  rust-bindgen
  rust-src
)
options=(
  !debug
  !strip
)
# Extract the version parts from pkgver and assemble them as needed.
IFS='.' read -r -a _version_parts <<< "$pkgver"
_major="${_version_parts[0]}"
_minor="${_version_parts[1]}"
# Depending on whether patch level is 0, assemble the upstream version to use.
if (( _version_parts[2] == 0 )); then
  _upstream_ver="${_version_parts[0]}.${_version_parts[1]}"
else
  _upstream_ver="${_version_parts[0]}.${_version_parts[1]}.${_version_parts[2]}"
fi
# Based on upstream version, assemble the RT patch version.
_rt_patch_ver="${_upstream_ver}-${_version_parts[3]}"
# Assemble the distribution patch version.
_distro_patch_ver="${_version_parts[0]}.${_version_parts[1]}.${_version_parts[2]}-${_version_parts[3]}-${_version_parts[4]/parabola/arch}"
# Add a convenience abstraction for the source directory.
_srcname=linux-6.18
source=(
  https://linux-libre.fsfla.org/pub/linux-libre/releases/${_srcname##*-}-gnu/linux-libre-${_srcname##*-}-gnu.tar.xz{,.sign}
  https://linux-libre.fsfla.org/pub/linux-libre/releases/${_upstream_ver}-gnu/patch-${_srcname##*-}-gnu-${_upstream_ver}-gnu.xz{,.sign}
  https://repo.parabola.nu/other/linux-libre/logos/logo_linux_{clut224.ppm,vga16.ppm,mono.pbm}{,.sig}
  https://gitlab.archlinux.org/archlinux/packaging/upstream/linux-rt-lts/-/releases/v${_distro_patch_ver}/downloads/linux-rt-lts-v${_rt_patch_ver}.patch.{sig,zst}
  https://gitlab.archlinux.org/archlinux/packaging/upstream/linux-rt-lts/-/releases/v${_distro_patch_ver}/downloads/linux-rt-lts-v${_distro_patch_ver}.patch.{sig,zst}

  # maintain the TTY over USB disconnects
  # http://www.coreboot.org/EHCI_Gadget_Debug
  0001-usb-serial-gadget-no-TTY-hangup-on-USB-disconnect-WI.patch
  # fix Atmel maXTouch touchscreen support
  # https://labs.parabola.nu/issues/877
  # http://www.fsfla.org/pipermail/linux-libre/2015-November/003202.html
  0002-fix-Atmel-maXTouch-touchscreen-support.patch
)
source_x86_64=(config.x86_64)
source_i686=(
  config.i686
  # avoid using zstd compression in ultra mode (exhausts virtual memory)
  no-ultra-zstd.patch
)
source_armv7h=(
  config.armv7h
  # armv7h preset file for mkinitcpio ramdisk
  linux-armv7h.preset
)
validpgpkeys=(
  474402C8C582DAFBE389C427BCB7CF877E7D47A7  # Alexandre Oliva
  6DB9C4B4F0D8C0DC432CF6E4227CA7C556B2BA78  # David P.
  D5653EA39C8675DA4BD5971C13B55DD07C53B851  # Clark Williams (Kernel Correspondence) <clrkwllms@kernel.org>
  991F6E3F0765CF6295888586139B09DA5BF0D338  # David Runge <dvzrv@archlinux.org>
  05C7775A9E8B977407FE08E69D4C5AA15426DA0A  # Frederik Schwan <freswa@archlinux.org>
)
sha256sums=('4b38d5ba30c8cb0a99cc0380e61092b626d273305d6ef866286cecfec9cd8593'
            'SKIP'
            '3b3c723e8eb4c3b710c8e9a8a239943fd9c08f61586859b79640015cde8aa8d5'
            'SKIP'
            'bfd4a7f61febe63c880534dcb7c31c5b932dde6acf991810b41a939a93535494'
            'SKIP'
            '6de8a8319271809ffdb072b68d53d155eef12438e6d04ff06a5a4db82c34fa8a'
            'SKIP'
            '13bd7a8d9ed6b6bc971e4cd162262c5a20448a83796af39ce394d827b0e5de74'
            'SKIP'
            'SKIP'
            'ebba3cacb6802e040fa1d6921194e19d4af8d85c0bcfb4fe78a518a2987417f5'
            'SKIP'
            '68a1372dbf37d95389962b1ae8da0aac8c4b4b912020a1f76b1c3be18afe0b82'
            '0376bd5efa31d4e2a9d52558777cebd9f0941df8e1adab916c868bf0c05f2fc3'
            '351fd96be8cd5ebd0435c0a8a978673fc023e3b1026085e67f86d815b2285e25')
sha256sums_i686=('a32aced796cb1cff60f70403bbdc91c8444fd8d35d3d71a98d34a6be47852290'
                 'd32270be5fd9c3e3ba50f3aef33f6cfcb85be0c8216f03b777287cc621fdff28')
sha256sums_x86_64=('5f5965a6aa7c1c53c52a35a4173352e15697021c815476cad8fa8ad9be47a86f')
sha256sums_armv7h=('21b488f9e72b0208c072c158439c7f31db946374254f1f9dba3f64e1dd2e6cef'
                   'ca1708abbd2f8960c278722dc81a7c635973d92872386ad9e2fff10b86836e98')
b2sums=('9df3f1a1e89206e484b1322d87172fd755663900d4ec8986e254a0863fb041b3144ce214b304b4640cf0d608f0514a611bedc6fc92bd5c01904a5b48d6baed3d'
        'SKIP'
        '98f52c01e90f8f7ab32d183e730d909fb054738bed74494843a5a26a7428c415b65c3f2d00267494d5589da4e70854c00a2c4252b0b776a536f1fca50a4f67ff'
        'SKIP'
        '73fee2ae5cb1ffd3e6584e56da86a8b1ff6c713aae54d77c0dab113890fc673dc5f300eb9ed93fb367b045ece8fa80304ff277fe61665eccf7b7ce24f0c045eb'
        'SKIP'
        'd02a1153a4285b32c774dca4560fe37907ccf30b8e487a681b717ed95ae9bed5988875c0a118938e5885ae9d2857e53a6f216b732b6fa3368e3c5fe08c86382c'
        'SKIP'
        '580911af9431c066bbc072fd22d5e2ef65f12d8358cec5ff5a4f1b7deebb86cef6b5c1ad631f42350af72c51d44d2093c71f761234fb224a8b9dbb3b64b8201d'
        'SKIP'
        'SKIP'
        'a0ffd4bead299d422f352622f2de938f46991a85fd5d72c27d066fd7f3d38e93d625f77b0afe578b977bbef5d8419e7a80a3c75b06d1999715801807ead6523b'
        'SKIP'
        '5db5f2040a5ebda158d7989dd655145d7ac150a267334a7763c0e5f6b0dc2b4bfaa1bdd287c0d19c06410022f19fbd97586b1f4376d7dce6cf643563c5d3bb55'
        'c2214154c36900e311531bfe68184f31639f5c50fed23bc3803a7f18439b7ff258552a39f02fed0ea92f10744e17a6c55cef0ef1a98187f978fe480fb3dddc14'
        '0c7ceba7cd90087db3296610a07886f337910bad265a32c052d3a703e6eb8e53f355ab9948d72d366408d968d8ee7435084dd89bef5ed0b69355fd884c2cd468')
b2sums_i686=('60cae81af001a9605485e530f91a300df3026ac2019d43589c348d53210286bbc9b7e45f883b01435ead508387f14a7c576775dff8ada20c21723ec25345c777'
             '165ab9dd8cedeaae5327accc1581c19cf0be55f923b03feb889cad3351b74c7c4cd3d3c206938e5152bfe1d947513dea8f630f8f5544099ec13d16d254725c40')
b2sums_x86_64=('1f29c5c530762e0f910ea31715d5aa6b685844ecfd148dfa9c7b98e4f225ebf849b5ba29209c0cce5d7084ba1ffd8d4f33b5f4d344058e6a38854d6772ce93be')
b2sums_armv7h=('7f5847c32a04645683f5580351e46b9dfa92f30fca880d13edaf7776c5b24499a746c1d0cf9dbe705e1294146d437d2bb1892ea93e7f4d79f3044a64de44495b'
               'af69176b1117b94e56b043e97b0bd5873a2974a6a2fd52b102d0ffdca440ff68cfb241d6c4d4ef453cc8c220c236b739bad232e53fd500ce7672fa6e5ba87383')

_replacesarchkernel=("${_replacesarchkernel[@]/\%/${pkgbase#linux-libre}}")
_replacesoldkernels=("${_replacesoldkernels[@]/\%/${pkgbase#linux-libre}}")
_replacesoldmodules=("${_replacesoldmodules[@]/\%/${pkgbase#linux-libre}}")

export KBUILD_BUILD_HOST=parabola
export KBUILD_BUILD_USER=$pkgbase
export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"

prepare() {
  cd $_srcname

  if [ "${_srcname##*-}" != "${_upstream_ver}" ]; then
    echo "Applying upstream patch..."
    patch -Np1 < "../patch-${_srcname##*-}-gnu-${_upstream_ver}-gnu"
  fi

  echo "Adding freedo as boot logo..."
  install -m644 -t drivers/video/logo \
    ../logo_linux_{clut224.ppm,vga16.ppm,mono.pbm}

  echo "Setting version..."
  echo "-$pkgrel" > localversion.10-pkgrel
  echo "${pkgbase#linux-libre}" > localversion.20-pkgname

  # save extraversion as localversion to fit Arch patch while keeping the original value
  awk '/^EXTRAVERSION/ {print $3}' Makefile > localversion.00-extraversion
  sed -i '/^EXTRAVERSION[[:space:]]*=/ s/=.*$/=/' Makefile

  local source=("${source[@]}")
  eval "source+=(\${source_${CARCH}[@]})"

  local src
  for src in "${source[@]}"; do
    src="${src%%::*}"
    src="${src##*/}"
    src="${src%.zst}"
    [[ $src = *.patch ]] || continue
    echo "Applying patch $src..."
    patch -Np1 < "../$src"
  done

  # save extraversion as localversion after patching for correct string concatenation order
  awk '/^EXTRAVERSION/ {gsub(/arch/, "parabola", $3); print $3}' Makefile > localversion.01-extraversion
  sed -i '/^EXTRAVERSION[[:space:]]*=/ s/=.*$/=/' Makefile

  echo "Setting config..."
  cp ../config.$CARCH .config
  make olddefconfig
  diff -u ../config.$CARCH .config || :

  make -s kernelrelease > version
  echo "Prepared $pkgbase version $(<version)"

  if ! [[ $(scripts/config -s CONFIG_DEBUG_INFO_BTF) = y ]]; then
    echo "CONFIG_DEBUG_INFO_BTF is required to build vmlinux.h"
    exit 1
  fi
}

build() {
  cd $_srcname

  make htmldocs SPHINXOPTS=-QT &
  local pid_docs=$!

  make all
  make -C tools/bpf/bpftool vmlinux.h feature-clang-bpf-co-re=1
  wait $pid_docs
}

_package() {
  pkgdesc="The $pkgdesc kernel and modules"
  license=(
    'Apache-2.0 OR MIT'
    'BSD-2-Clause OR GPL-2.0-or-later'
    BSD-3-Clause
    'BSD-3-Clause OR GPL-2.0-only'
    'BSD-3-Clause OR GPL-2.0-or-later'
    BSD-3-Clause-Clear
    GPL-1.0-or-later
    'GPL-1.0-or-later OR BSD-3-Clause'
    GPL-2.0-only
    'GPL-2.0-only OR Apache-2.0'
    'GPL-2.0-only OR BSD-2-Clause'
    'GPL-2.0-only OR BSD-3-Clause'
    'GPL-2.0-only OR CDDL-1.0'
    'GPL-2.0-only OR Linux-OpenIB'
    'GPL-2.0-only OR MIT'
    'GPL-2.0-only OR MPL-1.1'
    'GPL-2.0-only OR X11'
    'GPL-2.0-only WITH Linux-syscall-note'
    GPL-2.0-or-later
    'GPL-2.0-or-later OR BSD-2-Clause'
    'GPL-2.0-or-later OR BSD-3-Clause'
    'GPL-2.0-or-later OR MIT'
    'GPL-2.0-or-later OR X11'
    'GPL-2.0-or-later WITH GCC-exception-2.0'
    ISC
    LGPL-2.0-or-later
    'LGPL-2.1-only'
    'LGPL-2.1-only OR BSD-2-Clause'
    LGPL-2.1-or-later
    MIT
    MPL-1.1
    X11
    Zlib
  )
  depends=(
    coreutils
    initramfs
    kmod
  )
  optdepends=(
    "$pkgbase-headers: headers and scripts for building modules"
    'linux-libre-firmware: firmware images needed for some devices'
    #'scx-scheds: to use sched-ext schedulers'
    'wireless-regdb: to set the correct wireless channels of your country'
  )
  optdepends_x86_64=(
    'scx-scheds: to use sched-ext schedulers' # only available for x86_64
  )
  provides=(
    KSMBD-MODULE
    VIRTUALBOX-GUEST-MODULES
    WIREGUARD-MODULE
  )
  provides+=(
    "${_replacesarchkernel[@]/%/=$pkgver}"
    "LINUX-ABI_VERSION=$pkgver"
  )
  conflicts+=(
    "${_replacesarchkernel[@]}"
    "${_replacesoldkernels[@]}"
    "${_replacesoldmodules[@]}"
  )
  replaces+=(
    "${_replacesarchkernel[@]}"
    "${_replacesoldkernels[@]}"
    "${_replacesoldmodules[@]}"
  )

  cd $_srcname
  local modulesdir="$pkgdir/usr/lib/modules/$(<version)"

  echo "Installing boot image..."
  # systemd expects to find the kernel here to allow hibernation
  # https://github.com/systemd/systemd/commit/edda44605f06a41fb86b7ab8128dcf99161d2344
  install -Dm644 "$(make -s image_name)" "$modulesdir/vmlinuz"

  # Used by mkinitcpio to name the kernel
  echo "$pkgbase" | install -Dm644 /dev/stdin "$modulesdir/pkgbase"

  echo "Installing modules..."
  ZSTD_CLEVEL=19 make INSTALL_MOD_PATH="$pkgdir/usr" INSTALL_MOD_STRIP=1 \
    DEPMOD=/doesnt/exist modules_install  # Suppress depmod

  # remove build link
  rm "$modulesdir"/build

  # licenses
  install -vDm 644 LICENSES/deprecated/{GPL-1.0,ISC,Linux-OpenIB,X11,Zlib} -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 LICENSES/preferred/{BSD,MIT}* -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 LICENSES/exceptions/* -t "$pkgdir/usr/share/licenses/$pkgname/"

  if [ "$CARCH" = "armv7h" ]; then
    echo "Installing dtbs..."
    make INSTALL_DTBS_PATH="$pkgdir/boot/dtbs/$pkgbase" dtbs_install

    # sed expression for following substitutions
    local _subst="
      s|%PKGBASE%|${pkgbase}|g
      s|%KERNVER%|$(<version)|g
    "

    # install mkinitcpio preset file
    sed "${_subst}" ../linux-armv7h.preset |
      install -Dm644 /dev/stdin "${pkgdir}/etc/mkinitcpio.d/${pkgbase}.preset"
  fi
}

_package-headers() {
  pkgdesc="Headers and scripts for building modules for the $pkgdesc kernel"
  license=(
    BSD-3-Clause
    'BSD-3-Clause OR GPL-2.0-only'
    GPL-1.0-or-later
    'GPL-1.0-or-later WITH Linux-syscall-note'
    GPL-2.0-only
    'GPL-2.0-only OR Apache-2.0'
    'GPL-2.0-only OR BSD-2-Clause'
    'GPL-2.0-only OR BSD-3-Clause'
    'GPL-2.0-only OR CDDL-1.0'
    'GPL-2.0-only OR Linux-OpenIB'
    'GPL-2.0-only OR Linux-OpenIB OR BSD-2-Clause'
    'GPL-2.0-only OR MIT'
    'GPL-2.0-only OR MPL-1.1'
    'GPL-2.0-only OR X11'
    'GPL-2.0-only WITH Linux-syscall-note'
    '(GPL-2.0-only WITH Linux-syscall-note) AND MIT'
    '(GPL-2.0-only WITH Linux-syscall-note) OR BSD-2-Clause'
    '(GPL-2.0-only WITH Linux-syscall-note) OR BSD-3-Clause'
    '(GPL-2.0-only WITH Linux-syscall-note) OR CDDL-1.0'
    '(GPL-2.0-only WITH Linux-syscall-note) OR Linux-OpenIB'
    '(GPL-2.0-only WITH Linux-syscall-note) OR MIT'
    GPL-2.0-or-later
    'GPL-2.0-or-later OR BSD-2-Clause'
    'GPL-2.0-or-later OR BSD-3-Clause'
    'GPL-2.0-or-later OR MIT'
    'GPL-2.0-or-later WITH Linux-syscall-note'
    '(GPL-2.0-or-later WITH Linux-syscall-note) OR BSD-3-Clause'
    '(GPL-2.0-or-later WITH Linux-syscall-note) OR MIT'
    'LGPL-2.0-or-later OR BSD-2-Clause'
    'LGPL-2.0-or-later WITH Linux-syscall-note'
    ISC
    'LGPL-2.0-or-later WITH Linux-syscall-note'
    'LGPL-2.0-or-later OR BSD-2-Clause'
    LGPL-2.1-only
    'LGPL-2.1-only OR BSD-2-Clause'
    'LGPL-2.1-only OR MIT'
    'LGPL-2.1-only WITH Linux-syscall-note'
    LGPL-2.1-or-later
    'LGPL-2.1-or-later OR BSD-2-Clause'
    'LGPL-2.1-or-later WITH Linux-syscall-note'
    MIT
    Zlib
  )
  depends=(
    binutils
    glibc
    libelf
    libgcc
    openssl
    pahole
    xxhash
    zlib
    zstd
  )
  provides=(LINUX-HEADERS)
  provides+=(
    "${_replacesarchkernel[@]/%/-headers=$pkgver}"
  )
  conflicts+=(
    "${_replacesarchkernel[@]/%/-headers}"
    "${_replacesoldkernels[@]/%/-headers}"
  )
  replaces+=(
    "${_replacesarchkernel[@]/%/-headers}"
    "${_replacesoldkernels[@]/%/-headers}"
  )

  cd $_srcname
  local builddir="$pkgdir/usr/lib/modules/$(<version)/build"

  local karch
  case "$CARCH" in
    i686|x86_64) karch=x86;;
    armv7h) karch=arm;;
    *) echo "Unknown CARCH $CARCH"; exit 1 ;;
  esac

  echo "Installing build files..."
  install -Dt "$builddir" -m644 .config Makefile Module.symvers System.map \
    localversion.* version vmlinux tools/bpf/bpftool/vmlinux.h
  install -Dt "$builddir/kernel" -m644 kernel/Makefile
  install -Dt "$builddir/arch/$karch" -m644 arch/$karch/Makefile
  [ "$CARCH" != i686 ] || install -Dt "$builddir/arch/x86" -m644 arch/x86/Makefile_32.cpu
  cp -t "$builddir" -a scripts

  if [[ $(scripts/config -s CONFIG_HAVE_STACK_VALIDATION) = y ]]; then
    install -Dt "$builddir/tools/objtool" tools/objtool/objtool
  fi

  if [[ $(scripts/config -s CONFIG_DEBUG_INFO_BTF_MODULES) = y ]]; then
    install -Dt "$builddir/tools/bpf/resolve_btfids" tools/bpf/resolve_btfids/resolve_btfids
  fi

  echo "Installing headers..."
  cp -t "$builddir" -a include
  cp -t "$builddir/arch/$karch" -a arch/$karch/include
  install -Dt "$builddir/arch/$karch/kernel" -m644 arch/$karch/kernel/asm-offsets.s

  install -Dt "$builddir/drivers/md" -m644 drivers/md/*.h
  install -Dt "$builddir/net/mac80211" -m644 net/mac80211/*.h

  # https://bugs.archlinux.org/task/13146
  install -Dt "$builddir/drivers/media/i2c" -m644 drivers/media/i2c/msp3400-driver.h

  # https://bugs.archlinux.org/task/20402
  install -Dt "$builddir/drivers/media/usb/dvb-usb" -m644 drivers/media/usb/dvb-usb/*.h
  install -Dt "$builddir/drivers/media/dvb-frontends" -m644 drivers/media/dvb-frontends/*.h
  install -Dt "$builddir/drivers/media/tuners" -m644 drivers/media/tuners/*.h

  # https://bugs.archlinux.org/task/71392
  install -Dt "$builddir/drivers/iio/common/hid-sensors" -m644 drivers/iio/common/hid-sensors/*.h

  echo "Installing KConfig files..."
  find . -name 'Kconfig*' -exec install -Dm644 {} "$builddir/{}" \;

  if [[ $(scripts/config -s CONFIG_RUST) = y ]]; then
    echo "Installing Rust files..."
    install -Dt "$builddir/rust" -m644 rust/*.rmeta
    install -Dt "$builddir/rust" rust/*.so
  fi

  echo "Installing unstripped VDSO..."
  make INSTALL_MOD_PATH="$pkgdir/usr" vdso_install \
    link=  # Suppress build-id symlinks

  echo "Removing unneeded architectures..."
  local arch
  for arch in "$builddir"/arch/*/; do
    [[ $arch = */$karch/ ]] && continue
    echo "Removing $(basename "$arch")"
    rm -r "$arch"
  done

  echo "Removing documentation..."
  rm -r "$builddir/Documentation"

  echo "Removing broken symlinks..."
  find -L "$builddir" -type l -printf 'Removing %P\n' -delete

  echo "Removing loose objects..."
  find "$builddir" -type f -name '*.o' -printf 'Removing %P\n' -delete

  echo "Stripping build tools..."
  local file
  while read -rd '' file; do
    case "$(file -Sib "$file")" in
      application/x-sharedlib\;*)      # Libraries (.so)
        strip -v $STRIP_SHARED "$file" ;;
      application/x-archive\;*)        # Libraries (.a)
        strip -v $STRIP_STATIC "$file" ;;
      application/x-executable\;*)     # Binaries
        strip -v $STRIP_BINARIES "$file" ;;
      application/x-pie-executable\;*) # Relocatable binaries
        strip -v $STRIP_SHARED "$file" ;;
    esac
  done < <(find "$builddir" -type f -perm -u+x ! -name vmlinux -print0)

  echo "Stripping vmlinux..."
  strip -v $STRIP_STATIC "$builddir/vmlinux"

  echo "Adding symlink..."
  mkdir -p "$pkgdir/usr/src"
  ln -sr "$builddir" "$pkgdir/usr/src/$pkgbase"

  # licenses
  install -vDm 644 LICENSES/deprecated/{ISC,Linux-OpenIB,X11,Zlib} -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 LICENSES/preferred/{BSD*,MIT} -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 LICENSES/exceptions/* -t "$pkgdir/usr/share/licenses/$pkgname/"
}

_package-docs() {
  pkgdesc="Documentation for the $pkgdesc kernel"
  license=(
    BSD-3-Clause
    GFDL-1.1-no-invariants-or-later
    GPL-2.0-only
    'GPL-2.0-only OR BSD-2-Clause'
    'GPL-2.0-only OR BSD-3-Clause'
    'GPL-2.0-only OR GFDL-1.1-no-invariants-or-later'
    'GPL-2.0-only OR GFDL-1.2-no-invariants-only'
    'GPL-2.0-only OR MIT'
    GPL-2.0-or-later
    'GPL-2.0-or-later OR BSD-2-Clause'
    'GPL-2.0-or-later OR CC-BY-4.0'
    'GPL-2.0-or-later OR MIT'
    'GPL-2.0-or-later OR X11'
    'LGPL-2.1-only OR BSD-2-Clause'
    MIT
  )
  provides=(
    "${_replacesarchkernel[@]/%/-docs=$pkgver}"
  )
  conflicts=(
    "${_replacesarchkernel[@]/%/-docs}"
    "${_replacesoldkernels[@]/%/-docs}"
  )
  replaces=(
    "${_replacesarchkernel[@]/%/-docs}"
    "${_replacesoldkernels[@]/%/-docs}"
  )

  cd $_srcname
  local builddir="$pkgdir/usr/lib/modules/$(<version)/build"

  echo "Installing documentation..."
  local src dst
  while read -rd '' src; do
    dst="${src#Documentation/}"
    dst="$builddir/Documentation/${dst#output/}"
    install -Dm644 "$src" "$dst"
  done < <(
    find Documentation \( -name '.*' -o -name __pycache__ \) -prune \
      -o \! -type d -print0
  )

  echo "Adding symlink..."
  mkdir -p "$pkgdir/usr/share/doc"
  ln -sr "$builddir/Documentation" "$pkgdir/usr/share/doc/$pkgbase"

  # licenses
  install -vDm 644 LICENSES/deprecated/X11 -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 LICENSES/preferred/{BSD*,MIT} -t "$pkgdir/usr/share/licenses/$pkgname/"
}

pkgname=(
  "$pkgbase"
  "$pkgbase-headers"
  "$pkgbase-docs"
)
for _p in "${pkgname[@]}"; do
  eval "package_$_p() {
    $(declare -f "_package${_p#$pkgbase}")
    _package${_p#$pkgbase}
  }"
done

# vim:set ts=8 sts=2 sw=2 et:
