#!/usr/bin/env bash
# =============================================================================
#  TASLEEM REPO WIRING KIT — FREE GitHub repos ko "bina load" wire karo
#  • SHALLOW clone only (--depth 1) = kam disk, kam network, server load ZERO
#  • KOI pip install NAHI, KOI service start NAHI — sirf disk pe aati hain
#  • Registry: ~/tasleem_repos/registry.json (TASLEEM UI '⭐ Repos' se linked)
#
#  Run:  bash WIRE_REPOS.sh                    (default curated 6 repos)
#        bash WIRE_REPOS.sh owner/repo [owner/repo2 ...]   (apni pasand)
# =============================================================================
DEST="${TASLEEM_REPOS:-$HOME/tasleem_repos}"
REG="$DEST/registry.json"
mkdir -p "$DEST"

if [ $# -gt 0 ]; then REPOS=("$@"); else
  REPOS=(
    "microsoft/markitdown"        # files→markdown (reports, docs)
    "unclecode/crawl4ai"          # AI-friendly web crawler (research)
    "crewAIInc/crewAI"            # multi-agent orchestration (squads reference)
    "yt-dlp/yt-dlp"               # YouTube resolve/download (media squad)
    "searxng/searxng"             # free meta-search engine (live research)
    "openbb-finance/OpenBB"       # market/gold research toolkit
  )
fi

command -v git >/dev/null 2>&1 || { echo "!! git nahi hai: sudo apt install -y git"; exit 1; }
echo "TASLEEM REPO WIRING → $DEST  (shallow depth-1, no install, zero load)"
echo ""

for R in "${REPOS[@]}"; do
  NAME="$(basename "$R")"
  TGT="$DEST/$NAME"
  if [ -d "$TGT/.git" ]; then
    echo "  SKIP  $R  (already wired)"
    continue
  fi
  echo "  WIRE  $R ..."
  if git clone --depth 1 --single-branch "https://github.com/$R.git" "$TGT" 2>/dev/null; then
    SZ=$(du -sh "$TGT" 2>/dev/null | cut -f1)
    echo "        OK — $SZ  → $TGT"
  else
    echo "        FAIL (naam/repo check karo ya net slow hai)"; continue
  fi
done

# registry likho (TASLEEM/server isko padh sakta hai — read-only)
python3 - "$DEST" "$REG" <<'PYEOF'
import json,os,sys,datetime
dest,reg=sys.argv[1],sys.argv[2]
items=[]
for d in sorted(os.listdir(dest)):
    p=os.path.join(dest,d)
    if os.path.isdir(p) and os.path.isdir(os.path.join(p,".git")):
        items.append({"name":d,"path":p,
                      "readme":os.path.exists(os.path.join(p,"README.md")),
                      "size_mb":round(sum(f.stat().st_size for r,_,fs in os.walk(p) for f in fs)/1e6,1),
                      "wired_at":datetime.datetime.now().isoformat(timespec="seconds")})
json.dump(items,open(reg,"w"),indent=1)
print("\nregistry:",reg,"→",len(items),"repos wired")
PYEOF

echo ""
echo "DONE. Notes:"
echo " • Ye repos sirf DISK pe hain — CPU/RAM pe ZERO load (jab tak na chalao)"
echo " • Koi pip install nahi hua — jab zaroorat ho tab module-wise chalao"
echo " • Repo hatana ho: rm -rf $DEST/<repo>  (registry next run pe update)"
