#!/usr/bin/env bash
# Modern CLI tools — what notes/aliases.zsh assumes + a fullstack-dev kit.
set -euo pipefail
# shellcheck source=./lib.sh
source "$(cd "$(dirname "$0")" && pwd)/lib.sh"
require_debian
require_not_root

# Requested in the project brief.
apt_install bat eza fd-find ripgrep fzf zoxide ncdu keychain

# Useful extras for a fullstack dev workflow.
apt_install jq yq httpie make tree htop tmux direnv dnsutils unzip rsync

# Debian renames `bat`→`batcat` and `fd`→`fdfind` to avoid binary
# conflicts. notes/aliases.zsh aliases the Debian names back, but a
# direct binary on PATH is more robust (works for non-interactive
# scripts and other shells). Symlink in ~/.local/bin.
mkdir -p "$HOME/.local/bin"
for pair in "batcat:bat" "fdfind:fd"; do
  src="${pair%%:*}"
  dst="${pair##*:}"
  src_path="$(command -v "$src" 2>/dev/null || true)"
  if [[ -z "$src_path" ]]; then
    warn "$src not found on PATH — skipping symlink."
    continue
  fi
  if [[ -L "$HOME/.local/bin/$dst" && "$(readlink "$HOME/.local/bin/$dst")" == "$src_path" ]]; then
    skip "$HOME/.local/bin/$dst already linked to $src."
  elif [[ -e "$HOME/.local/bin/$dst" ]]; then
    skip "$HOME/.local/bin/$dst exists (not a managed symlink) — leaving as-is."
  else
    ln -s "$src_path" "$HOME/.local/bin/$dst"
    ok "Linked $HOME/.local/bin/$dst → $src."
  fi
done

# Make sure ~/.local/bin is on PATH for both bash and zsh.
for rcfile in "$HOME/.bashrc" "$HOME/.zshrc"; do
  [[ -f "$rcfile" ]] || touch "$rcfile"
  ensure_line 'export PATH="$HOME/.local/bin:$PATH"' "$rcfile"
done

ok "CLI tools installed."
