From b91d942a719f653eef29a399139358c3c548ee24 Mon Sep 17 00:00:00 2001 From: John Mertz Date: Tue, 15 Oct 2024 16:21:58 -0600 Subject: [PATCH] Fix/improve package management for fedora and atomic spins --- waybar/waybar-pkg-fedora-atomic.sh | 51 +++++++++++++++++++++++++ waybar/waybar-pkg-fedora-container.sh | 1 + waybar/waybar-pkg-fedora-sway-atomic.sh | 1 + waybar/waybar-pkg-fedora.sh | 45 ++++++++++++++++++++++ waybar/waybar-pkg.sh | 5 ++- 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100755 waybar/waybar-pkg-fedora-atomic.sh create mode 120000 waybar/waybar-pkg-fedora-container.sh create mode 120000 waybar/waybar-pkg-fedora-sway-atomic.sh create mode 100755 waybar/waybar-pkg-fedora.sh diff --git a/waybar/waybar-pkg-fedora-atomic.sh b/waybar/waybar-pkg-fedora-atomic.sh new file mode 100755 index 0000000..8a0c774 --- /dev/null +++ b/waybar/waybar-pkg-fedora-atomic.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +COUNTFILE=${HOME}/.local/state/$(hostname)-upgradeable +COUNT=0 +REBOOT="" + +function count() { + RESULTS="`rpm-ostree upgrade --check 2> /dev/null | grep Diff: | sed 's/.*\([0-9][0-9]*\).*/\1/'`" + COUNT=0 + for i in $RESULTS; do + i=`echo $i | sed 's/;//'` + let COUNT=$COUNT+$i + done +} + +REBOOT="$(rpm-ostree status | grep Diff | sed -r 's/.*Diff: //')" + +if [ -e $COUNTFILE ] && [[ "$(date +%s) - $(stat -c %Y -- $COUNTFILE)" -gt 360 ]]; then + echo $COUNTFILE is older than 1h. Forcing refresh. + rm $COUNTFILE +fi + +if [ "$1" == 'bar' ]; then + if [ ! -e $COUNTFILE ]; then + count + echo $COUNT > $COUNTFILE + else + COUNT=`cat $COUNTFILE` + fi + if [[ "$COUNT" -gt 0 ]]; then + echo '{"text": "⭳'$COUNT'", "tooltip": "'$COUNT' updates available (click to download)", "class": "updateable"}' + elif [ "$REBOOT" ]; then + echo '{"text": "↻", "tooltip": "Needs reboot: '$REBOOT'", "class": "needs-reboot"}' + else + echo '{"text": "🗹", "tooltip": "Up-to-date", "class": "up-to-date"}' + fi +elif [ "$SUDO_USER" != '' ]; then + echo "Don't run with sudo. Run normally, but with a sudoer user" +elif [ $UID -eq 0 ]; then + echo "Don't run as root. Run normally, but with a sudoer user" +elif [ "$1" == 'upgrade' ]; then + rpm-ostree upgrade + count + echo $COUNT > $COUNTFILE +elif [ "$1" == 'update' ]; then + count + echo $COUNT > $COUNTFILE +else + echo "Missing argument: update, upgrade, bar" +fi + diff --git a/waybar/waybar-pkg-fedora-container.sh b/waybar/waybar-pkg-fedora-container.sh new file mode 120000 index 0000000..3bf31ad --- /dev/null +++ b/waybar/waybar-pkg-fedora-container.sh @@ -0,0 +1 @@ +/var/home/jpm/scripts/waybar/waybar-pkg-fedora.sh \ No newline at end of file diff --git a/waybar/waybar-pkg-fedora-sway-atomic.sh b/waybar/waybar-pkg-fedora-sway-atomic.sh new file mode 120000 index 0000000..ff93fb8 --- /dev/null +++ b/waybar/waybar-pkg-fedora-sway-atomic.sh @@ -0,0 +1 @@ +/var/home/jpm/scripts/waybar/waybar-pkg-fedora-atomic.sh \ No newline at end of file diff --git a/waybar/waybar-pkg-fedora.sh b/waybar/waybar-pkg-fedora.sh new file mode 100755 index 0000000..227a889 --- /dev/null +++ b/waybar/waybar-pkg-fedora.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +COUNTFILE=${HOME}/.local/state/$(hostname)-upgradeable +COUNT=0 + +function count() { + RESULTS="`rpm-ostree upgrade --check 2> /dev/null | grep Diff: | sed 's/.*\([0-9][0-9]*\).*/\1/'`" + COUNT=0 + for i in $RESULTS; do + i=`echo $i | sed 's/;//'` + let COUNT=$COUNT+$i + done +} + +if [ -e $COUNTFILE ] && [[ "$(date +%s) - $(stat -c %Y -- $COUNTFILE)" -gt 360 ]]; then + echo $COUNTFILE is older than 1h. Forcing refresh. + rm $COUNTFILE +fi + +if [ "$1" == 'bar' ]; then + if [ ! -e $COUNTFILE ]; then + count + echo $COUNT > $COUNTFILE + else + COUNT=`cat $COUNTFILE` + fi + if [ $COUNT -eq 0 ]; then + echo '{"text": "🗹", "tooltip": "Up-to-date", "class": "up-to-date"}' + else + echo '{"text": "⭳'$COUNT'", "tooltip": "'$COUNT' updates available (click to download)", "class": "updateable"}' + fi +elif [ "$SUDO_USER" != '' ]; then + echo "Don't run with sudo. Run normally, but with a sudoer user" +elif [ $UID -eq 0 ]; then + echo "Don't run as root. Run normally, but with a sudoer user" +elif [ "$1" == 'upgrade' ]; then + /usr/bin/uxterm -e "rpm-ostree upgrade" + count + echo $COUNT > $COUNTFILE +elif [ "$1" == 'update' ]; then + count + echo $COUNT > $COUNTFILE +else + echo "Missing argument: update, upgrade, bar" +fi diff --git a/waybar/waybar-pkg.sh b/waybar/waybar-pkg.sh index 72cc9b7..6c9f2df 100755 --- a/waybar/waybar-pkg.sh +++ b/waybar/waybar-pkg.sh @@ -6,15 +6,18 @@ VARIANT="$(grep '^VARIANT_ID=' /etc/os-release | cut -d= -f2)" if [ -n "$VARIANT" ]; then OS="${OS}-${VARIANT}" fi - +echo $OS # Build log file for upgradeable package counts/errors COUNT_FILE="$HOME/.local/state/upgradeable" +echo Default $COUNT_FILE if [ -f "/run/.containerenv" ] || [ -f "/.dockerenv" ]; then COUNT_FILE="$COUNT_FILE.$(echo "$HOSTNAME" | cut -d'.' -f1)" + echo Container $COUNT_FILE fi # Build command CMD=${0//pkg/pkg-$OS} +echo $CMD # Log error if command does not exist and exit if [ ! -f "$CMD" ]; then