#!/usr/bin/env bash
set -euo pipefail

REPO_NAME="aer"
PROVIDER_NAME="aetheros"
AER_CONF_PATH="/etc/pacman.d/${PROVIDER_NAME}.conf"
PACMAN_CONF="/etc/pacman.conf"

AER_CONF_URL="https://aetheros.kitechsoftware.com/conf/aer/${PROVIDER_NAME}.conf"
AER_KEY_URL="https://aetheros.kitechsoftware.com/repo/gpg/${PROVIDER_NAME}.pub"
AER_FPR_URL="https://aetheros.kitechsoftware.com/repo/gpg/${PROVIDER_NAME}.fingerprint"

AUTO_YES=0
COMMAND=""

get_fingerprint() {
  curl -fsSL "$AER_FPR_URL"
}

echo_prefix() {
  echo "==> $*"
}

confirm() {
  [[ "$AUTO_YES" -eq 1 ]] && return 0
  read -rp "$1 [y/N] " reply
  [[ "$reply" == "y" || "$reply" == "Y" ]]
}

install() {
  echo_prefix "Adding AetherOS (${REPO_NAME}) repository"

  echo "Fingerprint:"
  get_fingerprint
  echo

  if ! confirm "Proceed with installation?"; then
    echo "Aborted."
    exit 1
  fi

  curl -fsSL "$AER_CONF_URL" | sudo tee "$AER_CONF_PATH" >/dev/null
  curl -fsSL "$AER_KEY_URL" | sudo pacman-key --add -
  sudo pacman-key --lsign-key "$(get_fingerprint)"

  if ! grep -qF "Include = $AER_CONF_PATH" "$PACMAN_CONF"; then
    echo "Include = $AER_CONF_PATH" | sudo tee -a "$PACMAN_CONF" >/dev/null
  fi

  sudo pacman -Sy
  echo_prefix "${REPO_NAME} repository added"
}


uninstall() {
  echo_prefix "Removing ${REPO_NAME} repository"

  if ! confirm "Remove ${REPO_NAME} repository and trust?"; then
    echo "Aborted."
    exit 1
  fi

  sudo sed -i "\|Include = $AER_CONF_PATH|d" "$PACMAN_CONF" 2>/dev/null || true
  sudo rm -f "$AER_CONF_PATH" 2>/dev/null || true

  FPR="$(get_fingerprint)"
  sudo pacman-key --delete "$FPR" &>/dev/null || true

  sudo pacman -Sy
  echo_prefix "${REPO_NAME} repository removed"
}

check() {
  echo_prefix "Checking ${REPO_NAME} repository"

  if [[ -f "$AER_CONF_PATH" ]]; then
    echo "✓ Repo config exists"
  else
    echo "✗ Repo config missing"
  fi

  if grep -qF "Include = $AER_CONF_PATH" "$PACMAN_CONF"; then
    echo "✓ pacman.conf include present"
  else
    echo "✗ pacman.conf include missing"
  fi

  FPR="$(get_fingerprint)"
  if sudo pacman-key --list-keys "$FPR" &>/dev/null; then
    echo "✓ GPG key installed"
  else
    echo "✗ GPG key not installed"
  fi
}


usage() {
  cat <<EOF
Usage: $0 <command> [options]

Commands:
  install     Add the AetherOS (${REPO_NAME}) pacman repository
  uninstall   Remove the AetherOS (${REPO_NAME}) pacman repository
  remove      Alias for uninstall
  check       Verify repository configuration
  help        Show this message

Options:
  -y, --yes      Skip confirmation prompts
  -h, --help     Show help
EOF
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    install|uninstall|remove|check)
      COMMAND="$1"
      shift
      ;;
    -y|--yes)
      AUTO_YES=1
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "Unknown option: $1"
      usage
      exit 1
      ;;
  esac
done

case "$COMMAND" in
  install)
    install
    ;;
  uninstall|remove)
    uninstall
    ;;
  check)
    check
    ;;
  ""|help)
    usage
    ;;
  *)
    echo "Unknown command: $COMMAND"
    usage
    exit 1
    ;;
esac


