#!/bin/sh
#260309 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 ; }
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" "$@"
fi
}
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.]\+[KMG]')

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")":LBL \
    --field="$(gettext "persistent	- journal stored on disk")":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="$?"

case "$ACTION" in
    2) pkexec no-journald.sh  "$ACT" ;;
    4)
#        xterm -e "journalctl -f" &
J1(){
    journalctl -f -n 100 -o short | \
    yad \
  --window-icon=gtk-file --name=gtk-file --fontname="Mono 9" \
  --text-info \
  --tail \
  --wrap \
  --title="Journal (`whoami`)" \
  --width=900 \
  --height=600 \
  --button=yad-quit \ &
}

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

    case "$mode" in
        full)
            title="Live journal — Full"
            cmd="journalctl -f -n 100 -o short"
            ;;
        boot)
            title="Live journal — Current boot"
            cmd="journalctl -f -b -n 100 -o short"
            ;;
        err)
            title="Live journal — Errors only"
            cmd="journalctl -f -p err -n 100 -o short"
            ;;
        unit)
            unit=$(yad --entry \
                --title="Unit name" \
                --text="Enter systemd unit (example: sshd.service)" \
                --width=400)

            [ -z "$unit" ] && return

            title="Live journal — $unit"
            cmd="journalctl -f -u $unit -n 100 -o short"
            ;;
        *)
            return
            ;;
    esac

    sh -c "$cmd" | yad --text-info \
        --tail \
        --title="$title" \
        --width=900 --height=600 \
        --button="Full:10" \
        --button="Current boot:11" \
        --button="Errors:12" \
        --button="Unit:13" \
        --button="Close:1"

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

$0 &
