# Maintainer (Artix): artoo <artoo@cromnix.org>
# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>
# Maintainer: David P. <megver83@parabola.nu>
# Contributor: Luke Shumaker <lukeshu@parabola.nu>

# Note: have libelogind-manpages as a separate package, so that
# systemd-libsystemd (which also includes the same manpages) and
# libelogind don't conflict.

pkgbase=elogind
pkgname=('elogind' 'libelogind' 'libelogind-manpages')
pkgver=238.1.0
_commit='8bb4695ef79404b6731b20dee58115c313444754'
pkgrel=2.1
pkgdesc="The systemd project's logind, extracted to a standalone package"
arch=('i686' 'x86_64' 'armv7h')
url="https://github.com/elogind/elogind"
license=('GPL2' 'LGPL2.1') # Mostly LGPL2.1, only the single `src/uaccess-command/uaccess-command.c` file is GPL2
makedepends=('acl' 'docbook-xsl' 'gperf' 'pam'
             'libcap' 'polkit' 'dbus' 'udev' 'meson' 'git')
validpgpkeys=('0C4DF110D49D911C593050BF5FC1DF9CEC74BA48') # Sven Eden <seden@havi.de>
source=(# fragment is latest tag for source verification, final merge in prepare()
        "git+https://github.com/elogind/elogind#tag=v${pkgver%.*}?signed"
        'elogind-struct-statx-is-defined.patch')
sha256sums=('SKIP'
            '3d6a1b1590bee1e673db1384f570596a8604e2c68adf0a9c1dc81ed67c3943bb')

prepare() {
  cd "$pkgbase"

  # merge the latest stable commit (fast-foward only to make sure
  # the verified tag is in)
  git merge --ff-only "${_commit}"

  patch -Np1 -i ../elogind-struct-statx-is-defined.patch
}

pkgver() {
  local version count

  cd "$pkgbase"

  version="$(git describe --abbrev=0 --tags)"
  count="$(git rev-list --count ${version}..)"
  printf '%s.%s' "${version#v}" "${count}"
}

build() {
  local meson_options=(
    -Drootlibdir=/usr/lib
    -Drootlibexecdir=/usr/lib/elogind
    -Ddbuspolicydir=/usr/share/dbus-1/system.d
    -Ddefault-hierarchy=hybrid
    -Dcgroup-controller=openrc
  )

  arch-meson "$pkgbase" build "${meson_options[@]}"

  ninja -C build

  # Go ahead and split the package now.  It's easier this way, because
  # we can use mv instead of awkward, error-prone rm/cp pairs.
  rm -rf "$srcdir/dest"

  # Put things in the main systemd package by default
  DESTDIR="$srcdir/dest/elogind" ninja -C build install

  install -d "$srcdir/dest/libelogind"/usr/lib
  mv -T "$srcdir/dest"/{elogind,libelogind}/usr/lib/pkgconfig
  mv -T "$srcdir/dest"/{elogind,libelogind}/usr/include
  mv -t "$srcdir/dest/libelogind"/usr/lib -- "$srcdir/dest/elogind"/usr/lib/libelogind*

  install -d "$srcdir/dest"/libelogind-manpages/usr/share/man
  mv -T "$srcdir/dest"/{elogind,libelogind-manpages}/usr/share/man/man3
}

package_elogind() {
  depends=('acl' 'pam' 'dbus' 'udev' 'libelogind')
  conflicts=('systemd-tools') # because both provide org.freedesktop.login1, say 'systemd-tools' instead of 'systemd' to avoid hitting systemd-dummy
  optdepends=('elogind-openrc: elogind initscript'
              'polkit-elogind: polkit support')
  backup=('etc/elogind/logind.conf')

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"
}

package_libelogind(){
  pkgdesc="elogind client libraries"
  provides=('libelogind.so')
  optdepends=('libelogind-manpages')

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"
}

package_libelogind-manpages() {
  pkgdesc='Manual pages for libelogind'
  depends=('libelogind')
  conflicts=('systemd-libsystemd') # because both provide the same manpages

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"
}
