#!/usr/bin/env bash
# Expose local nginx (:80) via Cloudflare Tunnel (e.g. https://dev.ultispace.fr).
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

if [[ ! -f .env ]]; then
  echo "Missing .env — run: cp .env.example .env" >&2
  exit 1
fi

set -a
# shellcheck disable=SC1091
source .env
set +a

if [[ -z "${CLOUDFLARE_TUNNEL_TOKEN:-}" ]]; then
  echo "CLOUDFLARE_TUNNEL_TOKEN is not set in .env" >&2
  exit 1
fi

PUBLIC_URL="${CLOUDFLARE_TUNNEL_PUBLIC_URL:-https://dev.ultispace.fr}"
echo "Cloudflare tunnel → local stack (nginx :80)"
echo "Public URL: ${PUBLIC_URL}"
echo "Start stack first: ./deploy/compose-up.sh up -d"

exec cloudflared tunnel --loglevel error run --token "$CLOUDFLARE_TUNNEL_TOKEN"
