#!/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=audit
_name=audit-userspace
pkgname=('audit' 'python-audit')
pkgver=4.0
pkgrel=01
pkgdesc="Userspace components of the audit framework w/o systemd"
url='https://people.redhat.com/sgrubb/audit'
makedepends=(apparmor glibc krb5 libcap-ng libldap linux-api-headers python swig)
options=(emptydirs)
#options=(emptydirs debug)  # comment above options uncomment this to have debug packages produced
source=(	https://github.com/linux-audit/$_name/archive/v$pkgver/$_name-v$pkgver.tar.gz
	$pkgbase.tmpfiles)
#	$pkgbase-4.0-executable_paths.patch)  # systemd patch ... irrelevant

_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 systemd services
#  patch -Np1 -d $pkgbase-userspace-$pkgver -i ../$pkgbase-4.0-executable_paths.patch

  cd $_name-$pkgver
  autoreconf -fiv
}

build() {
  local configure_options=(
    --prefix=/usr
    --sbindir=/usr/bin
    --sysconfdir=/etc
    --enable-gssapi-krb5=yes
    --enable-systemd=no
    --enable-zos-remote
    --libexecdir=/usr/lib/audit
    --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

  # add log dir
  install -vdm 755 "$pkgdir/var/log/$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"

  # remove systemd crap that is still pushed by upstream even when you configure disable system
  rm -frv  $pkgdir/usr/lib/systemd
  (
    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"
}

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

arch=(x86_64)

license=(GPL-2.0-or-later LGPL-2.0-or-later)

sha256sums=(992a6c6c7f300327dc70e863f7ed282f26fd488780834c28aa11bfba52403b12  # audit-userspace-v4.0.tar.gz
	0ed3ff18f966f57e62d181705cad44ec0873a50d151dad46ca18a82edaa5589c) # audit.tmpfiles

##  c3220bbad0d3ba7716a372307a1f9393d12dd09d616ea9fb5fa84c4943d63426  audit-4.0-01-x86_64.pkg.tar.lz
##  eb9d15562cea0f1d78a2e15bbfff7da75443c26c49ed946b27b0fe49650a9f67  python-audit-4.0-01-x86_64.pkg.tar.lz
