#!/bin/bash
# FWS — mise à jour du système en une commande (bouton de la barre, fws-hello,
# ou terminal). pacman -Syu + nettoyage du cache + rapport des orphelins.
set -u
say() { printf '\n\033[1;34m==> %s\033[0m\n' "$*"; }

if [ "$(id -u)" -ne 0 ]; then
    exec sudo "$0" "$@"
fi

. /usr/local/lib/fws/i18n.sh update common

say "$(t update.updating_system)"
pacman -Syu || { echo "$(t update.failed)"; read -rp "$(t common.press_enter_to_close) " _; exit 1; }

if command -v paccache >/dev/null 2>&1; then
    say "$(t update.cleaning_cache)"
    paccache -rk2
fi

ORPHANS="$(pacman -Qtdq 2>/dev/null)"
if [ -n "$ORPHANS" ]; then
    say "$(t update.orphans)"
    echo "$ORPHANS"
    read -rp "$(t update.remove_orphans) $(t common.yes_no) " _rm
    t_is_yes "$_rm" && echo "$ORPHANS" | pacman -Rns --noconfirm -
fi

if command -v flatpak >/dev/null 2>&1; then
    say "$(t update.updating_flatpaks)"
    flatpak update -y || true
fi

say "$(t update.done)"
read -rp "$(t common.press_enter_to_close) " _
