#!/usr/bin/env bash
## Builds new aur packages into repo 'aurto' (run as root)
## Should be in the same dir as `check-aurto-git`
set -Eeuo pipefail

readonly AURVCS=${AURVCS:-.*-(cvs|svn|git|hg|bzr|darcs)$}

if [[ $EUID -ne 0 ]]; then
  # retry as root
  exec sudo "$0" "$@"
fi

lib_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
user=$(cat /usr/lib/aurto/user)
# shellcheck source=./shared-functions disable=SC1091
source "$lib_dir/shared-functions"

if [ -z "$user" ]; then
  echo 'Missing /usr/lib/aurto/user' >&2
  exit 1
fi

## Clean aurutils cache, if no aurto-add sessions are running
clean_aurutils_cache() {
  user_home=$(getent passwd "$user" | cut -d: -f6)
  aurutils_cache=$user_home/.cache/aurutils/sync/
  if [ -d "$aurutils_cache" ] && ! compgen -G "$user_home/.cache/aurto/aurto-add.*" >/dev/null; then
    rm -rf "$aurutils_cache"
  fi
}
trap clean_aurutils_cache ERR

pacsync aurto >/dev/null || true

## Check trust
## - remove packages no longer in the AUR
## - remove packages with maintainers lacking trust
if [ -f /etc/aurto/trusted-users ]; then
  echo "aurto: Checking maintainer trust..." >&2
else
  echo "aurto: Checking maintainer trust... $(dim disabled)" >&2
fi
# shellcheck disable=SC2046
mistrust=$("$lib_dir"/trust-check $(pacman -Slq aurto))
if [ -z "$mistrust" ]; then
  if [ -f /etc/aurto/trusted-users ]; then
    rm_last_print
    echo "Checking maintainer trust... $(green ✓)" >&2
  fi
else
  not_in_aur=$(not_in_aur_packages "$mistrust")
  mistrusted_pkgs=$(new_line_to_space_separated_unique "$(echo "$mistrust" | grep -v '::' | cut -d: -f1)")

  if [ -n "$not_in_aur" ]; then
    rm_last_print
    # shellcheck disable=SC2086
    aurto remove $not_in_aur
    echo "$(yellow WARNING:) Packages no longer in AUR removed from aurto: $(yellow "$not_in_aur")" >&2
  fi
  if [ -n "$mistrusted_pkgs" ]; then
    # shellcheck disable=SC2086
    aurto remove $mistrusted_pkgs
    echo -n "$(yellow WARNING:) Packages with unknown maintainers removed from aurto, " >&2
    echo "re-add with: $(green aurto add) $(cyan "$mistrusted_pkgs")" >&2
  fi
fi

modify=$(last_pkg_modify)

sync_args=(--no-view --no-confirm --database=aurto --upgrades -k0)
if [ -n "$chroot_arg" ]; then
  sync_args+=("$chroot_arg" --makepkg-conf=/etc/aurto/makepkg-chroot.conf --pacman-conf=/etc/aurto/pacman-chroot.conf)
fi
echo "Running: aur sync ${sync_args[*]}" >&2
sudo -u "$user" AUR_SYNC_USE_NINJA="${AUR_SYNC_USE_NINJA:-1}" aur sync "${sync_args[@]}"

if rm "$lib_dir/check-vcs" 2>/dev/null; then
  vcs_pkgs=$(aur repo --database=aurto --list | cut -f1 | grep -E "$AURVCS" || true)
  if [ -n "$vcs_pkgs" ]; then
    echo "Checking $(echo "$vcs_pkgs" | wc -l) VCS packages matching $(yellow "$AURVCS") for updates..." >&2
    sudo -u "$user" AURVCS="$AURVCS" "$lib_dir"/sync-devel "$chroot_arg"
  fi
fi

pacsync aurto || true
paccache -rk1 -c /var/cache/pacman/aurto

after_modify=$(last_pkg_modify)
if [ "${after_modify:-0}" != "${modify:-0}" ]; then
  ## prompt listeners to `/var/lib/pacman/local` that something has changed
  ## E.g. RaphaelRochet/arch-update
  touch /var/lib/pacman/local/aurto-arch-update
fi
