#!/bin/bash
#260224 sfs
export TEXTDOMAIN=systemctl-gui
if [ "$1" = "--user" ] ;then
    u="--user" && shift 
    rb=(--button="system services"!gtk-dialog-warning:4)
else 
    [ "`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 ; }
fi

yad --version |grep -E '^0' && export gtk=gtk || export gtk=yad
NTF(){
#    [ "$1" = "-i" ] && { notify-send -a mmod -i $2 "`printf "$3\n$4"`" ; return ; }
    i="--image=gtk-info --button=$gtk-ok:0 --back=red" && col=green
    [ "$1" = "-q" ] && i="--image=gtk-dialog-question --button=$gtk-yes:0 --button=$gtk-no" && col=black
    [ "$1" = "-a" ] && i="--image=gtk-dialog-error --button=$gtk-ok:0" && col=red
    tit="`echo $2 | sed 's/<[^>]*>//g'`"
    yad --width=600 --form --fixed --name=gtk-info  \
    --image-on-top $i  --mouse \
    --window-icon=gtk-info --title="$tit" \
    --text "<span color=\"$col\"><b>$2</b></span>\n<i>$3</i>"  >/dev/null && echo $? 
}
export -f NTF
[ "$2" ] && msg="\n<span size=\"small\">$2</span>" || msg="\n`systemctl show -p Description --value $1`"
if [ "$1" ] ;then
    srv="$1"
else
    srvs="`systemctl $u list-unit-files --type=service --no-pager --no-legend | awk '!/@/ {print $1}' |tr "\n" " "`"
    srv=$(yad --mouse --list --title="`whoami`: Systemd $u services" \
    --width=600 --height=600 --no-headers --search-column=1 \
    --Xfixed --image-on-top \
    --text="Systemd $u services:" "${rb[@]}" \
    --button=$(gettext "journalctl")!terminal!"xterm -e \"journalctl -f\"":3 \
    --button=yad-cancel \
    --button=yad-ok \
    --image=gtk-media-play --window-icon=gtk-media-play --name=gtk-media-play \
    --column= \
    $srvs \
	 )
ret="$?"
srv="`echo "$srv" |sed 's/|//' `"
#echo $ret $srv;exit
[ $ret -eq 4 ]  && exec $0 && exec $0 &&  exit 0
if [ $ret -eq 3 ] ;then
    xterm -e "sudo journalctl -f" &
    $0 $u &
    exit 0
fi

[ "$srv" ] || exit
fi

m1="$(gettext 'Start')"
##systemctl is-active $srv  || y1='--button='$m1'!gtk-media-play!systemctl_start_'$srv':2'
#systemctl is-active $srv  || y1=--button=Старт!gtk-media-play!'systemctl start_'$srv'':2
m2="$(gettext 'Stop')"
##systemctl is-active $srv  && y2='--button='$m2'!gtk-media-stop!"systemctl_stop_'$srv'":4'
if systemctl $u is-active $srv  ;then
    y2="--button=$m2!gtk-media-stop!systemctl stop $srv:4"
    gtkmediaplay=gtk-media-play
else
    y1="--button=$m1!gtk-media-play!systemctl start $srv:2"
    gtkmediaplay=gtk-media-stop
fi

m3="$(gettext 'ON')"
systemctl $u is-enabled $srv || y3="--button=$m3!gtk-add!systemctl enable $srv:6"
m4="$(gettext 'OFF')"
systemctl $u is-enabled $srv && y4="--button=$m4!gtk-remove!systemctl disable $srv:8"
yad_output=$(systemctl $u status $srv |sed 's/<//g' |  yad --mouse --Xform --title="$u $srv service" \
    --image=$gtkmediaplay --window-icon=$gtkmediaplay --name=$gtkmediaplay \
    --width=900 --height=600 \
    --fixed --image-on-top \
    --text="<span weight=\"bold\">$u ${srv}</span>$msg" --text-info --fontname="Mono 9" \
    --Xfield="										$(gettext 'Compression Algorithm')!max speed: lz4 > zstd > lzo
max compression: zstd > lzo > lz4:CB" "${ALGO}`systemctl $u list-unit-files --type=service --no-pager --no-legend | awk '{print $1}' |tr "\n" "!"`" \
    --Xfield="										$(gettext 'Allocated % of memory!The amount of RAM for ZRAM as a percentage of total RAM'):NUM" "${PERCENT}:1:100:1:0" \
    --Xfield="										$(gettext 'Priority!Higher number = higher priority. Must be higher than the swap to hdd/ssd.'):NUM" "${PRIORITY}:1:1000:1" \
    "$y1"  \
    "$y2"  \
    "$y3"  \
    "$y4"  \
    --button=$(gettext "journalctl")!terminal!"xterm -e \"journalctl -f\"":3 \
    --button=yad-edit:9 \
    --button=$(gettext 'Menu')!gtk-find:0 \
    --button=yad-cancel:1)
ret="$?"
#echo $ret "$yad_output" ;exit
SC(){
        e="$(systemctl $u $1 "$srv" 2>&1)" && ntf="-i" || ntf="-a"
        [ "$e" ] && NTF $ntf "$srv" "$e"
}
case "$ret" in
    1|252)  # Пользователь отменил
        exit 0
        ;;
    2)  # запуск службы
        SC start
        ;;
    3)  # Просмотр логов
        xterm -e "sudoq journalctl -f" &
        sleep 0.5
        ;;
    0)  #menu
        $0 $u
        exit 0
        ;;
    4)  # Остановка службы с обработкой ошибок
	SC stop
        ;;
    6)  # Включение автозапуска
        SC enable
        ;;
    8)  # Отключение автозапуска
        SC disable
        ;;
    9)  # edit службы
        f="`systemctl $u show -P FragmentPath "$srv"`"
        t1=$(stat -c %Y "$f")
        l3afpad "$f"
        t2=$(stat -c %Y "$f")
        if [[ "$t2" -gt "$t1" ]]; then
        systemctl $u daemon-reload
	 if systemctl $u is-active $srv  ;then
	    SC restart
         fi
        fi
        ;;
esac
$0 $u "$srv" "$2" &
