#!/usr/bin/env bash
# sync-agent-repos.sh — 同步 GitHub Agent 框架到 Gitea（排除大资源文件）
set -eo pipefail

GITEA="https://git666.u7f.cn"
GITEA_USER="sawz"
GITEA_PASS="45324622ww"
GITEA_ORG="Github"
PROXY="socks5://127.0.0.1:12002"
WORK_DIR="/tmp/gitea-sync"
mkdir -p "$WORK_DIR"

REPO_LIST=(
  "MetaGPT|FoundationAgents/MetaGPT"
  "CrewAI|crewAIInc/crewAI"
  "AutoGen|microsoft/autogen"
  "Mastra|mastra-ai/mastra"
  "MCP-Use|mcp-use/mcp-use"
  "RagaAI-Catalyst|raga-ai-hub/RagaAI-Catalyst"
)

EXCLUDE_PATTERNS=(
  "*.gif" "*.mp4" "*.mov" "*.avi" "*.webm" "*.webp"
  "*.png" "*.jpg" "*.jpeg" "*.svg" "*.ico"
  "*.woff" "*.woff2" "*.ttf" "*.eot"
  "*.zip" "*.tar" "*.gz" "*.bz2" "*.7z" "*.rar"
  "*.pdf" "*.whl" "*.egg" "*.jar"
  "docs/images/*" "docs/*/images/*" "static/images/*"
)

sync_repo() {
  local name="$1" github_path="$2" clone_dir="$WORK_DIR/$name"
  echo "=== [$name] ==="

  # 检查是否已存在
  if curl -sf -u "$GITEA_USER:$GITEA_PASS" "$GITEA/api/v1/repos/$GITEA_ORG/$name" >/dev/null 2>&1; then
    # 增量更新
    local remote_sha=$(curl -sf --proxy "$PROXY" "https://api.github.com/repos/$github_path" 2>/dev/null | python3 -c "import json,sys; print(json.load(sys.stdin).get('pushed_at',''))" 2>/dev/null)
    local local_sha=$(curl -sf -u "$GITEA_USER:$GITEA_PASS" "$GITEA/api/v1/repos/$GITEA_ORG/$name/commits?limit=1" 2>/dev/null | python3 -c "import json,sys; d=json.load(sys.stdin); print(d[0]['commit']['committer']['date'] if d else '')" 2>/dev/null)
    if [ "$remote_sha" = "$local_sha" ] 2>/dev/null; then
      echo "  ℹ️  无更新"; return 0
    fi
    echo "  🔄 检测到更新，重新同步..."
    curl -sf -u "$GITEA_USER:$GITEA_PASS" -X DELETE "$GITEA/api/v1/repos/$GITEA_ORG/$name" >/dev/null 2>&1
  fi

  rm -rf "$clone_dir"
  echo "  📥 克隆中..."
  GIT_HTTP_PROXY="$PROXY" git clone --depth=1 "https://github.com/$github_path.git" "$clone_dir" 2>/dev/null
  cd "$clone_dir"

  echo "  🧹 排除大文件..."
  for p in "${EXCLUDE_PATTERNS[@]}"; do
    find . -not -path './.git/*' -name "$p" -type f -delete 2>/dev/null
  done
  find . -not -path './.git/*' -type d -empty -delete 2>/dev/null

  curl -sf -u "$GITEA_USER:$GITEA_PASS" -X POST "$GITEA/api/v1/orgs/$GITEA_ORG/repos" \
    -H "Content-Type: application/json" -d "{\"name\":\"$name\",\"private\":false,\"auto_init\":false}" 2>/dev/null || true

  rm -rf .git && git init -q && git add -A
  git commit -q -m "sync: code-only from github.com/$github_path" --allow-empty
  git remote add origin "$GITEA/$GITEA_ORG/$name.git"
  git push -u origin HEAD:main --force 2>/dev/null || git push -u origin HEAD:master --force 2>/dev/null

  local size=$(du -sh . 2>/dev/null | cut -f1)
  echo "  ✅ $name — $size"
  cd "$WORK_DIR"
  rm -rf "$clone_dir"
}

echo "# Agent 框架同步 — $(date '+%Y-%m-%d %H:%M')"
for entry in "${REPO_LIST[@]}"; do
  sync_repo "${entry%%|*}" "${entry#*|}" 2>&1
done
echo "🎉 完成 — $(date '+%H:%M')"
