#!/usr/bin/env bash
# Pending updates: official repos (checkupdates) + AUR (paru -Qua).
#   count   number of pending updates (default)
#   list    JSON [{name, old, new, src}]   src = repo|aur
set -u
repo(){ checkupdates 2>/dev/null; }   # "name  old  ->  new"
aur(){  paru -Qua  2>/dev/null; }      # "name  old  ->  new"
case "${1:-count}" in
  count) echo $(( $(repo|wc -l) + $(aur|wc -l) )) ;;
  list)
    { repo | awk '{print $1"\t"$2"\t"$4"\trepo"}'
      aur  | awk '{print $1"\t"$2"\t"$4"\taur"}'; } \
    | jq -R -s -c 'split("\n")|map(select(length>0))|map(split("\t"))
                   |map({name:.[0],old:.[1],new:.[2],src:.[3]})' ;;
esac
