#!/bin/bash
# NERDLETTER NUMBERS STATION MP4 GENERATOR — macOS friendly
# Lincolnshire Poacher style for the retro crowd

MESSAGE="$1"
PAGE="$2"
EPISODE="${3:-01}"
OUTFILE="nerdletter_secret_${EPISODE}_page${PAGE}.mp4"

echo "=== NERDLETTER SECRET TRANSMISSION #${EPISODE} ==="
echo "Page ${PAGE} — Generating spoken MP4..."

# 1. Spoken audio (classic numbers-station female voice)
echo "NERDLETTER SECRET MESSAGE ${EPISODE}. USE DAY BOOK PAGE ${PAGE}. ${MESSAGE}" | \
espeak-ng -ven+f3 -s115 -k25 -a 160 -w temp_voice.wav --punct="" 2>/dev/null

# 2. Simple retro video (black background + green text) — no drawtext needed
ffmpeg -f lavfi -i color=black:s=1280x720:d=18 -c:v libx264 -t 18 -pix_fmt yuv420p temp_bg.mp4 -y

# Overlay text using subtitles (much more reliable on Homebrew ffmpeg)
cat > temp_sub.srt << EOF
1
00:00:01,000 --> 00:00:18,000
NERDLETTER SECRET TRANSMISSION #${EPISODE}
USE DAY BOOK PAGE ${PAGE}

${MESSAGE}

DESTROY PAGE AFTER USE
EOF

# 3. Burn in subtitles + combine audio
ffmpeg -i temp_bg.mp4 -i temp_voice.wav -vf subtitles=temp_sub.srt:force_style='Fontname=Courier New,Fontsize=42,PrimaryColour=&H00FF00&,BackColour=&H80000000&,BorderStyle=3' \
-c:v libx264 -c:a aac -shortest "$OUTFILE" -y

# Cleanup
rm -f temp_voice.wav temp_bg.mp4 temp_sub.srt

echo "✅ Done! → ${OUTFILE}"
echo "Upload this MP4 to the website. Subscribers will feel like they're tuning into a real numbers station."

