#!/usr/bin/env bash
## aurto commands, see bottom help output

set -Eeuo pipefail
version="0.13.0"
command=${1:-}
arg1=${2:-}

if [[ $EUID -eq 0 ]]; then
  # shouldn't be run as root
  if [ -f /usr/lib/aurto/user ]; then
    user=$(cat /usr/lib/aurto/user)
    exec sudo -u "$user" "$0" "$@"
  else
    echo "aurto not initialised. Run $(green aurto init) as a non-root user"
    exit 1
  fi
fi

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

if [[ $command != "init" ]] && ! initialised; then
  echo "aurto not initialised. Run $(green aurto init)"
  exit 1
fi

function aurto_sync { sudo pacsync aurto >/dev/null; }

function check_new_package_trust {
  local mistrust
  local not_in_aur
  local mistrusted_users

  if [ -f /etc/aurto/trusted-users ]; then
    echo "aurto: Checking maintainer trust..." >&2
  else
    echo "aurto: Checking maintainer trust... $(dim disabled)" >&2
  fi

  mistrust=$("$lib_dir"/trust-check "${@:1}")
  if [ -z "$mistrust" ]; then
    if [ -f /etc/aurto/trusted-users ]; then
      rm_last_print
      echo "aurto: Checking maintainer trust... $(green ✓)" >&2
    fi
  else
    not_in_aur=$(not_in_aur_packages "$mistrust")
    if [ -n "$not_in_aur" ]; then
      rm_last_print
      echo "aurto: Package not in AUR: $(yellow "$not_in_aur")" >&2
      exit 1
    fi

    mistrusted_users=$(new_line_to_space_separated_unique "$(echo "$mistrust" | cut -d: -f2)")
    rm_last_print
    echo -n "aurto: Trust maintainer(s): $(bold "$mistrusted_users")? [y/N] " >&2
    read -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
      echo "aurto: Adding $(bold "$mistrusted_users") >> /etc/aurto/trusted-users" >&2
      # shellcheck disable=SC2001
      echo "$mistrusted_users" | sed -e 's/ /\n/g' >> /etc/aurto/trusted-users
    else
      exit 1
    fi
  fi
}

echo_status() {
  echo "$(bold aurto) v$version managing $(bold "$(pacman -Sql aurto | wc -l)") aur packages"

  echo
  echo "Packages: $(dim pacman -Sl aurto)"
  pacman -Sl aurto --color=always \
   | sed 's/^/  /'

  echo
  echo "Timers: $(dim systemctl list-timers)"
  list_timers=$(systemctl list-timers -a)
  echo "  $(echo "$list_timers" | head -n1 | cut -c1-"$COLUMNS")"
  echo "$list_timers" \
   | grep -E 'aurto' \
   | sed 's/^/  /' \
   | cut -c1-"$COLUMNS"

  echo
  echo "Recent logs: $(dim journalctl -u update-aurto --since \'1.5 hours ago\')"
  journalctl -u update-aurto --since '1.5 hours ago' \
   | sed 's/^/  /' \
   | cut -c1-"$COLUMNS"

  echo
  echo "Log warnings: $(dim journalctl -u update-aurto --since \'1 week ago\' \| grep -v \'Skipping all source file integrity\' \|  grep -E \'ERROR\|WARN\|FAIL\' -A5 -B5)"
  log_warns=$(
    journalctl -u update-aurto --since '1 week ago' \
     | grep -v 'Skipping all source file integrity' \
     | grep -E 'ERROR|WARN|FAIL' -A5 -B5 --color=always \
     | sed 's/^/  /'
  )
  if [ -n "$log_warns" ]; then
    echo "$log_warns" | cut -c1-"$COLUMNS"
  else
    green '  None'
  fi
}

if [ "$command" == "add" ] && [ -n "$arg1" ]; then
  packages_and_deps="$(aur depends --pkgname "${@:2}")"
  # shellcheck disable=SC2086
  check_new_package_trust $packages_and_deps

  ## Create aurto-add.xxx marker file to indicate `aurto add` is running
  mkdir -p ~/.cache/aurto
  aurto_add_temp=$(mktemp ~/.cache/aurto/aurto-add.XXXXXXXX)
  trap 'rm -f "$aurto_add_temp"' EXIT

  aurto_sync

  sync_args=(--no-view --no-confirm --database=aurto -k0)
  if [ -n "$chroot_arg" ]; then
    sync_args+=("$chroot_arg" --makepkg-conf=/etc/aurto/makepkg-chroot.conf --pacman-conf=/etc/aurto/pacman-chroot.conf)
  else
    sync_args+=(--no-ver)
  fi
  echo "aurto: Running: $(dim aur sync "${sync_args[*]}") $(cyan "${*:2}")" >&2
  AUR_SYNC_USE_NINJA=${AUR_SYNC_USE_NINJA:-1} aur sync "${sync_args[@]}" "${@:2}"
  
  aurto_sync
  echo -e "aurto: To install run: $(green pacman -Syu) $(cyan "${*:2}")" >&2

elif [ "$command" == "addpkg" ] && [ -n "$arg1" ]; then
  check_new_package_trust "${@:2}"
  db_tar_path=$(readlink -f /var/cache/pacman/aurto/aurto.db)
  echo "aurto: Running: $(dim repo-add "$db_tar_path") $(cyan "${*:2}")" >&2
  repo-add "$db_tar_path" "${@:2}"
  for pkg in "${@:2}"; do
    cp "$pkg" /var/cache/pacman/aurto/
  done
  aurto_sync

elif [ "$command" == "remove" ] && [ -n "$arg1" ]; then
  db_tar_path=$(readlink -f /var/cache/pacman/aurto/aurto.db)
  removed=""
  for pkg in "${@:2}"; do
    if remove_out=$(repo-remove "$db_tar_path" "$pkg" 2>&1); then
      if [[ $remove_out = *"ERROR"* ]]; then
        # may no longer be necessary as repo-remove seems to exit non-zero in this case
        echo "aurto: $(cyan "$pkg") $(red not found)" >&2
      else
        rm -rf /var/cache/pacman/aurto/"$pkg"*.pkg.* || true
        removed="$pkg $removed"
      fi
    else
      echo "aurto: $(cyan "$pkg") $(red not found)" >&2
    fi
  done
  if [ -n "$removed" ]; then
    echo -e "aurto: Removed $(cyan "$removed")" >&2
    aurto_sync
  fi

elif [ "$command" == "status" ]; then
  aurto_sync
  echo_status | less -RF
elif [[ $command == "init" ]]; then
  shift
  /usr/lib/aurto/install "$@"
else
  echo "$(bold aurto) v$version: simple management tool for the 'aurto' repository"
  echo "  General usage: $(green aurto add)|$(green addpkg)|$(green remove) $(cyan PACKAGES...)"
  echo
  echo "  Examples"
  echo "  - add: build aur packages & dependencies, add them to the 'aurto' repo"
  echo "      $(green aurto add) $(cyan aurutils)"
  echo
  echo "  - remove: remove packages from the 'aurto' repo (does not uninstall)"
  echo "      $(green aurto remove) $(cyan aurutils)"
  echo
  echo "  - addpkg: add a prebuilt package to the 'aurto' repo"
  echo "      $(green aurto addpkg) $(cyan aurutils-2.3.1-1-any.pkg.tar.xz)"
  echo
  echo "  - status: current 'aurto' repo packages, logs & info"
  echo "      $(green aurto status)"
  exit 1
fi
