# Maintainer: Sébastien Luttringer
# Contributor: Balwinder S "bsd" Dheeman (bdheeman AT gmail.com)

pkgname=dkms
pkgver=2.8.6
pkgrel=2
pkgdesc='Dynamic Kernel Modules System'
arch=('any')
url='https://github.com/dell/dkms'
license=('GPL2')
depends=('bash' 'kmod' 'gcc' 'make' 'patch')
makedepends=('git')
optdepends=('linux-headers: build modules against the Arch kernel'
            'linux-lts-headers: build modules against the LTS kernel'
            'linux-zen-headers: build modules against the ZEN kernel'
            'linux-hardened-headers: build modules against the HARDENED kernel')
backup=('etc/dkms/framework.conf')
install=$pkgname.install
source=("git+https://github.com/dell/dkms.git#tag=v$pkgver"
        'hook.install'
        'hook.remove'
        'hook.upgrade'
        'hook.sh'
        '0001-Revert-Make-newly-installed-modules-available-immedi.patch')
sha256sums=('SKIP'
            '047e0e46967e98fbf3ff8860a80f259c28693d5989373d5a5395714495b59844'
            'ae5483980db914d52bff981ed83356c85679dd7e6e8f73bdd94265f065014c73'
            'd61d94532789928085a8494ba039c59244cfb3bfd47deaf21bcbe54a2243dc45'
            '55209947f6266dc7f29b82f3d2635a6b92f6e175d59571dca4d2fd435dffab4a'
            '124b821b1b44ba365d915945825bcf32c1077655c3a98eb1644de56b6abfcd09')

prepare() {
  cd dkms

  # apply patch from the source array (should be a pacman feature)
  local filename
  for filename in "${source[@]}"; do
    if [[ "$filename" =~ \.patch$ ]]; then
      msg2 "Applying patch ${filename##*/}"
      patch -p1 -N -i "$srcdir/${filename##*/}"
    fi
  done

  # /usr move
  msg2 '/usr move patching'
  for i in dkms{,_framework.conf,.bash-completion,.8,_common.postinst}; do
    sed -ri 's,/lib/modules,/usr/lib/modules,g' "$i"
  done
}

package() {
  # alpm hooks
  install -D -m 644 hook.install "$pkgdir/usr/share/libalpm/hooks/70-dkms-install.hook"
  # both upgrade and remove hooks are removing dkms modules.
  # remove always call depmod while upgrade never, as install will later.
  # upgrade is run before remove in order to have the cleanest depmod file,
  # if something goes wrong before the depmod call in install hook.
  install -D -m 644 hook.upgrade "$pkgdir/usr/share/libalpm/hooks/70-dkms-upgrade.hook"
  install -D -m 644 hook.remove "$pkgdir/usr/share/libalpm/hooks/71-dkms-remove.hook"
  # hook helper
  install -D -m 755 hook.sh "$pkgdir/usr/lib/dkms/alpm-hook"
  # upstream installer
  cd dkms
  # we don't need kconf files, so we install them outside $pkgdir
  make \
    DESTDIR="$pkgdir" \
    SBIN="$pkgdir/usr/bin" \
    BASHDIR="$pkgdir/usr/share/bash-completion/completions" \
    KCONF="$srcdir"/kconf \
    install
}

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