#!/bin/bash

# omarchy:summary=Check whether Omarchy updates are available.

set -euo pipefail

updates=()

if [[ $OMARCHY_PATH != "/usr/share/omarchy" ]]; then
  upstream=$(git -C "$OMARCHY_PATH" rev-parse --abbrev-ref --symbolic-full-name '@{upstream}' 2>/dev/null || true)
  if [[ -n $upstream ]]; then
    GIT_TERMINAL_PROMPT=0 timeout 10 git -C "$OMARCHY_PATH" fetch --quiet 2>/dev/null || true

    behind=$(git -C "$OMARCHY_PATH" rev-list --count "HEAD..$upstream" 2>/dev/null || echo 0)
    if (( behind > 0 )); then
      commit_label=commits
      (( behind == 1 )) && commit_label=commit
      updates+=("omarchy-dev-checkout $behind new $commit_label on $upstream")
    fi
  fi
fi

package=""
if pacman -Qq omarchy-dev >/dev/null 2>&1; then
  package=omarchy-dev
elif pacman -Qq omarchy >/dev/null 2>&1; then
  package=omarchy
fi

if [[ -n $package ]]; then
  update=$(checkupdates --nocolor 2>/dev/null | awk -v package="$package" '$1 == package { print; exit }' || true)
fi

if [[ -n ${update:-} ]]; then
  updates+=("$update")
fi

if (( ${#updates[@]} > 0 )); then
  printf '%s\n' "${updates[@]}"
  exit 0
else
  echo "Omarchy is up to date"
  exit 1
fi
