shtools/zupdate/sysupdate
zawz 2f03f2e23f zupdate: improve sysupdate
~ will not prompt if a fullscreen app is running
2020-10-23 10:04:23 +02:00

138 lines
3.4 KiB
Bash
Executable file

#!/bin/sh
warning() {
if [ -z "$opt_w" ] ; then
printf "\033[0;33m%s\033[0m\n" "$1" >&2
fi
}
# $1 = bin name , $* = warn message
# return value: passed
warn_which()
{
which "$1" >/dev/null 2>&1 || { warning "$*"; return 1; }
}
fullscreen_window()
{
warn_which xwininfo "not found, fullscreen detection missing" || return $?
warn_which xdotool "not found, fullscreen detection missing" || return $?
info=$(xwininfo -id $(xdotool getactivewindow)) || return $?
window_geo=$(echo "$info" | awk '{if($1=="-geometry") print $2}')
if [ -n "$opt_A" ] # all screens not just main
then
# window geometry calculation
window_offset=$(echo "$info" | awk '{if($1=="Corners:") print $2}')
window_geo=$window_geo$window_offset
window_geo=$(echo "$window_geo" | cut -d '-' -f1 | cut -d '+' -f1)$window_offset
# get all monitor geometry
all_geo=$(xrandr | awk '{if($2 == "connected"){ if($3 == "primary") print $4;else print $3 }}' )
# test
echo "$all_geo" | grep -q "^$window_geo\$"
else
# get main monitor geometry
main_geo=$(xrandr | awk '{if($2 == "connected" && $3 == "primary") print $4}')
# test
[ "$window_geo" = "$main_geo" ]
fi
}
opt_A=y
linuxpkgnames=""
[ -z "$TMPDIR" ] && TMPDIR=/tmp
lock_file="$TMPDIR/sysupdate.lock"
get_running_programs()
{
echo "$*" | tr '-' '_' | grep -qwE "$(echo "$linuxpkgnames" | tr '-' '_')" && echo linux
for I in $*
do
pgrep -xi "$I" >/dev/null && echo $I
done
}
stop()
{
rm "$lock_file"
exit $1
}
fetch_error()
{
echo "Error: Could not fetch packages" >&2
znotif -T "System updates" -t 120 -m "Error during system updates: could not fetch packages"
stop 3
}
update_error()
{
echo "Error: Could not upgrade system" >&2
znotif -T "System updates" -t 120 -m "Error during system updates: could not upgrade"
stop 3
}
which zupdate >/dev/null 2>&1 || { echo "zupdate not installed" >&2; exit 4; }
which znotif >/dev/null 2>&1 || { echo "znotif not installed" >&2; exit 4; }
# Check if another process is running
if [ -n "$(pgrep zupdate)" ] || [ -f "$lock_file" ]
then
echo "Updates are already running" >&2
exit 1
fi
touch "$lock_file"
# package manager specific variables
if which apt >/dev/null 2>&1
then
linuxpkgnames="linux-image-.*"
elif which pacman > /dev/null 2>&1
then
linuxpkgnames="linux|linux-hardened|linux-lts|linux-zen|linux-mainline|linux-rt|linux-git|linux-next"
else
echo "Unsupported package manager" >&2
stop 2
fi
#fetch updates
all=$(zupdate -MkdL) || fetch_error
size=$(echo "$all" | head -n1)
packages=$(echo "$all" | cut -d' ' -f1 | tail -n+2)
if [ -z "$packages" ]
then
echo "No updates"
stop 0
fi
# find running updates
running_programs=$(get_running_programs "$packages")
if [ -n "$running_programs" ]
then
if fullscreen_window
then
znotif -T "System Updates" -t 99999 -m "The following running programs have recieved updates while you were busy:
$(echo "$running_programs" | sed 's|^| - |g')
Run 'sysupdate' again to upgrade"
stop 5
else
if ! znotif -T "System Updates" -y -m "The following running programs have recieved updates: \n$(echo "$running_programs" | sed 's|^| - |g')\nProceed?"
then
echo "Update cancelled" >&2
stop 5
fi
fi
fi
znotif -T "System Updates" -t 10 -m "Installing updates for $size download"
#update
zupdate -yu || update_error
#end update
znotif -T "System Updates" -t 5 -m "Updates finished"
stop 0