# 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
# - 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=246.10
pkgrel=2
pkgdesc="The systemd project's logind, extracted to a standalone package"
arch=('x86_64')
arch+=('i686' 'armv7h')
url="https://github.com/elogind/elogind"
license=('GPL' 'LGPL2.1')
makedepends=('acl' 'intltool' 'libtool' 'gperf' 'gtk-doc' 'polkit' 'dbus'
            'libseccomp' 'meson' 'kexec-tools' 'openrc' 'libcap')
options=('!libtool')
source=("${pkgbase}-${pkgver}.tar.gz::https://github.com/elogind/elogind/archive/v${pkgver}.tar.gz"
        #'elogind.confd' 'elogind.initd'
        'elogind.sv.initd')
sha256sums=('c490dc158c8f5bca8d00ecfcc7ad5af24d1c7b9e59990a0b3b1323996221a922'
            'b4097e85f81f37d8529eb57563813e94b7c7afb9931d892907d805093187761b')

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
        -Dman=true
    )

    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}" 'logind')
    depends=('acl' '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

    install -d "${pkgdir}"/{etc,usr/lib}/elogind/{logind,sleep}.conf.d

    # Init script
    #install -Dm755 "${srcdir}"/elogind.initd "${pkgdir}"/etc/init.d/elogind
    #install -Dm755 "${srcdir}"/elogind.confd "${pkgdir}"/etc/conf.d/elogind
    install -Dm755 "${srcdir}"/elogind.sv.initd "${pkgdir}"/etc/init.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"
    provides=('libelogind.so' 'liblogind')
    provides+=("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
}
