#!/usr/bin/env bash

#NOTIFY_ICON=/usr/share/icons/Papirus/32x32/apps/system-software-update.svg
#~/.config/polybar/hack/scripts/updates.s

get_total_updates() { UPDATES=$(~/.config/polybar/hack/scripts/checkupdates 2>/dev/null | wc -l); }

while true; do
    get_total_updates

    # notify user of updates
    if hash notify-send &>/dev/null; then
        if (( UPDATES > 100 )); then
            notify-send -u low -i $NOTIFY_ICON \
                "Attention," "$UPDATES New packages"
        elif (( UPDATES > 50 )); then
            notify-send -u low -i $NOTIFY_ICON \
                "You should update soon" "$UPDATES New packages"
        elif (( UPDATES > 2 )); then
            notify-send -u low -i $NOTIFY_ICON \
                "$UPDATES New packages"
        fi
    fi

    # when there are updates available
    # every 10 seconds another check for updates is done
    while (( UPDATES > 0 )); do
        if (( UPDATES == 1 )); then
            echo " $UPDATES"
        elif (( UPDATES > 1 )); then
            echo " $UPDATES"
        else
            echo ""
        fi
        sleep 10
        get_total_updates
    done

    # when no updates are available, use a longer loop, this saves on CPU
    # and network uptime, only checking once every 30 min for new updates
    while (( UPDATES == 0 )); do
        echo ""
        sleep 1800
        get_total_updates
    done
done
