#!/bin/bash
# Update the Cloudflare Worker's TUNNEL_URL to point to the current quick tunnel.
# Run this after starting a new tunnel.
set -euo pipefail

TUNNEL_URL_FILE="$HOME/.claude/channels/teams/tunnel-url.txt"

if [ $# -eq 1 ]; then
  TUNNEL_URL="$1"
elif [ -f "$TUNNEL_URL_FILE" ]; then
  TUNNEL_URL=$(cat "$TUNNEL_URL_FILE")
else
  echo "Usage: $0 [tunnel-url]"
  echo "Or save the URL to $TUNNEL_URL_FILE"
  exit 1
fi

echo "Updating worker TUNNEL_URL to: $TUNNEL_URL"

cd "$(dirname "$0")"
echo "$TUNNEL_URL" | npx wrangler secret put TUNNEL_URL 2>&1

echo "Done. Verify: curl -s https://rc-notifications-proxy.paperclip-gb.workers.dev/health"
echo "NOTE: this CF Worker is being decommissioned. New endpoint is the bun proxy at"
echo "      https://claude-cloud.tail053faf.ts.net/rc (see tools/rc-notifications-bun/)."
