#!/bin/sh
#191212 sfs
HLP(){
echo 'Usage:	'$0' [-s] "sleep 3" ["text"]
	    выполняет "$1", во время выполнения, выводит "Выполняется "$1" ["text"]"
	    и сообщает (ntf) о результате выполнения
        '$0' -kill 
	    убивает все процессы '$0'
	'$0' -start ["text"] 
	    бесконечный вывод
	-o - не сообщать успешный результат выполнения
	-s - не сообщать любой результат выполнения' && exit
}
[ "$1" = "--help" ] && HLP
[ ! "$1" ] && HLP


[ "$1" = "-s" ] && s=1 && o=1 && shift
[ "$1" = "-o" ] && o=1 && shift

e="$@" ; t="$@"
[ "$2" ] && e="$1" && t="$2"
[ "$1" = "-start" ]  && t="$2" && e= #(wt "sleep 99998888" "$t" &) && exit

PID(){ 
not=
[ -f /usr/lib/notify-osd/notify-osd ] || [ -f /usr/lib/xfce4/notifyd/xfce4-notifyd ] \
    || [ -f /usr/bin/dunst ] \
    || [ -f /usr/lib/notify-osd-customizable/notify-osd ] \
    || [ -f /usr/lib/mate-notification-daemon/mate-notification-daemon ] \
    || [ -f /usr/lib/notification-daemon-1.0/notification-daemon ] \
    || [ -f /usr/bin/lxqt-notificationd ] \
    && { pid="`pidof notify-osd dunst xfce4-notifyd mate-notification-daemon notification-daemon lxqt-notificationd`" ; not=1 ; }
}
#KILL(){ PID ; [ "$pid" ] && sudo kill "$pid" || pkill -f "xpupsay --image=/usr/share/pixmaps/clock_interval.svg" ;}
KILL(){ 
PID 
if [ "$pid" ];then
    sleep 1
    sudo kill $pid 
else pkill -f "xpupsay --image=/usr/share/pixmaps/clock_interval.svg" 
fi
}

while [ -n "$1" ]; do
    if   [ "$1" = "-kill" ] ;then 
    KILL
    #kill `pgrep -f "bsplash --title=wait"`
    #kill "`pgrep -f "yad --title=wait"`" ; killall wt #; kill `pgrep -f "sleep 99998888"` ;
    #kill "`pgrep -f "xpupsay --image=/usr/share/pixmaps/clock_interval.svg"`"
    exit
    #elif [ "$1" = "-s" ] ;then s=1 && shift
    fi ; shift
done

#    [ "$s" ] || ntf -$i "Выполнение '$e'" "закончилось $m" &
#echo $s= ;exit
myloop()
{
#    while  read somefile
    while :
#    while [ "`pidof htop`" ]
    do
         echo "wqw$somefile"
         #((++i <= 100)) && echo o$i
         sleep 1 
    done #< <( find /home -type f -name '*.*' )
}    

YAD(){
#myloop | yad --title='test' --progress --percentage=5 --progress-text="working..." --auto-close 
myloop | yad --title='wait' --geometry=-5+5  --Xon-top --undecorated --no-buttons  \
    --image-on-top --image="/usr/share/pixmaps/cat.gif" \
    --wrap --text  "<b>Выполняется</b>: 
$a" \
    --progress --percentage=5 --pulsate --progress-text="                ждите...                " --auto-close  &
}

TMP1(){
if [ "`which bsplash`" ];then
bsplash --title='wait' -i "/usr/share/pixmaps/cat.gif"   -t "<b>Выполняется:</b>
<i>$t</i>
. . . . . . . . . . . . . . . . . . . ждите..." -x 300 -y 150 -p "5000000,20"  -f "DejaVu Sans 10" >/dev/null &
else
yad --title=wait --image="/usr/share/pixmaps/clock_interval.svg"  --undecorated  X--no-buttons --geometry=-0 --progress \
    --pulsate --skip-taskbar --timeout-indicator=bottom --wrap --text "<span color=\"green\"><b>Выполняется:</b></span>
<i>$t</i>
. . . . . . . . . . ждите. . . . . . . . . . " &
fi
}
#ntf -i  "<span color=\"green\"><b>Выполняется:</b></span>
#<i>$t</i>" \
#". . . . . . ждите. . . . . . . " &
PID
if [ "$not" ];then
    tt=0
    [ -f /usr/lib/notify-osd-customizable/notify-osd ] && tt=9999999 && dd=dbus-launch
    [ -f /usr/bin/lxqt-notificationd ] && tt=9999999 && dd=dbus-launch
    dd=dbus-launch
    sudou="sudo -u "`sfsusr`" $dd "
    $sudou  /usr/bin/notify-send -i "/usr/share/pixmaps/clock_interval.svg" -t $tt "Выполняется: 
$t" ". . . . . . ждите. . . . . . ." &
else xpupsay --image="/usr/share/pixmaps/clock_interval.svg" -f "Mono 12" --time=999999  --at=10000000,$(( 15*`pidof xpupsay|wc -m` )) "<span color='green'>Выполняется: 
$t</span>
<span font=\"Mono 10\"><i>. . . . . . ждите. . . . . . .</i></span>" &
fi

if [ ! "$e" = "" ] ;then
    er="\n\n`$e 2>&1`"
    #er="\n\n`${e} 2>&1`"
    #er="\n\n$("${e}" 2>&1)"
    	if [ "$?" = "0" ] ;then 
	    m=успешно ; i=i ;er= ;ex=0 
    #kill `pgrep -f "yad --title=wait"`
    #kill `pgrep -f "bsplash --title=wait"`
    #kill `pgrep -f "xpupsay --image=/usr/share/pixmaps/clock_interval.svg"`
    KILL
    [ "$o" ] || ntf -$i "Выполнение '$e'" "закончилось $m$er" &
	else
	    m="неудачей" ; i=a ;ex=1 
	    KILL
    #kill `pgrep -f "yad --title=wait"`
    #kill `pgrep -f "bsplash --title=wait"`
    #kill `pgrep -f "xpupsay --image=/usr/share/pixmaps/clock_interval.svg"`
    [ "$s" ] || ntf -$i "Выполнение '$e'" "закончилось $m$er" 
	fi
    #kill `pgrep -f "yad --title=wait"`
#    [ "$s" ] || ntf -$i "Выполнение '$e'" "закончилось $m$er" $ii
    exit $ex
fi
