#!/usr/bin/env bash
# Build western-cv-fuse-energy.html -> PDF with a full timestamp, drop the result
# into /mnt/storage/inbox (visible via https://files.udalovs.ru).
set -euo pipefail

cd "$(dirname "$0")"

SRC="western-cv-fuse-energy.html"
INBOX="${INBOX:-/mnt/storage/inbox}"

STAMP="$(date +%Y%m%d-%H%M%S)"
GEN="$(date -u +'%Y-%m-%d %H:%M:%S UTC')"
OUT="fuse-energy-cv-${STAMP}.pdf"

TMP="$(mktemp --suffix=.html)"
sed "s/__GEN_STAMP__/${GEN}/" "$SRC" > "$TMP"

# render directly to inbox (no local copy — never commit binaries)
playwright pdf --paper-format A4 "file://$(realpath "$TMP")" "$INBOX/${OUT}"

cp -f "$INBOX/${OUT}" "$INBOX/fuse-energy-cv.pdf"

rm -f "$TMP"

echo "OK:"
echo "  inbox : $INBOX/${OUT}"
echo "  latest: $INBOX/fuse-energy-cv.pdf"
echo "  web   : https://files.udalovs.ru/${OUT}"
