#!/usr/bin/env bash
# Bootstrap a fresh Ubuntu 22.04 GPU VM as the headless YouTube gateway (see GATEWAY_VM.md).
# Run ON the VM after cloning the repo from the scp'd bundle:  ~/TRAP/v3/broadcast/deploy/setup_gateway_vm.sh
# Idempotent — safe to re-run. Reboot once after the NVIDIA driver install.
set -euo pipefail

# `$HOME` (used for the cloud-sql-proxy path below) is unset in a non-interactive/root context such as a
# GCE startup-script, and `set -u` would abort there — so default it before any expansion.
export HOME="${HOME:-/root}"

REPO="$(cd "$(dirname "$0")/../../.." && pwd)"

sudo apt-get update
# linux-headers are needed for the NVIDIA .run --dkms kernel-module build (else "module was not created").
# speech-dispatcher + espeak-ng are what give Chromium a VOICE on Linux: the Web Speech API is a front
# end to speech-dispatcher, and with neither installed `speechSynthesis.getVoices()` returns an empty
# list and `speak()` is a silent no-op — no error, no warning, a broadcast with no commentary. This
# script provisioned the VM for `gcp_gateway.py`, whose module docstring lists both as REQUIRED, and
# installed neither; the stream went out silent (Ulf 2026-08-11). Windows and macOS ship voices, so
# this is a fault that exists only on the gateway and only in production.
sudo apt-get install -y xvfb pulseaudio ffmpeg xdotool git python3-venv python3-pip \
    speech-dispatcher espeak-ng "linux-headers-$(uname -r)"

# Google Chrome (snap-free; gcp_gateway runs it with --chromium google-chrome)
if ! command -v google-chrome >/dev/null 2>&1; then
    curl -fsSL https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -o /tmp/chrome.deb
    sudo apt-get install -y /tmp/chrome.deb
fi

# NVIDIA driver — enables NVENC (ffmpeg h264_nvenc) and GPU WebGL. Reboot after first install.
if ! command -v nvidia-smi >/dev/null 2>&1; then
    curl -fsSL https://raw.githubusercontent.com/GoogleCloudPlatform/compute-gpu-installation/main/linux/install_gpu_driver.py \
        -o /tmp/install_gpu_driver.py
    sudo python3 /tmp/install_gpu_driver.py || {
        echo "Google installer failed — falling back to the Ubuntu packaged driver" >&2
        sudo apt-get install -y nvidia-driver-535
    }
fi

# Cloud SQL Auth Proxy (serve_v3_live reads prod Postgres through it on 127.0.0.1:5432)
if [ ! -x "$HOME/cloud-sql-proxy" ]; then
    curl -fsSL https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.14.1/cloud-sql-proxy.linux.amd64 \
        -o "$HOME/cloud-sql-proxy"
    chmod +x "$HOME/cloud-sql-proxy"
fi

# Python venv for serve_v3_live + gcp_gateway. The code needs Python 3.11+ (datetime.UTC) but Ubuntu
# 22.04 ships 3.10, so install 3.12 from deadsnakes and build the venv with it.
if ! command -v python3.12 >/dev/null 2>&1; then
    sudo apt-get install -y software-properties-common
    sudo add-apt-repository -y ppa:deadsnakes/ppa
    sudo apt-get update
    sudo apt-get install -y python3.12 python3.12-venv
fi
[ -x "$REPO/.venv/bin/python" ] || python3.12 -m venv "$REPO/.venv"
"$REPO/.venv/bin/pip" install --quiet --upgrade pip
"$REPO/.venv/bin/pip" install --quiet -r "$REPO/v3/requirements.txt"

echo
echo "bootstrap done. Now:  sudo reboot"
echo "then verify:          nvidia-smi && ffmpeg -hide_banner -encoders | grep nvenc"
echo "and remember:         scp cesium-config.js into v3/broadcast/core/web/ (gitignored, see GATEWAY_VM.md)"
