#!/bin/bash

# Prefer AUR helpers over plain pacman when they are on PATH.
# hash is faster than which for simple existence checks.
pkgmgr="pacman"
hash paru 2>/dev/null && pkgmgr="paru"
hash yay 2>/dev/null && pkgmgr="yay"

# Set IFS to newlines so the package-list array splits on lines, not spaces.
# \r is included to handle any Windows-style line endings from the output.
IFS=$'\n'$'\r'

# -Qu lists packages that have an available upgrade; array length = update count.
updatesli=($($pkgmgr -Qu))
text=${#updatesli[@]}
# Show an empty icon when up-to-date; show a package emoji when updates are pending.
icon=""
[ $text -eq 0 ] && icon="" || icon="📦"

# Build the tooltip string by concatenating each pending package on its own line.
for i in ${updatesli[@]}
do
  tooltip+="$i\n"
done

# Emit a Waybar JSON payload: text is the status icon, tooltip shows the count.
cat << EOF
{ "text":"$icon", "tooltip":"UPDATES:  $text"}
EOF
