#!/usr/bin/env bash
# Host-aware installer for every CLI tool the dotfiles assume at runtime.
#
#   macOS  -> Homebrew (bootstraps Homebrew itself if missing)
#   Linux  -> apt (Debian/Ubuntu) + upstream installers for tools not in apt
#
# zinit and all zsh plugins (powerlevel10k, zsh-autosuggestions,
# zsh-syntax-highlighting, zsh-completions, zsh-vi-mode, fzf-tab) bootstrap
# themselves from ~/.zshrc on first shell start — they are NOT installed here.
#
# Idempotent: re-running only fills gaps. Pairs with ./link-dotfiles, or run
# `./link-dotfiles --apps` to install then link in one shot.
#
# Consolidates the former macinstaller / install-apt-pkgs / install-tools /
# install-zellij scripts.
set -euo pipefail

# fzf-git.sh is a sourced helper (not a package) — same on every platform.
clone_fzf_git() {
  if [ -d "$HOME/fzf-git.sh" ]; then
    echo "fzf-git.sh already cloned — skipping"
  else
    git clone --depth 1 https://github.com/junegunn/fzf-git.sh.git "$HOME/fzf-git.sh"
  fi
}

install_macos() {
  if ! command -v brew >/dev/null 2>&1; then
    echo "Homebrew not found — installing…"
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    if   [ -x /opt/homebrew/bin/brew ]; then eval "$(/opt/homebrew/bin/brew shellenv)"
    elif [ -x /usr/local/bin/brew   ]; then eval "$(/usr/local/bin/brew shellenv)"
    fi
  fi

  echo "Installing CLI tools via Homebrew…"
  brew install \
    zsh git curl wget stow \
    bat fd fzf zoxide \
    zellij neovim btop htop fastfetch
  # tldr is deprecated upstream — best-effort so a dead formula can't abort the
  # run. tlrc is the maintained `tldr` client.
  brew install tlrc || brew install tldr || echo "tldr unavailable — skipping"

  # fzf key-bindings + completion -> ~/.fzf.zsh (sourced by .zshrc). These flags
  # make it non-interactive; --no-update-rc stops it editing .zshrc (the repo
  # owns that file).
  "$(brew --prefix)/opt/fzf/install" --key-bindings --completion --no-update-rc >/dev/null

  clone_fzf_git
}

install_linux() {
  if ! command -v apt >/dev/null 2>&1; then
    echo "error: Linux app install targets Debian/Ubuntu (apt). Install the tools manually on this distro." >&2
    exit 1
  fi

  echo "Installing base packages via apt…"
  sudo apt update
  sudo apt install -y --no-install-recommends \
    neovim vim-nox btop htop curl wget tldr git zip unzip stow zsh cargo \
    zsh-autosuggestions zsh-syntax-highlighting
  sudo apt install -y --no-install-recommends rar unrar || true
  # fastfetch (modern neofetch); falls back to neofetch on older distros.
  sudo apt install -y fastfetch || sudo apt install -y neofetch || echo "fastfetch/neofetch unavailable — skipping"

  # fzf (vendored under ~/.cache/lk-tools/fzf; .zshrc adds its bin to PATH)
  if [ ! -d "$HOME/.cache/lk-tools/fzf" ]; then
    git clone --depth 1 https://github.com/junegunn/fzf.git "$HOME/.cache/lk-tools/fzf"
    "$HOME/.cache/lk-tools/fzf/install" --key-bindings --completion --no-update-rc
  fi
  clone_fzf_git

  # zoxide (upstream installer -> ~/.local/bin, already on PATH)
  command -v zoxide >/dev/null 2>&1 || \
    curl -sS https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | sh

  # bat + fd: upstream .deb so the binaries are named `bat`/`fd` (Debian ships
  # them as `batcat`/`fdfind`). amd64 assumed — adjust the URLs on arm64 boxes.
  if ! command -v bat >/dev/null 2>&1; then
    wget -qO /tmp/bat.deb https://github.com/sharkdp/bat/releases/download/v0.24.0/bat_0.24.0_amd64.deb
    sudo apt install -y /tmp/bat.deb
  fi
  if ! command -v fd >/dev/null 2>&1; then
    wget -qO /tmp/fd.deb https://github.com/sharkdp/fd/releases/download/v10.1.0/fd_10.1.0_amd64.deb
    sudo apt install -y /tmp/fd.deb
  fi

  # zellij: cargo build, fall back to apt
  command -v zellij >/dev/null 2>&1 || cargo install zellij || sudo apt install -y zellij
}

case "$(uname -s)" in
  Darwin) install_macos ;;
  Linux)  install_linux ;;
  *) echo "error: unsupported OS '$(uname -s)'." >&2; exit 1 ;;
esac

echo "Done. Open a new shell (or 'exec zsh') to pick everything up."
