#!/usr/bin/env bash
set -euo pipefail

echo 'Packrift MCP external activation selected-runs runner'
echo 'Release: PACKRIFT-MCP-EXTERNAL-ACTIVATION-BRIEF-R09'
echo 'Selected contact-ready runs: 1'
echo 'Expected tool-call lift: 4'
echo 'Projected external-qualified tool calls: 165/50'
echo 'This is a thin wrapper around existing hosted Packrift MCP first-run scripts. It does not create a CLI, checkout surface, or order.'
echo 'Run it from an external reviewer, directory owner, or real MCP host environment. Packrift self-runs do not prove the adoption goal.'

if [ "${PACKRIFT_EXTERNAL_ACTIVATION:-}" != "1" ]; then
  echo ''
  echo 'Refusing to execute until PACKRIFT_EXTERNAL_ACTIVATION=1 is set.'
  echo 'Copy one selected source command into the real external host or reviewer environment:'
  echo 'curl -sS '\''https://mcp.packrift.com/r/run/mcp_so/generic_streamable_http?format=sh'\'' | bash'
  echo ''
  echo 'Or explicitly run this selected-runs bundle: PACKRIFT_EXTERNAL_ACTIVATION=1 curl -sS https://mcp.packrift.com/ai/mcp-external-activation-brief-runner.sh | bash'
  exit 2
fi

run_selected_task() {
  local rank="$1"
  local source="$2"
  local script_url="$3"
  local expected_lift="$4"
  local tmp_file
  tmp_file="$(mktemp)"
  trap 'rm -f "$tmp_file"' RETURN
  echo ''
  echo "[$rank] Running Packrift MCP selected external activation: $source (expected tool-call lift: $expected_lift)"
  echo "Fetching existing first-run script: $script_url"
  curl -fsSL "$script_url" -o "$tmp_file"
  PACKRIFT_MCP_SESSION_ID="mcp-external-brief-${source}-$(date -u +%Y%m%dT%H%M%SZ)-$RANDOM" bash "$tmp_file"
}

run_selected_task '1' 'mcp_so' 'https://mcp.packrift.com/r/run/mcp_so/generic_streamable_http?format=sh' '4'

echo ''
echo 'Packrift MCP external activation selected-runs runner finished.'
echo 'Review proof after telemetry settles:'
echo 'https://mcp.packrift.com/ai/mcp-external-activation-brief.json'
echo 'https://mcp.packrift.com/ai/mcp-source-activation-queue.json'
echo 'https://mcp.packrift.com/ai/mcp-funnel-snapshot.json'
