#!/bin/bash
conf="${XDG_CONFIG_HOME:-$HOME/.config}/fiordland/dots"
names=(caelestia illogical-impulse hyde ml4w jakoolit inir)

usage() {
  echo "usage: fiordland-dots <${names[*]// /|}>"
  exit 2
}

if [[ $1 != --run ]]; then
  if [[ -n $1 ]]; then
    id=$1
  else
    [[ -r $conf ]] || exit 0
    id=$(<"$conf")
    rm -f "$conf"
  fi
  [[ " ${names[*]} " == *" $id "* ]] || usage
  for t in kitty foot alacritty konsole gnome-terminal xterm; do
    if command -v "$t" >/dev/null; then
      case $t in
        kitty) exec kitty --class fiordland-dots -e fiordland-dots --run "$id" ;;
        foot) exec foot --app-id fiordland-dots fiordland-dots --run "$id" ;;
        gnome-terminal) exec gnome-terminal -- fiordland-dots --run "$id" ;;
        *) exec "$t" -e fiordland-dots --run "$id" ;;
      esac
    fi
  done
  exit 1
fi

id=$2
bold=$'\e[1m' dim=$'\e[2m' blue=$'\e[38;2;142;197;255m' reset=$'\e[0m'

aur_helper() {
  command -v paru >/dev/null || command -v yay >/dev/null && return
  echo "${dim}Installing paru for AUR packages${reset}"
  local d
  d=$(mktemp -d)
  git clone --depth 1 https://aur.archlinux.org/paru-bin.git "$d/paru-bin" && (cd "$d/paru-bin" && makepkg -si --noconfirm)
  rm -rf "$d"
}

run() {
  case $id in
    caelestia)
      aur_helper
      local h; h=$(command -v paru || command -v yay)
      "$h" -S --needed caelestia-cli && caelestia install ;;
    illogical-impulse)
      local p="$HOME/.cache/dots-hyprland" repo=https://github.com/end-4/dots-hyprland
      if [ -d "$p/.git" ]; then
        git -C "$p" remote get-url origin | grep -q "end-4/dots-hyprland" || { echo "$p is not a copy of $repo"; return 1; }
        git -C "$p" pull --ff-only origin main || return 1
      else
        rm -rf "$p" && git clone "$repo" "$p" || return 1
      fi
      git -C "$p" submodule update --init --recursive && (cd "$p" && ./setup install) ;;
    hyde)
      rm -rf "$HOME/HyDE"
      git clone --depth 1 https://github.com/HyDE-Project/HyDE "$HOME/HyDE" && cd "$HOME/HyDE/Scripts" && ./install.sh ;;
    ml4w)
      local d tag repo=https://github.com/mylinuxforwork/ml4w-dotfiles-installer.git
      sudo pacman -S --needed --noconfirm git make curl jq || return 1
      tag=$(git ls-remote --tags --refs "$repo" | sed 's|.*refs/tags/||' | grep -E '^v?[0-9]+(\.[0-9]+)*$' | sort -V | tail -n1)
      [ -n "$tag" ] || { echo "could not find an ML4W installer release"; return 1; }
      d=$(mktemp -d)
      git clone --depth=1 --branch "$tag" -c advice.detachedHead=false "$repo" "$d" && (cd "$d" && make install) &&
        PATH="$HOME/.local/bin:$PATH" ml4w-dotfiles-installer --install \
          https://raw.githubusercontent.com/mylinuxforwork/dotfiles/main/hyprland-dotfiles-stable.dotinst
      local rc=$?
      rm -rf "$d"
      return $rc ;;
    jakoolit)
      sh <(curl -fsSL https://raw.githubusercontent.com/JaKooLit/Arch-Hyprland/main/auto-install.sh) ;;
    inir)
      mkdir -p "$HOME/.local/src" && rm -rf "$HOME/.local/src/inir"
      git clone https://github.com/snowarch/inir.git "$HOME/.local/src/inir" && cd "$HOME/.local/src/inir" && ./setup install ;;
  esac
}

clear
echo "${bold}${blue}Setting up $id${reset}"
echo "${dim}This runs the official installer from the $id project. It asks for your password and may ask a few questions.${reset}"
echo
until curl -fsI --max-time 5 https://github.com >/dev/null 2>&1; do
  echo "${dim}Waiting for an internet connection...${reset}"
  sleep 5
done
read -rp "Press Enter to start, or Ctrl+C to skip. "
if [[ $(cat /etc/fiordland/channel 2>/dev/null) == stable ]]; then
  echo "${dim}Syncing system packages with the stable channel first${reset}"
  if ! sudo pacman -Syuu; then
    echo
    echo "${bold}Could not sync system packages.${reset} Run ${blue}tawaki update${reset}, then ${blue}fiordland-dots $id${reset} to try again."
    read -rp "Press Enter to close. "
    exit 1
  fi
  echo
fi
if run; then
  echo
  echo "${bold}Done.${reset} Log out and back in, or restart, to load the new setup."
else
  echo
  echo "${bold}The installer stopped with an error.${reset} Run ${blue}fiordland-dots $id${reset} to try again."
fi
read -rp "Press Enter to close. "
