#!/usr/bin/bash
# JOBoRun		: Jwm OpenBox Obarun RUNit
# Maintainer	: Joe Bo Run <joborun@disroot.org>
# PkgSource		: url="https://gittea.disroot.org/joborun-pkg/jobcore/$pkgname"
# Website		: https://pozol.eu
#-----------------------------------------| DESCRIPTION |---------------------------------------

pkgbase=linux
pkgver=5.15.134
pkgrel=01
pkgdesc='Linux LTS 5.15 w/o zstd ipv6 + vfio i915 patch'
url="https://www.kernel.org/"
makedepends=(bc kmod libelf pahole cpio perl tar xz 
  gettext python-sphinx python-sphinx_rtd_theme python-six graphviz imagemagick texlive-latexextra) ## for building docs
options=('!strip')
_srcname=linux-$pkgver
url="https://cdn.kernel.org/pub/linux/kernel"
source=($url/v5.x/$_srcname.tar.{xz,sign}
  config         # the main kernel config file
  0001-ZEN-Add-sysctl-and-CONFIG-to-disallow-unprivileged-C.patch
  0002-PCI-Add-more-NVIDIA-controllers-to-the-MSI-masking-q.patch
  0003-iommu-intel-do-deep-dma-unmapping-to-avoid-kernel-fl.patch
  0004-Bluetooth-btintel-Fix-bdaddress-comparison-with-garb.patch
  0005-lg-laptop-Recognize-more-models.patch
  i915-vga-arbiter.patch
  sha256sums::https://cdn.kernel.org/pub/linux/kernel/v5.x/sha256sums.asc)

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

#_make() {
#  test -s version
#  make KERNELRELEASE="$(<version)" "$@"
#}

prepare() {
  cd $_srcname

  echo "Setting version... $pkgver"
  scripts/setlocalversion --save-scmversion
  echo "-$pkgrel" > localversion.10-pkgrel
  echo "${pkgbase#linux}" > localversion.20-pkgname
#  make defconfig
#  make -s kernelrelease > version
#  make mrproper

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

  echo "Setting joborun config..."
  cp ../config .config
  make olddefconfig
#  diff -u ../config .config || :

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

build() {
  cd $_srcname
  make htmldocs SPHINXOPTS='-T'
}
_package-docs() {
  pkgdesc="Documentation for the $pkgdesc kernel"

  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 '.*' -prune -o ! -type d -print0)

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

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

#---- arch license gpg-key & sha256sums ----

arch=(x86_64)

license=(GPL2)

validpgpkeys=(ABAF11C65A2970B130ABE3C479BE3E4300411886 # Linus Torvalds
	647F28654894E3BD457199BE38DBBDC86092693E)   # Greg Kroah-Hartman

# https://cdn.kernel.org/pub/linux/kernel/v5.x/sha256sums.asc  --> sha256sums
# sha256sums:f37182aecb57ed6853d01e1074d3a60a653331e35f3115728381e08be050b9d3  linux-5.15.134.tar.xz

sha256sums=(f37182aecb57ed6853d01e1074d3a60a653331e35f3115728381e08be050b9d3  # linux-5.15.134.tar.xz
	24b245ecafa96a61f8f37cdf2159ae29523b193662b658c66ea0d94cf20b6842  # linux-5.15.134.tar.sign
	617ff2f2bc3f790dcb5ecbb4da958f2ee442256005469ad7185694ad36d33dfa  # config
	3b5cfc9ca9cf778ea2c4b619b933cda26519969df2d764b5a687f63cf59974cd  # 0001-ZEN-Add-sysctl-and-CONFIG-to-disallow-unprivileged-C.patch
	c175fbb141c3cec013c799f694d88310375ac5456042f6a4a1adc7667836d786  # 0002-PCI-Add-more-NVIDIA-controllers-to-the-MSI-masking-q.patch
	8357f000b2b622e73dcfd41c2bad42b5e99fffe8f7ee64f774aa771f86cef43c  # 0003-iommu-intel-do-deep-dma-unmapping-to-avoid-kernel-fl.patch
	5c1ee81fdd5818442af6081de987f9c1a9ce3c8d183566b3dfc19a8433aa3dde  # 0004-Bluetooth-btintel-Fix-bdaddress-comparison-with-garb.patch
	067e8995fcd6f6ed25e0253e9374c0e179a000c154da3e59ce62634945ac5be9  # 0005-lg-laptop-Recognize-more-models.patch
	856230cfbdc2bb53a4920dfbcb6fb2d58427b7b184e5f94e21f08011d0a2fcc6  # i915-vga-arbiter.patch
	436279faa9bef342b4a455288f79449cd450812e77b33154dc9a5a6a2287932a) # sha256sums


## 16e452a65bf7542a6f5c73cad64623e993b29e012aad837c73c97a35ad0a0099  linux-docs-5.15.134-01-x86_64.pkg.tar.lz
