#!/usr/bin/env bash
set -euo pipefail
REF='75530dc4da76480f735bb016bef07888c350de31'
REPO='https://github.com/rpuls/my-own-suite.git'
[ "$(id -u)" -eq 0 ] || { echo 'Run with sudo bash.' >&2; exit 1; }
. /etc/os-release
[ "${ID:-}" = ubuntu ] && [ "${VERSION_ID:-}" = 24.04 ] || { echo 'MOS requires Ubuntu 24.04.' >&2; exit 1; }
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y ca-certificates curl git
IP="$(curl -fsS --proto '=https' --tlsv1.2 --max-time 15 https://api.ipify.org)"
printf '%s' "$IP" | grep -Eq '^([0-9]{1,3}\.){3}[0-9]{1,3}$' || { echo 'Could not discover public IPv4.' >&2; exit 1; }
if ! command -v node >/dev/null || [ "$(node -p 'Number(process.versions.node.split(".")[0])' 2>/dev/null || echo 0)" -lt 22 ]; then
  curl -fsSL --proto '=https' --tlsv1.2 https://deb.nodesource.com/setup_22.x | bash -
  apt-get install -y nodejs
fi
WORK="$(mktemp -d)"; trap 'rm -rf "$WORK"' EXIT
git -C "$WORK" init --quiet
git -C "$WORK" remote add origin "$REPO"
git -C "$WORK" fetch --quiet --depth 1 origin "$REF"
git -C "$WORK" checkout --quiet --detach FETCH_HEAD
[ "$(git -C "$WORK" rev-parse HEAD)" = "$REF" ] || { echo 'Source verification failed.' >&2; exit 1; }
node "$WORK/scripts/installers/render-bootstrap.cjs" --target shell --front-door ssh-bootstrap --repo-url "$REPO" --repo-ref "$REF" --public-ipv4 "$IP" | bash
