dotfiles/.config/eww/scripts/pkg-updates.sh

67 lines
1.3 KiB
Bash
Executable file

#!/bin/bash
# Time in seconds between each check
interval=3600
# Number of updates displayed in tooltip
max_shown=25
function check {
# Initiate variables
declare -i retry_count=0
declare -i max_retries=5
declare -i retry_delay=30
declare -i success=0
# Retry until success or retry count reaches max_retries
while [[ $retry_count -le $max_retries && $success -ne 1 ]]; do
cmd_res="$(checkupdates 2>&1)"
case $? in
# Update(s) available
0)
success=1
count=$(echo "$cmd_res" | wc -l)
tooltip="$(echo "$cmd_res" | head -n "$max_shown" | sed -z 's/\n/\\n/g' | sed 's/\\n$//')"
if [[ $count -gt $max_shown ]]; then
tooltip="$tooltip\n[..] +$((count - max_shown))"
fi
class=" "
;;
# No update available
2)
success=1
count=0
tooltip="No update available."
class="disabled"
;;
*)
retry_count+=1
count=""
tooltip="$(echo "$cmd_res" | sed -z 's/\n/\\n/g' | sed 's/\\n$//')"
class="error"
;;
esac
# Show time of check
time="$(date '+%H:%M:%S')"
tooltip="$tooltip\n\nLatest check: $time"
cat <<- EOF
{"count": "$count", "tooltip": "$tooltip", "class": "$class"}
EOF
[[ $success -ne 1 ]] && sleep $retry_delay
done
}
declare -i sleep_pid
while :; do
check
sleep $interval &
sleep_pid=$!
trap "kill $sleep_pid" USR1
wait $sleep_pid
done