#!/bin/bash
# NERDLETTER NUMBERS STATION MP4 GENERATOR — macOS Fixed
# Lincolnshire Poacher style, using drawtext (more reliable on Homebrew)

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 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. Create background video + burn text with drawtext (no subtitles filter needed)
ffmpeg -f lavfi -i color=black:s=1280x720:d=18 -c:v libx264 -t 18 -pix_fmt yuv420p temp_bg.mp4 -y

ffmpeg -i temp_bg.mp4 -i temp_voice.wav \
-vf "drawtext=font='Courier New':fontsize=48:fontcolor=green@0.95:text='NERDLETTER SECRET TRANSMISSION #${EPISODE}':x=(w-text_w)/2:y=80, \
drawtext=font='Courier New':fontsize=36:fontcolor=green@0.85:text='USE DAY BOOK PAGE ${PAGE}':x=(w-text_w)/2:y=160, \
drawtext=font='Courier New':fontsize=42:fontcolor=lime@0.95:text='${MESSAGE}':x=(w-text_w)/2:y=260:box=1:boxcolor=black@0.7:boxborderw=12, \
drawtext=font='Courier New':fontsize=28:fontcolor=green@0.7:text='DESTROY PAGE AFTER USE':x=(w-text_w)/2:y=620" \
-c:v libx264 -c:a aac -shortest "$OUTFILE" -y

# Cleanup
rm -f temp_voice.wav temp_bg.mp4

echo "✅ Done! → ${OUTFILE}"
echo "Upload this MP4 + the text groups to the website."
