#!/bin/bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
OPTCTL="$ROOT/bin/optctl"
"$OPTCTL" test status >/dev/null
"$OPTCTL" discover >/dev/null
"$OPTCTL" gates >/dev/null
"$OPTCTL" control check >/dev/null
AUTO="$(python3 -c 'import json,sys;print(str(json.load(open(sys.argv[1])).get("auto_shadow",False)).lower())' "$ROOT/config/agent.json" 2>/dev/null || echo false)"
PCT="$(python3 -c 'import json,sys;print(int(json.load(open(sys.argv[1])).get("shadow_start_percent",5)))' "$ROOT/config/agent.json" 2>/dev/null || echo 5)"
if [ "$AUTO" = "true" ]; then
  "$OPTCTL" shadow start --percent "$PCT" >/dev/null || true
fi
