#!/bin/bash
#
# Batch create OpenCog repositories via P2P admin protocol
#
# Usage:
# SERVER_KEY=3cf3266a... bash scripts/create-opencog-repos.sh
#
# Prerequisites:
# 1. pear-git server running with P2P enabled
# 2. Noise key linked to admin user: pgh identity link-noise --token <token>
# 3. SERVER_KEY set (or in ~/.config/pear-git/pgh.json)
#
set -u
OPENCOG_REPOS_DIR="/storage/projects/code/hypercore-kb/opencog-kb/repos"
SERVER_KEY="${SERVER_KEY:-}"
if [ -z "$SERVER_KEY" ]; then
# Try to read from config
SERVER_KEY=$(python3 -c "import json; print(json.load(open('$HOME/.config/pear-git/pgh.json'))['serverKey'])" 2>/dev/null || true)
fi
if [ -z "$SERVER_KEY" ]; then
echo "Error: SERVER_KEY not set and not in config"
echo "Usage: SERVER_KEY=<hex> bash $0"
exit 1
fi
echo "Server key: ${SERVER_KEY:0:16}..."
echo "Repos dir: $OPENCOG_REPOS_DIR"
echo ""
# All OpenCog repos to import
REPOS=(
agents
atomese-simd
atomspace
atomspace-cog
atomspace-rocks
atomspace-storage
cogserver
cogutil
CompactHash
docker
evidence
generate
learn
lg-atomese
link-grammar
matrix
rocca
sensory
SimpleGPUHashTable
ure
)
SUCCESS=0
FAILED=0
for repo in "${REPOS[@]}"; do
REPO_PATH="$OPENCOG_REPOS_DIR/$repo"
REPO_NAME="opencog-$repo"
if [ ! -d "$REPO_PATH" ]; then
echo "SKIP $REPO_NAME (path not found: $REPO_PATH)"
continue
fi
echo -n "CREATE $REPO_NAME ... "
OUTPUT=$(bare /storage/projects/code/hypercore-kb/hyper-fleeky/pear-git/bin/pgh repo create "$REPO_NAME" \
--server-key "$SERVER_KEY" \
--from-path "$REPO_PATH" \
--visibility public \
--category opencog \
--json 2>&1)
if echo "$OUTPUT" | python3 -c "import sys,json; d=json.load(sys.stdin); exit(0 if d.get('success') else 1)" 2>/dev/null; then
echo "OK"
SUCCESS=$((SUCCESS + 1))
else
ERROR=$(echo "$OUTPUT" | python3 -c "import sys,json; print(json.load(sys.stdin).get('error','unknown'))" 2>/dev/null || echo "$OUTPUT")
echo "FAILED: $ERROR"
FAILED=$((FAILED + 1))
fi
done
echo ""
echo "Done: $SUCCESS created, $FAILED failed (${#REPOS[@]} total)"