#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-or-later
set -u
export TEXTDOMAIN=ximper-plasma-desktop-failure
export TEXTDOMAINDIR=${TEXTDOMAINDIR:-/usr/share/locale}
unit=plasma-plasmashell.service

tr_text() { gettext "$1"; }
log() { printf 'ximper-plasma-desktop-failure: %s\n' "$*" >&2; }
error_dialog() {
    kdialog --title "$(tr_text 'Could not start the desktop')" \
        --error "$(tr_text "$1")" || log 'Could not display the error dialog.'
}

case "${1:-dialog}" in
dialog)
    while true; do
        kdialog --title "$(tr_text 'Could not start the desktop')" \
            --yes-label "$(tr_text 'Open terminal')" \
            --no-label "$(tr_text 'Try again')" \
            --cancel-label "$(tr_text 'Close')" \
            --warningyesnocancel "$(tr_text '<p>The KDE Plasma desktop could not start after several attempts.</p><p>Try starting it again. If the problem persists, check whether it also occurs with a new user account.</p><p>We also have an <a href="https://t.me/ximperlinux">Ximper Linux chat</a>.</p>')"
        result=$?
        case "$result" in
        0)
            if ! systemd-run --user --collect --service-type=exec \
                --property=ExitType=cgroup \
                --property=PartOf=graphical-session.target \
                --property=Requisite=graphical-session.target \
                --property=After=graphical-session.target \
                /usr/libexec/ximper-plasma-desktop-failure terminal; then
                log 'Could not start the terminal service.'
                error_dialog 'Could not open a terminal.'
            fi
            ;;
        1)
            if systemctl --user reset-failed "$unit" &&
                systemctl --user --no-block start "$unit"; then
                exit 0
            fi
            log 'Could not request another desktop startup attempt.'
            error_dialog 'Could not request another desktop startup attempt.'
            ;;
        2|255) exit 0 ;;
        *) log "KDialog failed with status $result."; exit "$result" ;;
        esac
    done
    ;;
terminal)
    # This process belongs to its own service, not to the dialog's cgroup.
    if command -v xdg-terminal-exec >/dev/null 2>&1; then
        if xdg-terminal-exec /usr/libexec/ximper-plasma-desktop-failure diagnostics; then
            exit 0
        fi
        log 'xdg-terminal-exec failed; trying Konsole.'
    fi
    if konsole --separate -e /usr/libexec/ximper-plasma-desktop-failure diagnostics; then
        exit 0
    fi
    log 'Could not open a terminal with Konsole.'
    error_dialog 'Could not open a terminal.'
    exit 1
    ;;
diagnostics)
    systemctl --user status --no-pager --full "$unit" || log 'The desktop unit is not active, or status could not be read.'
    journalctl --user -b -u "$unit" -n 100 --no-pager || log 'Could not read the desktop journal.'
    user_shell=${SHELL:-/bin/bash}
    if [[ "$user_shell" != /* || ! -x "$user_shell" ]]; then
        user_shell=/bin/bash
    fi
    exec "$user_shell" -i
    ;;
*) log "Unknown action: $1"; exit 64 ;;
esac
