#!/usr/bin/env bash

# chezmoi completion zsh --output ~/.config/zsh/completions/_chezmoi

# Save this file to:
# ~/.local/share/chezmoi/run_once_after_install-completions.sh
# (make sure it has +x in the source state)

# Install chezmoi shell completions for bash, zsh, fish
# Idempotent — safe to run multiple times
# Works on Debian / Ubuntu (and derivatives)

set -euo pipefail

CHEZMOI_VERSION=$(chezmoi --version 2>/dev/null | awk '{print $2}' || echo "unknown")

echo "Detected chezmoi version: ${CHEZMOI_VERSION:-not found}"
if [[ "$CHEZMOI_VERSION" == "unknown" ]]; then
  echo "Error: chezmoi not found in PATH. Please install it first."
  exit 1
fi

# ────────────────────────────────────────────────
# Helper: generate and write completion if needed
# ────────────────────────────────────────────────
install_completion() {
  local shell="$1"
  local dest="$2"
  local dest_dir

  dest_dir=$(dirname "$dest")

  # Create directory if missing (common for fresh ~/.config/zsh/completions)
  mkdir -p "$dest_dir"

  echo "→ Installing completion for $shell → $dest"

  # Generate fresh completion
  if ! chezmoi completion "$shell" --output="$dest.tmp" 2>/dev/null; then
    echo "  Failed to generate $shell completion"
    rm -f "$dest.tmp"
    return 1
  fi

  # Only replace if different (keeps mtime unchanged when identical → nice for chezmoi itself)
  if [[ -f "$dest" ]] && cmp -s "$dest.tmp" "$dest"; then
    echo "  Already up-to-date"
    rm -f "$dest.tmp"
  else
    mv "$dest.tmp" "$dest"
    echo "  Written → $dest"
  fi
}

# ────────────────────────────────────────────────
# bash completion (most common locations on Debian)
# ────────────────────────────────────────────────
# Preferred: /usr/share/bash-completion/completions/ (system-wide)
# Fallback:  ~/.local/share/bash-completion/completions/ (user-only)

BASH_COMPLETION_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion/completions"
mkdir -p "$BASH_COMPLETION_DIR"

install_completion bash "$BASH_COMPLETION_DIR/chezmoi"

# Also try system-wide (requires sudo) — uncomment if you want it
# sudo install_completion bash /usr/share/bash-completion/completions/chezmoi

# ────────────────────────────────────────────────
# zsh completion
# ────────────────────────────────────────────────
# Common user location (you already used this one)
ZSH_COMPLETION_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/zsh/completions"
install_completion zsh "$ZSH_COMPLETION_DIR/_chezmoi"

# Make sure your .zshrc sources the directory (add if missing)
ZSHRC="$HOME/.zshrc"
if ! grep -q "fpath.*zsh/completions" "$ZSHRC" 2>/dev/null; then
  echo "# Load completions (added by chezmoi install script)" >>"$ZSHRC"
  echo 'fpath=("${XDG_CONFIG_HOME:-$HOME/.config}/zsh/completions" $fpath)' >>"$ZSHRC"
  echo "autoload -Uz compinit && compinit" >>"$ZSHRC"
  echo "→ Added zsh completions path to ~/.zshrc (reload with 'source ~/.zshrc')"
fi

# ────────────────────────────────────────────────
# fish completion
# ────────────────────────────────────────────────
# Fish looks in ~/.config/fish/completions/
FISH_COMPLETION_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/fish/completions"
install_completion fish "$FISH_COMPLETION_DIR/chezmoi.fish"

echo ""
echo "Done! Completions installed for:"
echo "  • bash  → $BASH_COMPLETION_DIR/chezmoi"
echo "  • zsh   → $ZSH_COMPLETION_DIR/_chezmoi"
[[ -d "$FISH_COMPLETION_DIR" ]] && echo "  • fish  → $FISH_COMPLETION_DIR/chezmoi.fish"

echo ""
echo "Next steps:"
echo "  • Bash → open new terminal or run:  source /etc/profile.d/bash_completion.sh"
echo "  • Zsh  → run:  source ~/.zshrc     (or open new terminal)"
echo "  • Fish → open new terminal"
echo ""
echo "Test with:   chezmoi <tab><tab>"
