#!/usr/bin/env bash
## Aurto initialisation logic. 
## * ./install: Install aurto
## * ./install remove: Uninstall aurto
set -eu

command=${1:-}
user="${SUDO_USER:-$USER}"
lib_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# shellcheck source=./shared-functions disable=SC1091
source "$lib_dir/shared-functions"

# Initialise the aurto repo, trusted-users list & systemd timers
init() {
  echo "aurto: Initialising for user: $user"

  # Ensure $user is in `wheel`. This is required for update-aurto passwordless sudo to work.
  groups=$(groups "$user")
  if [[ $groups != "wheel "* ]] && [[ $groups != *" wheel "* ]] && [[ $groups != *" wheel" ]]; then
    echo -ne "aurto: $(red user is not in group \`wheel\`), this is required for auto updating." >&2
    echo -e " Add with: $(cyan usermod -aG wheel "$user")" >&2
    exit 2
  fi

  echo "$user" > /usr/lib/aurto/user
  chmod 700 /usr/lib/aurto/user

  install -d /var/cache/pacman/aurto -o "$user"
  sudo -u "$user" tar acf /var/cache/pacman/aurto/aurto.db.tar.zst -T /dev/null
  sudo -u "$user" ln -rsf /var/cache/pacman/aurto/aurto.db{.tar.zst,}

  if ! test -f /etc/aurto/trusted-users; then
    echo 'aurto: Adding default trusted users -> /etc/aurto/trusted-users' >&2
    install -Dm640 -o "$user" /usr/lib/aurto/default-trusted-users.txt /etc/aurto/trusted-users
  fi

  echo 'aurto: Adding & enabling systemd timer update tasks' >&2
  systemctl enable --now /usr/lib/systemd/system/check-aurto-git-trigger.timer
  systemctl enable --now /usr/lib/systemd/system/update-aurto.timer
  systemctl enable /usr/lib/systemd/system/update-aurto-startup.timer

  echo "\"Include = /etc/pacman.d/aurto\" must be added to /etc/pacman.conf to enable the aurto repo"
  if test -t 1; then
    read -p "$(bold Add \"Include = /etc/pacman.d/aurto\" \>\> /etc/pacman.conf \?) [yN] " -n1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
      echo -e "# aurto repo\\nInclude = /etc/pacman.d/aurto" >> /etc/pacman.conf
      yellow "Important: Before package removal \"Include = /etc/pacman.d/aurto\" must be removed from /etc/pacman.conf"
      yellow "This can be done by running $(bold "$(cyan aurto init remove)")"
    fi
  fi
}

# Remove aurto repo files & disable systemd timers
pre_remove() {
  if ! initialised; then
    exit 0
  fi

  echo 'aurto: Removing systemd timer update tasks' >&2
  systemctl disable --now check-aurto-git-trigger.timer || true
  systemctl disable --now update-aurto.timer || true
  systemctl disable update-aurto-startup.timer || true

  echo "aurto: Removing \"$(bold Include = /etc/pacman.d/aurto)\" from pacman.conf" >&2
  sed -i '/^Include = \/etc\/pacman.d\/aurto$/d' /etc/pacman.conf
  sed -i '/^# aurto repo$/d' /etc/pacman.conf

  echo 'aurto: Removing /var/cache/pacman/aurto' >&2
  rm -rf /var/cache/pacman/aurto 2>/dev/null || true

  rm -f /usr/lib/aurto/user 2>/dev/null || true
  rm -f /etc/aurto/trusted-users 2>/dev/null || true
  rm -d /etc/aurto 2>/dev/null || true
}

if [ -z "$command" ]; then
  if initialised; then
    echo 'aurto: already initialised ✓' >&2
    exit 0
  fi

  if [[ $EUID -ne 0 ]]; then 
    exec sudo "$0" "$@"
  fi

  init

elif [[ $command == "remove" ]]; then
  if [[ $EUID -ne 0 ]]; then 
    exec sudo "$0" "$@"
  fi
  pre_remove

else
  echo "unknown command: $command" >&2
  exit 1
fi
