#!/bin/bash

# format: print '-' when the count is 0 so the output is readable rather than "0".
format() {
    if [ "$1" -eq 0 ]; then
        echo '-'
    else
        echo "$1"
    fi
}

# checkupdates (pacman-contrib) queries the official repos without touching the local db.
# wc -l counts the resulting lines; failure (no updates / error) falls back to 0.
if ! updates_arch="$(checkupdates | wc -l)"; then
    updates_arch=0
fi

# yay -Qum lists AUR packages where the local version is behind the AUR version.
# 2>/dev/null suppresses "no packages" warnings; failure falls back to 0.
if ! updates_aur="$(yay -Qum 2>/dev/null | wc -l)"; then
    updates_aur=0
fi

# Total pending updates across both official repos and the AUR.
updates="$((updates_arch + updates_aur))"

if [ "$updates" -gt 0 ]; then
    # Format: " (arch_count/aur_count)" — Nerd-Font package icon.
    echo " ($(format $updates_arch)/$(format $updates_aur))"
else
    # Print nothing when fully up-to-date (Waybar will show an empty text field).
    echo
fi
# NOTE: original file contained a typo "fia" instead of "fi"; corrected above.
