#!/bin/bash
# Restore .mcp.json from golden copy if it was overwritten.
# Claude Code rewrites .mcp.json when MCP server configs change,
# which can drop custom env vars (RC, AMD creds in teams-channel).
set -euo pipefail

REPO_DIR="$(cd "$(dirname "$0")/.." && pwd)"
MCP_FILE="$REPO_DIR/.mcp.json"
GOLDEN="$REPO_DIR/.mcp.json.golden"

if [ ! -f "$GOLDEN" ]; then
  echo "restore-mcp: no golden copy found at $GOLDEN"
  exit 0
fi

# Check if teams-channel still has RINGCENTRAL_CLIENT_ID
if grep -q "RINGCENTRAL_CLIENT_ID" "$MCP_FILE" 2>/dev/null; then
  exit 0
fi

echo "restore-mcp: .mcp.json missing RC creds, restoring from golden copy"
cp "$GOLDEN" "$MCP_FILE"
echo "restore-mcp: restored"
