#!/bin/sh
# $3 == 1 means branch switch (not file checkout), skip single-file checkouts.
#
# On branch switches where package definition files (pyproject.toml,
# setup.py, etc.) differ, triggers an editable reinstall.  Before
# reinstalling, the hook:
#   1. Stops the daemon (releases collab.exe lock on Windows)
#   2. Removes any stale non-editable copy  (site-packages/collab/)
#   3. Removes any pip rename orphans        (~ollab_runtime-*.dist-info/)
#   4. Runs pip install -e .
#   5. Restarts the daemon (background, non-blocking).
[ "$3" = "1" ] || exit 0
prev=$1; next=$2
changed=$(git diff --name-only "$prev" "$next" -- pyproject.toml setup.py setup.cfg requirements*.txt 2>/dev/null)
[ -z "$changed" ] && exit 0
pip_bin=".venv/bin/pip"
[ -f ".venv/Scripts/pip.exe" ] && pip_bin=".venv/Scripts/pip.exe"
if [ -f "$pip_bin" ]; then
  # Stop daemon to release collab.exe lock on Windows
  python_bin=".venv/bin/python"
  [ -f ".venv/Scripts/python.exe" ] && python_bin=".venv/Scripts/python.exe"
  if [ -f "$python_bin" ]; then
    "$python_bin" -m collab daemon-stop 2>/dev/null || true
  fi
  # Clean up stale non-editable copy and pip orphans before editable install
  site_pkgs=$("$pip_bin" show collab-runtime 2>/dev/null | grep Location | cut -d' ' -f2)
  if [ -n "$site_pkgs" ] && [ -d "$site_pkgs" ]; then
    rm -rf "$site_pkgs/collab" 2>/dev/null || true
    rm -rf "$site_pkgs/~ollab_runtime"-*.dist-info 2>/dev/null || true
  fi
  if "$pip_bin" install -e . --quiet; then
    echo "collab: editable install refreshed (branch switch)"
    # Restart daemon in background (non-blocking)
    if [ -f "$python_bin" ]; then
      "$python_bin" -m collab daemon-start 2>/dev/null &
    fi
  fi
fi
