#!/bin/sh
set -e
FGCONSOLE=$(fgconsole)
strip_quotes() {
i="$1"
i=${i%\"}
i=${i#\"}
echo "${i}"
}
get_property() {
session="$1"
property="$2"
# the output is "type value" - we only support single outputs here, not
# arrays or anything
val=$(busctl --no-pager \
--no-legend \
--system \
get-property \
org.freedesktop.login1 \
"${session}" \
org.freedesktop.login1.Session \
"${property}" 2>/dev/null | cut -d" " -f 2 || true)
strip_quotes "${val}"
}
if [ -x /usr/bin/busctl ]; then
SESSIONS=$(busctl --no-pager \
--no-legend \
--system \
call \
org.freedesktop.login1 \
/org/freedesktop/login1 \
org.freedesktop.login1.Manager \
ListSessions 2>/dev/null || true)
if [ -n "${SESSIONS}" ]; then
# return type, number of sessions, rest (actual session information)
echo "${SESSIONS}" | while read -r _ n r; do
for i in $(seq 1 "${n}"); do
read -r _ _ _ _ session rest <<EOT
$(echo "${r}")
EOT
session=$(strip_quotes "${session}")
VT=$(get_property "${session}" VTNr)
if [ -n "${VT}" ] && [ "${VT}" -eq "${FGCONSOLE}" ]; then
type=$(get_property "${session}" Type)
if [ "${type}" = "tty" ]; then
echo "would restart"
fi
fi
r="${rest}"
done
done
fi
fi