#!/usr/bin/env bash
set -euo pipefail

# PATH shim for uv — intercepts mutating `uv pip` (legacy interface) and
# passes everything else through to the real uv binary. Read-only
# introspection (list/show/tree/check) stays available for diagnostics;
# the standard only proscribes the legacy install/modify path.

if [[ "${1:-}" == "pip" ]]; then
  case "${2:-}" in
    list | show | tree | check) ;; # read-only — pass through
    *)
      echo "ERROR: \`uv pip ${2:-}\` is the legacy interface. Use instead:" >&2
      echo "  uv add <package>       # instead of uv pip install" >&2
      echo "  uv remove <package>    # instead of uv pip uninstall" >&2
      echo "  uv sync                # instead of uv pip sync" >&2
      echo "  uv export              # instead of uv pip freeze" >&2
      exit 1
      ;;
  esac
fi

# Find the real uv by skipping this shim's directory in PATH
shim_dir="$(cd "$(dirname "$0")" && pwd)"
IFS=: read -ra path_entries <<<"${PATH:-}"
for dir in "${path_entries[@]}"; do
  resolved="$(cd "$dir" 2>/dev/null && pwd)" || continue
  [[ "$resolved" == "$shim_dir" ]] && continue
  if [[ -x "$dir/uv" ]]; then
    exec "$dir/uv" "$@"
  fi
done

echo "ERROR: real uv binary not found on PATH" >&2
exit 127
