#!/bin/sh
# Sync CLAUDE.md to .github/copilot-instructions.md so both Claude Code and
# GitHub Copilot use the same project instructions.
cp CLAUDE.md .github/copilot-instructions.md
git add .github/copilot-instructions.md

# Regenerate Copilot CLI agents from Claude Code agents so both tools share
# one source of truth. Each .claude/agents/<name>.md produces
# .github/agents/<name>.agent.md with converted frontmatter and verbatim body.
mkdir -p .github/agents
rm -f .github/agents/*.agent.md
python3 - <<'PY'
import pathlib
import re
import sys

TOOL_MAP = {
    "read": "read",
    "notebookread": "read",
    "edit": "edit",
    "multiedit": "edit",
    "write": "edit",
    "notebookedit": "edit",
    "grep": "search",
    "glob": "search",
    "bash": "execute",
    "shell": "execute",
    "powershell": "execute",
    "task": "agent",
    "websearch": "web",
    "webfetch": "web",
    "todowrite": "todo",
}

src_dir = pathlib.Path(".claude/agents")
dst_dir = pathlib.Path(".github/agents")
frontmatter_re = re.compile(r"^---\n(.*?)\n---\n?(.*)$", re.DOTALL)

for src in sorted(src_dir.glob("*.md")):
    text = src.read_text()
    m = frontmatter_re.match(text)
    if not m:
        sys.exit(f"{src}: missing frontmatter")
    fm_raw, body = m.group(1), m.group(2)

    fm = {}
    key = None
    for line in fm_raw.splitlines():
        if re.match(r"^[A-Za-z_][\w-]*:", line):
            key, _, val = line.partition(":")
            key = key.strip()
            fm[key] = val.strip()
        elif key is not None:
            fm[key] = (fm[key] + " " + line.strip()).strip()

    description = fm.get("description", "").strip()
    tools_raw = fm.get("tools", "").strip()

    aliases = []
    for name in re.split(r"[,\s]+", tools_raw):
        if not name:
            continue
        alias = TOOL_MAP.get(name.lower())
        if alias and alias not in aliases:
            aliases.append(alias)

    header = ["---"]
    if description:
        header.append("description: |")
        for bl in description.splitlines() or [description]:
            header.append("  " + bl)
    if aliases:
        header.append("tools: [" + ", ".join(f'"{a}"' for a in aliases) + "]")
    header.append("---")

    dst = dst_dir / f"{src.stem}.agent.md"
    dst.write_text("\n".join(header) + "\n" + body)
PY

git add .github/agents/

# Strip the volatile per-cell execution timestamps (metadata.execution -- the dates a cell
# last ran) from staged example notebooks so they never appear in version control.
# execution_count and cell outputs are intentionally kept: they record that the notebook ran
# and what it produced, which the example tests verify.
python3 - <<'PY'
import json
import pathlib
import subprocess

result = subprocess.run(
    ["git", "diff", "--cached", "--name-only", "--diff-filter=ACM"],
    capture_output=True,
    text=True,
)
staged = [p for p in result.stdout.splitlines() if p.startswith("examples/") and p.endswith(".ipynb")]

for rel_path in staged:
    nb_path = pathlib.Path(rel_path)
    if not nb_path.exists():
        continue
    with nb_path.open(encoding="utf-8") as f:
        nb = json.load(f)
    changed = False
    for cell in nb.get("cells", []):
        if cell.get("cell_type") != "code":
            continue
        if "execution" in cell.get("metadata", {}):
            del cell["metadata"]["execution"]
            changed = True
    if changed:
        with nb_path.open("w", encoding="utf-8") as f:
            json.dump(nb, f, indent=1, ensure_ascii=False)
            f.write("\n")
        subprocess.run(["git", "add", rel_path], check=True)
PY
