#!/bin/sh

set -eu

OS="$(uname -s)"

have() {
  command -v "$1" >/dev/null 2>&1
}

check_prerequisites() {
  case "$OS" in
  Darwin)
    if ! have brew; then
      echo "Homebrew ist nicht installiert."
      exit 1
    fi
    ;;
  FreeBSD)
    if ! have doas; then
      echo "doas ist nicht installiert."
      exit 1
    fi
    ;;
  *)
    echo "Nicht unterstütztes Betriebssystem: $OS"
    exit 1
    ;;
  esac
}

setup_homebrew_env() {
  case "$OS" in
  Darwin)
    export HOMEBREW_NO_AUTO_UPDATE=1
    export HOMEBREW_NO_INSTALL_CLEANUP=1
    ;;
  esac
}

install_pkg() {
  binary="$1"
  package="$2"

  if have "$binary"; then
    printf "✓ %-32s vorhanden\n" "$binary"
    return
  fi

  printf "→ Installiere %s\n" "$package"

  case "$OS" in
  Darwin)
    brew install "$package"
    ;;
  FreeBSD)
    doas pkg install -y "$package"
    ;;
  esac
}

install_npm() {
  binary="$1"
  package="$2"

  if have "$binary"; then
    printf "✓ %-32s vorhanden\n" "$binary"
    return
  fi

  if ! have npm; then
    echo "npm wurde nicht gefunden."
    exit 1
  fi

  printf "→ Installiere npm-Paket %s\n" "$package"
  npm install -g "$package"
}

install_pip() {
  python="$1"
  package="$2"

  if "$python" -c "import $package" >/dev/null 2>&1; then
    printf "✓ %-32s vorhanden\n" "$package"
    return
  fi

  printf "→ Installiere Python-Paket %s\n" "$package"
  env \
    -u C_INCLUDE_PATH \
    -u CPLUS_INCLUDE_PATH \
    -u LIBRARY_PATH \
    "$python" -m pip install --no-cache-dir "$package"
}

setup_python_provider() {
  VENV="$HOME/.local/venv/nvim"
  PYTHON="${PYTHON:-python3}"

  if ! have "$PYTHON"; then
    echo "Python wurde nicht gefunden."
    exit 1
  fi

  if [ ! -x "$VENV/bin/python" ]; then
    printf "→ Erzeuge Python-Venv %s\n" "$VENV"

    mkdir -p "$(dirname "$VENV")"
    "$PYTHON" -m venv "$VENV"
  fi

  install_pip "$VENV/bin/python" pynvim
}

# === Voraussetzungen ===

check_prerequisites
setup_homebrew_env

install_pkg curl curl
install_pkg git git
install_pkg node node
install_pkg python3 python

# === Provider ===

install_npm neovim-node-host neovim
setup_python_provider

# === Formatter ===

install_pkg gawk gawk
install_pkg jq jq
install_npm prettier prettier
install_pkg ruff ruff
install_pkg shfmt shfmt
install_pkg stylua stylua
install_pkg yamlfmt yamlfmt

case "$OS" in
Darwin)
  install_pkg clang-format clang-format
  ;;
FreeBSD)
  install_pkg xmllint libxml2
  ;;
esac

# === Tools ===

install_pkg rg ripgrep

case "$OS" in
Darwin)
  install_pkg gmake make
  install_pkg fd fd
  install_pkg tree-sitter tree-sitter
  ;;
FreeBSD)
  install_pkg gmake gmake
  install_pkg fd fd-find
  install_pkg tree-sitter tree-sitter-cli
  ;;
esac

# === Language Server ===

install_npm basedpyright-langserver basedpyright
install_npm bash-language-server bash-language-server

case "$OS" in
Darwin)
  install_pkg shellcheck shellcheck
  ;;
FreeBSD)
  install_pkg shellcheck hs-ShellCheck
  ;;
esac

install_pkg clangd llvm
install_npm vscode-html-language-server vscode-langservers-extracted
install_npm vscode-json-language-server vscode-langservers-extracted
install_pkg lua-language-server lua-language-server
install_npm prisma-language-server @prisma/language-server
install_npm vtsls @vtsls/language-server

echo
echo "Bootstrap abgeschlossen."
