#!/bin/sh
# dhai edge2 stage 0, served publicly at https://e2.dhai.uk (Cloudflare Worker, verified TLS).
# Contains no secret. It fetches the real bootstrap from wh through the household's dryad port
# on the home LAN, TLS-verified against dryad's tailnet certificate, and runs it.
# Typed by davidad at the Edge2 console:   curl -fsSL https://e2.dhai.uk | sudo sh
set -eu
stage0() {
  H=dryad.gaur-betta.ts.net:8470
  A=192.168.4.93
  [ "$(id -u)" = 0 ] || { echo "edge2: FAILED: not root"; echo "edge2: next: type the line exactly, ending in | sudo sh"; exit 1; }
  T=$(mktemp /tmp/edge2-bootstrap.XXXXXX)
  trap 'rm -f "$T"' EXIT
  echo "edge2: fetching the dhai bootstrap from dryad ($A)"
  if ! curl -fsS --max-time 30 --retry 3 --retry-delay 2 --resolve "$H:$A" -o "$T" "https://$H/edge2"; then
    echo "edge2: FAILED: could not fetch the bootstrap from dryad's port"
    echo "edge2: next: photo of this screen to the household Signal thread; dhai checks the dryad port"
    exit 1
  fi
  sh "$T" "$@"
}
stage0 "$@"
