| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | |
| 4 | |
| 5 | has() { command -v "$1" >/dev/null 2>&1; } |
| 6 | |
| 7 | if [[ $1 == --upgrade ]]; then |
| 8 | if has apt; then sudo apt update && sudo apt upgrade |
| 9 | elif has pacman; then sudo pacman -Syu |
| 10 | elif has dnf; then sudo dnf upgrade |
| 11 | fi |
| 12 | has flatpak && flatpak update |
| 13 | pkill -RTMIN+10 waybar |
| 14 | read -rp "Done — press Enter to close" |
| 15 | exit |
| 16 | fi |
| 17 | |
| 18 | count=0; list="" |
| 19 | if has apt; then |
| 20 | list=$(apt list --upgradable 2>/dev/null | tail -n +2 | cut -d/ -f1) |
| 21 | elif has checkupdates; then |
| 22 | list=$(checkupdates 2>/dev/null | awk '{print $1}') |
| 23 | elif has dnf; then |
| 24 | list=$(dnf -q check-update 2>/dev/null | awk 'NF==3{print $1}') |
| 25 | fi |
| 26 | [[ -n $list ]] && count=$(wc -l <<<"$list") |
| 27 | |
| 28 | if (( count > 0 )); then |
| 29 | tip=$(head -n 20 <<<"$list" | sed ':a;N;$!ba;s/\n/\\n/g') |
| 30 | (( count > 20 )) && tip+="\\n… and $((count-20)) more" |
| 31 | printf '{"text":" %s","class":"pending","tooltip":"%s updates:\\n%s\\n\\nclick to upgrade"}\n' "$count" "$count" "$tip" |
| 32 | else |
| 33 | printf '{"text":"","class":"none","tooltip":"System is up to date"}\n' |
| 34 | fi |