#!/usr/bin/env bash
# Run any crew in this folder against YOUR local Ollama. Nothing leaves your machine.
# Usage: ./run_crew.sh <crew_dir> [run|train|replay|test]
# One-time setup: see ../SOVEREIGN_SETUP.md
set -euo pipefail
cd "$(dirname "$0")"
CREW="${1:?usage: ./run_crew.sh <crew_dir> [cmd]}"
CMD="${2:-run}"

if ! curl -s --max-time 3 http://127.0.0.1:11434/api/version >/dev/null; then
  echo "Ollama isn't running on this machine (http://127.0.0.1:11434)."
  echo "Start it (usually: 'ollama serve' or it runs as a service) and re-run."
  exit 1
fi
if ! curl -s http://127.0.0.1:11434/api/tags | grep -q 'gpt-4o-mini'; then
  echo "Model alias missing. Run once:"
  echo "  ollama pull qwen2.5:14b && ollama cp qwen2.5:14b gpt-4o-mini"
  exit 1
fi

# Point the crews' OpenAI-style client at your local model server. No cloud.
export OPENAI_API_BASE="http://127.0.0.1:11434/v1"
export OPENAI_BASE_URL="$OPENAI_API_BASE"
export OPENAI_API_KEY="local-ollama"
# Web-search tools are optional cloud SaaS; placeholders make them fail closed.
# Export your own real key BEFORE running if you want live search.
export EXA_API_KEY="${EXA_API_KEY:-disabled-sovereign}"
export BRAVE_API_KEY="${BRAVE_API_KEY:-disabled-sovereign}"
export SERPER_API_KEY="${SERPER_API_KEY:-disabled-sovereign}"
export FIRECRAWL_API_KEY="${FIRECRAWL_API_KEY:-disabled-sovereign}"
export OTEL_SDK_DISABLED=true
export CREWAI_DISABLE_TELEMETRY=true

PKG=$(ls "$CREW/src" | head -1)
echo "[sovereign] $CREW ($PKG) -> $CMD on your local model"
cd "$CREW"
PYTHONPATH=src ../.venv/bin/python -c "from $PKG.main import $CMD; $CMD()"
