#!/usr/bin/bash
#
# dmald - display manager abstraction layer daemon

set -o errexit
set -o pipefail
set -o nounset
set -o errtrace

function cleanup() {
    trap - ERR
    trap - EXIT

    [[ -z "${DMAL_RUN_DIR-}" ]] || rm -f "$DMAL_RUN_DIR/dmald.pid"
}

# shellcheck disable=SC2329,SC2317
function err_exit_trap() {
    local _status=$?
    local _traptype="$1"

    cleanup

    echo "${FUNCNAME[0]-?}: unexpected exit/error" \
         "near '$BASH_COMMAND', status=$_status, trap=$_traptype"
    exit 1
}

trap 'err_exit_trap "ERR"' ERR
trap 'err_exit_trap "EXIT"' EXIT

function fatal() {
    echo "FATAL ERROR: $*" >&2
    exit 1
}

function create_lightdm_autologin_config() {
    local _user

    _user=$(cat "$DMAL_AUTOLOGIN_USER_FILE") \
        || fatal "failed to read \$DMAL_AUTOLOGIN_USER_FILE" \
                 "($DMAL_AUTOLOGIN_USER_FILE)"
    cat <<EOF
# autologin config by dmal

[LightDM]
minimum-vt=1
lock-memory=true
user-authority-in-system-dir=true
sessions-directory=$DMAL_SESSIONS_DIR

[Seat:*]
xserver-command=X -background none
greeter-session=lightdm-autologin-greeter
greeter-hide-users=true
user-session=xfce
autologin-guest=false
autologin-user=$_user
autologin-user-timeout=0
# greeter-setup-script=/usr/local/sbin/lightdm-greeter-setup-hook
session-wrapper=/usr/libexec/dmal/dmal-run-session
session-setup-script=/usr/libexec/dmal/dmal-call-hooks setup
session-cleanup-script=/usr/libexec/dmal/dmal-call-hooks cleanup

[XDMCPServer]

[VNCServer]


EOF
}

function create_lightdm_config() {
    cat <<EOF
# config by dmal

[LightDM]
minimum-vt=1
lock-memory=true
user-authority-in-system-dir=true

# this is currently an .inf² default
sessions-directory=$DMAL_SESSIONS_DIR

[Seat:*]
xserver-command=X -background none
greeter-session=lightdm-autologin-greeter
greeter-hide-users=true
user-session=xfce
autologin-guest=false
# greeter-setup-script=/usr/local/sbin/lightdm-greeter-setup-hook
session-wrapper=/usr/libexec/dmal/dmal-run-session
session-setup-script=/usr/libexec/dmal/dmal-call-hooks setup
session-cleanup-script=/usr/libexec/dmal/dmal-call-hooks cleanup

[XDMCPServer]

[VNCServer]


EOF
}

if [ -z "${JOURNAL_STREAM-}" ]
then
    # we do not run as systemd service, so lets configure logging
    exec > \
         >(systemd-cat -t dmald -p info) \
         2> >(systemd-cat -t dmad -p err)
fi

# default values
DMAL_DM_BIN=/usr/sbin/lightdm
DMAL_HOOK_DIR=/etc/dmal/hooks
DMAL_RUN_DIR=/var/run/dmal
DMAL_DM_CONF_DIR="$DMAL_RUN_DIR/dm-conf"
DMAL_AUTOLOGIN_USER_FILE=
DMAL_EXIT_ON_ERROR=
DMAL_SESSIONS_DIR=

if [ -f /etc/default/dmal ]
then
    # shellcheck disable=SC1091
    source /etc/default/dmal || fatal "failed to source /etc/default/dmal"
else
    echo "WARNING: /etc/default/dmal not found, using hardcoded values"
fi

mkdir -p "$DMAL_RUN_DIR" || fatal "unable to mkdir $DMAL_RUN_DIR"
echo "$$" > "$DMAL_RUN_DIR/dmald.pid" \
    || fatal "unable to create $DMAL_RUN_DIR/dmald.pid"
mkdir -p "$DMAL_DM_CONF_DIR" || fatal "unable to mkdir $DMAL_DM_CONF_DIR"

DMAL_DM_CONF_FILE="$DMAL_DM_CONF_DIR/dmal-$$-lightdm.conf"

if [ -z "${DMAL_AUTOLOGIN_USER_FILE-}" ]
then
    create_lightdm_config > "$DMAL_DM_CONF_FILE" \
        || fatal "create_lightdm_config failed"
else
    create_lightdm_autologin_config > "$DMAL_DM_CONF_FILE" \
        || fatal "create_lightdm_autologin_config failed"
fi

[ -z "${DISPLAY-}" ] || {
    echo "found a DISPLAY variable (DISPLAY=$DISPLAY) - removing it"
    unset DISPLAY
}

#UNIT_NAME="dmal-lightdm-manual-run"

#systemctl stop "$UNIT_NAME" || true
#systemctl reset-failed "$UNIT_NAME" || true

# shellcheck disable=SC2064
# trap "systemctl stop $UNIT_NAME; systemctl reset-failed $UNIT_NAME" INT TERM

#systemd-run --unit="$UNIT_NAME" --wait --property=Type=simple lightdm \
#            --config="$DMAL_DM_CONF_DIR/dmal-$$-lightdm.conf" --debug \
#    || fatal "lightdm --config=\"$DMAL_DM_CONF_FILE\" failed"

lightdm --config="$DMAL_DM_CONF_DIR/dmal-$$-lightdm.conf" --debug &
# shellcheck disable=SC2064
trap "kill $!" INT TERM

wait || echo "lightdm failed ($?)" >&2

cleanup

exit 0
