#!/usr/bin/openrc-run
# Copyright 2021 Artix Linux
# Distributed under the terms of the GNU General Public License v2

depend() {
    need dbus
    provide logind
    before xdm
}

start() {
    ebegin "Starting elogind"

    if [ -s "${ELOGIND_PIDFILE}" ]; then
        pid="$(cat "${ELOGIND_PIDFILE}" 2>/dev/null || true)"
        if [ -n "${pid}" ] && kill -0 "${pid}" 2>/dev/null; then
            eend 0
            return 0
        fi
    fi

    start-stop-daemon --start --quiet \
        --make-pidfile \
        --pidfile "${ELOGIND_PIDFILE}" \
        --exec "${ELOGIND_EXEC}" -- --daemon
    eend $?
}

stop() {
    ebegin "Stopping elogind"
    start-stop-daemon --stop --quiet --pidfile "${ELOGIND_PIDFILE}" || true
    eend 0
}
