#!/bin/sh
# SPDX-License-Identifier: MIT
# Nginx Proxy Manager on the bench, generator side: the upstream site every proxy host forwards to,
# and the certificate the dataset imports. Runs in the load generator container; idempotent.
#   - nginx on port 8080, serving a 20 KB page, a 30 KB stylesheet, a 60 KB script and a 5 KB logo,
#     made of words drawn with a fixed seed: every run serves the same bytes, and they compress like
#     real text does;
#   - a self-signed certificate for *.banc.test, in /opt/banc/banc.crt and /opt/banc/banc.key.
set -eu
export DEBIAN_FRONTEND=noninteractive
command -v nginx >/dev/null || { apt-get update -qq && apt-get install -y -qq nginx openssl >/dev/null; }
mkdir -p /srv/banc-site
mots() { awk -v n="$1" -v graine="$2" 'BEGIN { srand(graine); k = split("server request cache style header button layout container module render value config index token session user account margin padding display function return const event element window document image width height color border", m, " "); t = 0; while (t < n) { w = m[int(rand() * k) + 1]; printf "%s ", w; t += length(w) + 1 } }'; }
fichier() { mots "$2" "$3" | head -c "$2" > "/srv/banc-site/$1"; }
[ -f /srv/banc-site/app.js ] || {
  { printf '<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Bench site</title>'
    printf '<link rel="stylesheet" href="/app.css"><script defer src="/app.js"></script></head><body>'
    printf '<p>'; mots 19500 1 | head -c 19500; printf '</p><img src="/logo.svg" alt=""></body></html>'; } > /srv/banc-site/index.html
  fichier app.css 30720 2; fichier app.js 61440 3; fichier logo.svg 5120 4
}
cat > /etc/nginx/sites-available/banc-amont <<'EOF'
server {
    listen 10.30.0.208:8080;
    root /srv/banc-site;
    access_log off;
}
EOF
ln -sf /etc/nginx/sites-available/banc-amont /etc/nginx/sites-enabled/banc-amont
rm -f /etc/nginx/sites-enabled/default
nginx -t -q && systemctl reload-or-restart nginx
[ -f /opt/banc/banc.crt ] || openssl req -x509 -newkey rsa:2048 -nodes -days 3650 -subj '/CN=*.banc.test' \
  -addext 'subjectAltName=DNS:*.banc.test' -keyout /opt/banc/banc.key -out /opt/banc/banc.crt 2>/dev/null
curl -fsS -o /dev/null http://10.30.0.208:8080/
