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

LOCAL_PATHS=".claude/project-paths.local.md"

if [ ! -f "$LOCAL_PATHS" ]; then
  echo "ERROR: $LOCAL_PATHS not found. Run /eck:init-project to generate it." >&2
  exit 2
fi

TOPLEVEL=$(git rev-parse --show-toplevel 2>/dev/null || echo "")
if [ -z "$TOPLEVEL" ]; then
  exit 0
fi

PRIMARY_PATH=$(grep -E '^\|[[:space:]]*PRIMARY_PATH[[:space:]]*\|' "$LOCAL_PATHS" | awk -F'|' '{gsub(/^[[:space:]]+|[[:space:]]+$/, "", $3); print $3}' | head -1)

if [ -n "$PRIMARY_PATH" ] && [ "$PRIMARY_PATH" != "$TOPLEVEL" ]; then
  REAL_PRIMARY=$(cd "$PRIMARY_PATH" 2>/dev/null && pwd -P || echo "$PRIMARY_PATH")
  REAL_TOPLEVEL=$(cd "$TOPLEVEL" && pwd -P)
  if [ "$REAL_PRIMARY" != "$REAL_TOPLEVEL" ]; then
    echo "WARNING: PRIMARY_PATH is stale (mismatch). Expected: $TOPLEVEL, got: $PRIMARY_PATH. Run /eck:init-project --refresh" >&2
  fi
fi

exit 0
