#!/bin/sh
#260311 sfs
#[ "`whoami`" != "root" ] && { pkexec ${0} "${@}" ; exit ; } || { export XDG_RUNTIME_DIR=/run/user/$(loginctl list-sessions --no-legend | awk '$4 ~ /seat[0-9]+/ && ! /root/ {print $2}' | sort -u) ; PATH="$PATH":/usr/local/bin ; }
[ "$(id -u)" -ne 0 ] && { pkexec env XDG_RUNTIME_DIR="$XDG_RUNTIME_DIR" PATH="$PATH" ${0} "${@}" ; exit $?; }
PKE(){
if [ "$(id -u)" -ne 0 ]; then
    #exec \
    pkexec env \
        DISPLAY="$DISPLAY" \
        WAYLAND_DISPLAY="$WAYLAND_DISPLAY" \
        XDG_RUNTIME_DIR="$XDG_RUNTIME_DIR" \
        DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" \
        PATH="$PATH" \
        "$0" "$@" ; exit $?
fi
}
#getent group systemd-journal | grep -qw "$USER" || PKE

TEXTDOMAIN=`basename $0`
TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN TEXTDOMAINDIR

CONF="/etc/systemd/journald.conf"
OVERRIDE_DIR="/etc/systemd/journald.conf.d"
CONF2="$OVERRIDE_DIR/nolog.conf"

STATUS=$(grep "^Storage=" "$CONF2" 2>/dev/null)
[ -z "$STATUS" ] && STATUS=$(grep "^Storage=" "$CONF" 2>/dev/null)
[ -z "$STATUS" ] && STATUS="Storage=auto"
STATUS="$(echo "$STATUS" | sed 's/Storage=//')"

DISK_USAGE=$(journalctl --disk-usage | grep -o '[0-9.]\+[BKMG]')

ACT=$(yad  --image=gtk-file --window-icon=gtk-file --name=gtk-file --fixed --image-on-top \
    --form --center --separator="" \
    --title="$(gettext "Journald Manager")" \
    --text="$(gettext "Archived and active journals take up") $DISK_USAGE\n\n$(gettext "Journald current mode:")" \
    --button="$(gettext "View journal")"!gtk-file!"journalctl -f -n 100 -o short":4 \
    --field="<b>Storage=</b>":CB \
    --field="$(gettext "none		- journaling disabled")":LBL \
    --field="$(gettext "volatile		- journal stored in RAM /run/log/journal")":LBL \
    --field="$(gettext "persistent	- journal stored on disk /var/log/journal")":LBL \
    --field="$(gettext "auto		- default mode!if /var/log/journal exists → persistent, otherwise → volatile")":LBL \
    --button="yad-ok":2 \
    --button="yad-quit":3 \
    $STATUS!none!auto!persistent!volatile
)
ACTION="$?"

live_journal() {
    mode="${1:-full}"

    cmdF="journalctl -f -n 100 -o short"
    cmdB="journalctl -f -b -n 100 -o short"
    cmdE="journalctl -f -p err -n 100 -o short"

    case "$mode" in
        full)
            title="$(gettext "Journal — Full")"
            cmd="$cmdF"
            ;;
        boot)
            title="$(gettext "Journal — Current boot")"
            cmd="$cmdB"
            ;;
        err)
            title="$(gettext "Journal — Errors only")"
            cmd="$cmdE"
            ;;
        unit)
            unit=$(yad --entry \
                --window-icon=gtk-find --name=gtk-find \
                --title="$(gettext "Unit name")" \
                --text="$(gettext "Enter systemd unit (example: sshd.service)")" \
                --width=400)
#"
            [ -z "$unit" ] && return

            title="$(gettext "Journal — %s" | sed "s/%s/$unit/")"
            cmd="journalctl -f -u $unit -n 100 -o short"
            ;;
        *)
            return
            ;;
    esac

    sh -c "$cmd" | yad --text-info \
        --window-icon=gtk-file --name=gtk-file --fontname="Mono 9" \
        --tail \
        --title="$title" \
        --width=900 --height=600 \
        --button="$(gettext "Full")!gtk-select-all!$cmdF:10" \
        --button="$(gettext "Current boot")!gtk-media-play!$cmdB:11" \
        --button="$(gettext "Errors")!gtk-dialog-error!$cmdE:12" \
        --button="$(gettext "Unit")!gtk-find!$cmdU:13" \
        --button=yad-quit:1

    case "$?" in
        10) live_journal full ;;
        11) live_journal boot ;;
        12) live_journal err ;;
        13) live_journal unit ;;
        *) return ;;
    esac
}

case "$ACTION" in
    2) pkexec no-journald.sh  "$ACT" ;;
    4)
#        xterm -e "journalctl -f" &
	live_journal &
        sleep 0.5
        ;;
    *) exit ;;
esac

$0 &
