#!/bin/sh
set -eu

case "$(uname)" in
FreeBSD)
  echo "FreeBSD detected – using system Python + pkg pynvim"
  ;;
Darwin)
  VENV="$HOME/.local/venv/nvim"
  PYTHON="${PYTHON:-python3}"

  # Python vorhanden?
  if ! command -v "$PYTHON" >/dev/null 2>&1; then
    echo "ERROR: python3 not found"
    exit 1
  fi

  # venv anlegen (falls nicht vorhanden)
  if [ ! -x "$VENV/bin/python" ]; then
    echo "Creating Neovim Python venv at $VENV"
    "$PYTHON" -m venv "$VENV"
  fi

  # pip sicherstellen
  "$VENV/bin/python" -m ensurepip --upgrade >/dev/null 2>&1 || true

  # pynvim installieren / aktualisieren
  "$VENV/bin/python" -m pip install --upgrade pip pynvim

  echo "Neovim Python provider ready:"
  "$VENV/bin/python" -c 'import pynvim; print("pynvim:", pynvim.__version__)'
  ;;
esac
