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

# This package comes from Artix
# Parabola changes:
# - elogind and libelogind: do not provide {lib,}elogind=$pkgver, it's ridiculous
# - elogind: do not provide systemd-tools, openrc will conflict it
# - libelogind: make libsystemd.so a copy of libelogind.so instead of linking it,
#   so in that way it will provide the correct version according to the
#   architecture (libsystemd.so=0-64 or libsystemd.so=0-32)
# - remove prepare()

pkgbase=elogind
pkgname=('elogind' 'libelogind')
pkgver=243.7
pkgrel=2
pkgdesc="The systemd project's logind, extracted to a standalone package"
arch=('x86_64' 'i686' 'armv7h')
url="https://github.com/elogind/elogind"
license=('GPL' 'LGPL2.1')
conflicts=('systemd-libsystemd')
makedepends=('intltool' 'libtool' 'gperf' 'gtk-doc' 'polkit' 'dbus'
            'libseccomp' 'meson' 'kexec-tools' 'openrc')
options=('!libtool')
source=("${pkgbase}-${pkgver}.tar.gz::https://github.com/elogind/elogind/archive/v${pkgver}.tar.gz"
        'elogind.confd' 'elogind.initd')
sha256sums=('941fde1ffbdf51d61e47fcebc49e2fc2b1347fcf3b0522bfa9d65ad5da653e53'
            '344db76bf8aadfba4363c63aacd93c945a647bd5e425d756cbfd9cb4453afc66'
            '2fc375b3aa61cb63b16c55840acaa69d26b16a01d393ee129d77c804efdac278')

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

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

    ninja -C build
}

check(){
    meson test -C build --print-errorlogs
}

package_elogind() {
    pkgdesc="The systemd project's logind, extracted to a standalone package"
    provides=("systemd=${pkgver}")
    depends=('pam' 'dbus' 'libseccomp' 'libelogind' 'kexec-tools' 'udev')
    optdepends=('polkit: polkit support')
    backup=('etc/elogind/logind.conf')

    provides+=("${pkgname}-openrc")
    conflicts+=('systemd-udev' "${pkgname}-openrc")
    replaces=("${pkgname}-openrc")

    DESTDIR="$pkgdir" meson install -C build

    ln -sfv libelogind.pc ${pkgdir}/usr/lib/pkgconfig/libsystemd.pc

    install -dm755 ${srcdir}/_libelogind
    mv -v ${pkgdir}/usr/lib/libelogind*.so* ${srcdir}/_libelogind

    # Init scripts
    install -Dm755 ${srcdir}/elogind.initd ${pkgdir}/etc/init.d/elogind
    install -Dm755 ${srcdir}/elogind.confd ${pkgdir}/etc/conf.d/elogind

    install -d "${pkgdir}/etc/runlevels/boot"
    ln -sf "/etc/init.d/elogind" "${pkgdir}/etc/runlevels/boot/elogind"
}

package_libelogind(){
    pkgdesc="elogind client libraries"
    groups=('base-devel')
    provides=('libelogind.so'
             "libsystemd=${pkgver}" "systemd-libs=${pkgver}"
             'libsystemd.so')
    conflicts+=('libsystemd.so')
    depends=('libcap' 'libudev')

    cd ${pkgbase}-${pkgver}

    install -dm755 ${pkgdir}/usr/lib
    mv ${srcdir}/_libelogind/libelogind*.so* ${pkgdir}/usr/lib

    install -m755 ${pkgdir}/usr/lib/libelogind.so ${pkgdir}/usr/lib/libsystemd.so
    ln -sfv libsystemd.so ${pkgdir}/usr/lib/libsystemd.so.0
}
