# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Christian Rebischke <Chris.Rebischke@archlinux.org>
# Contributor: Daniel Micay <danielmicay@gmail.com>
# Contributor: <kang@insecure.ws>
# Contributor: Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
# Contributor: Connor Behan <connor.behan@gmail.com>
# Contributor: henning mueller <henning@orgizm.net>

pkgbase=audit
_name=audit-userspace
pkgname=(
  audit
  python-audit
)
pkgver=4.0.2
pkgrel=3
pkgdesc='Userspace components of the audit framework'
url='https://people.redhat.com/sgrubb/audit'
arch=(x86_64 aarch64)
license=(
  GPL-2.0-or-later
  LGPL-2.0-or-later
)
makedepends=(
  apparmor
  glibc
  krb5
  libcap-ng
  libldap
  linux-api-headers
  python
  swig
)
options=(emptydirs)
source=(
  https://github.com/linux-audit/$_name/archive/v$pkgver/$_name-v$pkgver.tar.gz
  $pkgbase.tmpfiles
  $pkgbase-4.0-executable_paths.patch
  $pkgbase-4.0.2-flagtab_h.patch::https://github.com/linux-audit/audit-userspace/commit/6b9b74acba4db9edbab8595474d2323eb7eef51f.patch
)
sha512sums=('558b9211a5dc1062eee98aa7bcd292797f06109a8ee735da1d704bc18d97b0bee93487ef9303404016df2e08cff32d90f1dd056797ac05beaabe3cccb5db5af2'
            '1750741755f58d0ae19ed2c30e136d05560dc12ec613a502bad12f47c6b70432d30b3a16f3f1574c8433ad2701428d1c1d567a4d3b55be19abac300310c831d9'
            '78d0289c5ea37bf28fecf43db7d83f1f754f2ac58a70667c8d720b0e5be51afca35f86861ba1abdea4833a4be8fbcfc05801240a2493904ab8ea4d2be5ca32e7'
            '7446b2a5c59dd51dfc15a938da4346cdf4c66e7f2ef69aa0510a0b2ed1a59856e1c1e44b40cbd5acc0b47f53f56bf79a807ff7effde2f4f43e6b25ae6467aee4')
b2sums=('27bcc020a41b13f1f0c176ebf513a756a159d0cb6368e2ece261ac507da063f513146cc82e3aa166771a3c0287ec9de01f6da72cf5fd688148b5f226c27822d1'
        '549ebbbc9e43277d44d0dc5bfd8ca2926628322d898479171b2707dd004968d036ef792b442548af90ad56dea868a72c88b5cf3bb93ea70cb8bbed82747ad9b5'
        '4cfeb5d47218e77d693d2645de49c9867abe10bf185b1315d526d0410e93a8c5967cdff107eefbefcd4760c6ccbdd010bd47e2ef1cb8b3970f86910f1ba50e3f'
        '180305d05867aa481428204b67e3297d376387241b3d365862af49e525d41c2bd8f580144424a262b2751c2dd4d67da5e9012415b1f9b7d8346f4d20f25de6ed')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  # use /usr and /bin merge compatible paths in configs and services
  patch -Np1 -d $pkgbase-userspace-$pkgver -i ../$pkgbase-4.0-executable_paths.patch

  # fix weird issue with autotools and an unneeded header file: https://github.com/linux-audit/audit-userspace/issues/390
  patch -Np1 -d $pkgbase-userspace-$pkgver -i ../$pkgbase-4.0.2-flagtab_h.patch

  cd $_name-$pkgver
  autoreconf -fiv
}

build() {
  local configure_options=(
    --enable-gssapi-krb5=yes
    --enable-systemd=yes
    --enable-zos-remote
    --libexecdir=/usr/lib/audit
    --prefix=/usr
    --runstatedir=/run
    --sbindir=/usr/bin
    --sysconfdir=/etc
    --with-apparmor=yes
    --with-io_uring=yes
    --with-libcap-ng=yes
    --with-python3=yes
  )

  cd $_name-$pkgver
  ./configure "${configure_options[@]}"
  # prevent excessive overlinking due to libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

package_audit() {
  depends=(
    glibc
    krb5 libkrb5.so libgssapi_krb5.so
    libcap-ng libcap-ng.so
  )
  optdepends=(
    'libldap: for audispd-zos-remote'
    'sh: for augenrules'
  )
  provides=(
    libaudit.so
    libauparse.so
  )
  backup=(
    etc/libaudit.conf
    etc/audit/audit-stop.rules
    etc/audit/auditd.conf
    etc/audit/audisp-remote.conf
    etc/audit/zos-remote.conf
    etc/audit/plugins.d/af_unix.conf
    etc/audit/plugins.d/au-remote.conf
    etc/audit/plugins.d/audispd-zos-remote.conf
    etc/audit/plugins.d/syslog.conf
  )

  make DESTDIR="$pkgdir" install -C $_name-$pkgver

  install -vDm 644 $_name-$pkgver/{{README,SECURITY}.md,ChangeLog} -t "$pkgdir/usr/share/doc/$pkgname/"

  # add rules.d dir to satisfy augenrules
  install -vdm 755 "$pkgdir/etc/audit/rules.d/"
  # add config dir for audisp
  install -vdm 755 "$pkgdir/etc/audisp"

  # add factory files
  install -vdm 755 "$pkgdir/usr/share/factory/"
  cp -av "$pkgdir/etc" "$pkgdir/usr/share/factory/"

  # add tmpfiles.d integration for factory files and file permissions
  install -vDm 644 $pkgbase.tmpfiles "$pkgdir/usr/lib/tmpfiles.d/$pkgbase.conf"

  # remove legacy files
  rm -frv "$pkgdir/usr/lib/audit"

  (
    cd "$pkgdir"
    _pick python-audit usr/lib/python*
  )
}

package_python-audit() {
  pkgdesc+=' - Python bindings'
  depends=(
    audit libaudit.so libauparse.so
    glibc
    python
  )

  mv -v $pkgname/* "$pkgdir"
}

# vim: ts=2 sw=2 et:
