# Maintainer: Developer <dev@opalos.org>
# Maintainer: Developer <dev@opalos.org>

pkgbase=audit
_name=audit-userspace
pkgname=(
  audit
  python-audit
)
pkgver=%version%
pkgrel=%release%
pkgdesc='Userspace components of the audit framework'
url='https://people.redhat.com/sgrubb/audit'
arch=(x86_64)
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
)
sha512sums=('297664a55ab44b40c9280202c19612cfbfdacc209c4d226461ea5faa638e35617cb516e53d1f0bc3748cdd038d9524f3e5ebe11c8de4a5511ab4f12b7d06478c'
            '1750741755f58d0ae19ed2c30e136d05560dc12ec613a502bad12f47c6b70432d30b3a16f3f1574c8433ad2701428d1c1d567a4d3b55be19abac300310c831d9'
            '78d0289c5ea37bf28fecf43db7d83f1f754f2ac58a70667c8d720b0e5be51afca35f86861ba1abdea4833a4be8fbcfc05801240a2493904ab8ea4d2be5ca32e7')
b2sums=('3fc22fdfa98cddb9325cc68389d013886b579a75339aa43f812017612c9b0f02d27bab76b92324b6c419ea917a5cd3235e91a1ffcb0925d41cb7268d622d58fa'
        '549ebbbc9e43277d44d0dc5bfd8ca2926628322d898479171b2707dd004968d036ef792b442548af90ad56dea868a72c88b5cf3bb93ea70cb8bbed82747ad9b5'
        '4cfeb5d47218e77d693d2645de49c9867abe10bf185b1315d526d0410e93a8c5967cdff107eefbefcd4760c6ccbdd010bd47e2ef1cb8b3970f86910f1ba50e3f')

_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

  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:
