#!/bin/bash

set -e

echo -e "\e[32m\nUpdate system packages\e[0m"
sudo pacman -Syu --noconfirm

# Update AUR packages if any are installed
if pacman -Qem >/dev/null; then
  if omarchy-pkg-aur-accessible; then
    echo -e "\e[32m\nUpdate AUR packages\e[0m"
    yay -Sua --noconfirm
    echo
  else
    echo -e "\e[31m\nAUR is unavailable (so skipping updates)\e[0m"
    echo
  fi
fi

orphans=$(pacman -Qtdq || true)
if [[ -n $orphans ]]; then
  echo -e "\e[32m\nRemove orphan system packages\e[0m"
  for pkg in $orphans; do
    sudo pacman -Rs --noconfirm "$pkg" || true
  done
  echo
fi
