#!/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'

checkout() {
  local repo=$1 rev=$2 dest=$3 stage
  if [[ -e $dest || -L $dest ]]; then
    echo "Preserving existing directory: $dest. Move it aside to retry." >&2
    return 1
  fi
  mkdir -p "$(dirname "$dest")" || return 1
  stage=$(mktemp -d "${dest}.new.XXXXXXXX") || return 1
  if git -C "$stage" init -q && git -C "$stage" remote add origin "$repo" &&
     git -C "$stage" fetch --depth=1 origin "$rev" &&
     git -C "$stage" checkout -q --detach "$rev" &&
     git -C "$stage" submodule update --init --recursive &&
     mv -T --no-clobber "$stage" "$dest" && [[ ! -d $stage ]]; then
    return 0
  fi
  rm -rf -- "$stage"
  return 1
}

run() {
  case $id in
    caelestia)
      sudo pacman -S --needed caelestia-cli && caelestia install ;;
    illogical-impulse)
      local p="$HOME/.cache/dots-hyprland" repo=https://github.com/end-4/dots-hyprland
      checkout "$repo" 21733c3ca7f4edc20faafedea844dc96002c6f7f "$p" &&
        (cd "$p" && ./setup install) ;;
    hyde)
      checkout https://github.com/HyDE-Project/HyDE cc824554ac63417d58253ea6764dc0045d963dce "$HOME/HyDE" &&
        (cd "$HOME/HyDE/Scripts" && ./install.sh) ;;
    ml4w)
      local d
      sudo pacman -S --needed --noconfirm git make curl jq || return 1
      d=$(mktemp -d) || return 1
      checkout https://github.com/mylinuxforwork/ml4w-dotfiles-installer 715112aaea5a1aa33a656948a56410c0e9a76606 "$d/installer" &&
        (cd "$d/installer" && make install) &&
        PATH="$HOME/.local/bin:$PATH" ml4w-dotfiles-installer --install \
          https://raw.githubusercontent.com/mylinuxforwork/dotfiles/c6505852344be629f94bcc8ea6daef6270b4cf8e/hyprland-dotfiles-stable.dotinst
      local rc=$?
      rm -rf "$d"
      return $rc ;;
    jakoolit)
      checkout https://github.com/JaKooLit/Arch-Hyprland 7a3d6609b6419527c9ae79399a183b2ec6c1075d "$HOME/Arch-Hyprland" &&
        (cd "$HOME/Arch-Hyprland" && bash ./install.sh) ;;
    inir)
      checkout https://github.com/snowarch/inir 28365436c2a00d083d29503aacfc0b5b321d67c6 "$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. "
