#!/bin/bash
# Bring back the desktop-mode taskbar/system tray if plasmashell has crashed.
# In SteamOS's nested desktop session plasmashell isn't a systemd unit, so it
# must be relaunched with the session's environment (taken from ksmserver).
# Translations (lib/i18n.sh; the copy install_i18n puts here), else English
# shellcheck source=/dev/null
source "$HOME/.local/share/frame-tweaks/i18n/i18n.sh" 2>/dev/null ||
    t() { local f=$1; shift; if [ $# -eq 0 ]; then printf '%s' "$f"; else printf -- "$f" "$@"; fi; }
# Stop a running one first, and wait until it's gone: a new one quits at once while the old one
# still holds its D-Bus name. It handles SIGTERM in its event loop, so a frozen one never acts on
# it: after 5 s it's killed.
if pkill -u "$(id -u)" -x plasmashell; then
    for _ in 1 2 3 4 5; do sleep 1; pgrep -u "$(id -u)" -x plasmashell >/dev/null || break; done
    pkill -KILL -u "$(id -u)" -x plasmashell && sleep 1
fi
src=$(pgrep -u "$(id -u)" -x ksmserver | head -1)
[ -n "$src" ] || { t 'No Plasma session (ksmserver) found — are you in desktop mode?' >&2; echo >&2; exit 1; }
mapfile -d '' envs < "/proc/$src/environ"
cd "$HOME" && env -i "${envs[@]}" setsid -f plasmashell >"/run/user/$(id -u)/plasmashell.log" 2>&1 </dev/null
sleep 2; pgrep -x plasmashell >/dev/null && { t '%s restarted' plasmashell; echo; } || { t '%s failed; see %s' plasmashell "/run/user/$(id -u)/plasmashell.log" >&2; echo >&2; exit 1; }
