#!/bin/bash

# Function to install prerequisites
install_prerequisites() {
  echo "Installing prerequisites..."
  if [ -f /etc/os-release ]; then
    . /etc/os-release
    case ${ID-} in
      debian | ubuntu)
        sudo apt-get update
        sudo apt-get install -y curl wget tar fontconfig
        ;;
      fedora)
        sudo dnf install -y curl wget tar fontconfig
        ;;
      centos | rocky)
        sudo yum install -y curl wget tar fontconfig
        ;;
      opensuse*)
        sudo zypper install -y curl wget tar fontconfig
        ;;
      alpine)
        sudo apk --no-cache add curl wget tar fontconfig
        ;;
      *)
        echo "Unsupported OS. Please install curl, wget, tar, and fontconfig manually."
        exit 1
        ;;
    esac
  else
    echo "Unable to detect OS. Please install curl, wget, tar, and fontconfig manually."
    exit 1
  fi
}

# Ensure `curl` or `wget`, `tar`, and `fc-cache` are installed
# shellcheck disable=SC2312
if [ -z "$(command -v curl)" ] || [ -z "$(command -v wget)" ] || [ -z "$(command -v tar)" ] || [ -z "$(command -v fc-cache)" ]; then
  echo "Prerequisites (curl/wget, tar, fc-cache) not found. Attempting to install..."
  install_prerequisites
fi

# Creating fonts folder if not exists
mkdir -p "${HOME}/.fonts"

download_and_install_font() {
  font_name=$1

  download_url="https://github.com/ryanoasis/nerd-fonts/releases/latest/download/${font_name}.tar.xz"

  # shellcheck disable=SC2312
  if [ -n "$(command -v curl)" ]; then
    curl -sSOL "${download_url}"
  elif [ -n "$(command -v wget)" ]; then
    wget -q "${download_url}"
  fi
  ret=$?

  if [ "${ret}" -ne 0 ]; then
    echo "Error: Download failed for ${font_name}. Check if the font name is correct."
    return 1
  fi

  mkdir -p "${HOME}/.fonts/${font_name}/"
  tar -xf "${font_name}.tar.xz" -C "${HOME}/.fonts/${font_name}/"
  { fc-cache -fv 2>&1; } >/dev/null # Suppressing output but showing errors

  if [ "${ret}" -ne 0 ]; then
    echo "Error: Extraction failed for ${font_name}. Skipping to the next font."
    return 1
  fi

  rm -- "${font_name}.tar.xz"
  echo "[-] ${font_name} Nerd Font installed successfully! [-]"
}

fons_list=("3270" "Agave" "AnonymousPro" "Arimo" "AurulentSansMono" "BigBlueTerminal" "BitstreamVeraSansMono" "CascadiaCode" "CodeNewRoman" "ComicShannsMono" "Cousine" "DaddyTimeMono" "DejaVuSansMono" "DroidSansMono" "FantasqueSansMono" "FiraCode" "FiraMono" "Gohu" "Go-Mono" "Hack" "Hasklig" "HeavyData" "Hermit" "iA-Writer" "IBMPlexMono" "InconsolataGo" "InconsolataLGC" "Inconsolata" "IosevkaTerm" "Iosevka" "JetBrainsMono" "Lekton" "LiberationMono" "Lilex" "Meslo" "Monofur" "Monoid" "Mononoki" "MPlus" "NerdFontsSymbolsOnly" "Noto" "OpenDyslexic" "Overpass" "ProFont" "ProggyClean" "RobotoMono" "ShareTechMono" "SourceCodePro" "SpaceMono" "Terminus" "Tinos" "UbuntuMono" "Ubuntu" "VictorMono")

if [ "$#" -gt 0 ]; then
  for font_name in "$@"; do
    echo "[-] Downloading ${font_name} Nerd Font [-]"
    download_and_install_font "${font_name}"
  done
else
  if [ -t 0 ]; then # Check if script has a terminal
    # Print available fonts in multi-column format
    echo "Available Fonts:"
    for i in "${!fons_list[@]}"; do
      printf "%3d: %-20s" "${i}" "${fons_list[${i}]}"
      if (((i + 1) % 3 == 0)); then
        echo
      fi
    done
    echo

    # Ask user to select fonts
    echo "Enter the numbers of the fonts you want to install, separated by space (e.g., 1 3 5):"
    read -ra selected_indexes

    # Confirm selection
    echo "You've selected:"
    for i in "${selected_indexes[@]}"; do
      echo "${i}: ${fons_list[${i}]}"
    done

    echo "Proceed with installation? [y/N]"
    read -r proceed

    if [ "${proceed}" = "Y" ] || [ "${proceed}" = "y" ]; then
      for i in "${selected_indexes[@]}"; do
        echo "[-] Downloading ${fons_list[${i}]} Nerd Font [-]"
        download_and_install_font "${fons_list[${i}]}"
      done
    else
      echo "Installation aborted."
    fi
  else
    echo "No arguments provided and no terminal available for interactive selection. Aborting."
    exit 1
  fi
fi
