#!/bin/bash
# NERDLETTER NUMBERS STATION MP4 GENERATOR — Letter-by-Letter Edition
# Real cold-war numbers station pronunciation

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..."

# === CRITICAL FIX: Convert groups to spaced letters for proper pronunciation ===
SPACED_MESSAGE=$(echo "$MESSAGE" | sed 's/\([A-Z]\)/\1 /g' | sed 's/  / /g')

# Spoken announcement (clear and dramatic)
TEXT_TO_SPEAK="NERDLETTER SECRET MESSAGE ${EPISODE}. USE DAY BOOK PAGE ${PAGE}. ${SPACED_MESSAGE}"

echo "$TEXT_TO_SPEAK" | \
espeak-ng -ven+f3 -s105 -k40 -a 180 -w temp_voice.wav --punct="" 2>/dev/null

# Video with retro green text
ffmpeg -f lavfi -i color=black:s=1280x720:d=22 -c:v libx264 -t 22 -pix_fmt yuv420p temp_bg.mp4 -y

ffmpeg -i temp_bg.mp4 -i temp_voice.wav \
-vf "drawtext=font='Courier':fontsize=52:fontcolor=green@0.95:text='NERDLETTER SECRET TRANSMISSION #${EPISODE}':x=(w-text_w)/2:y=80, \
drawtext=font='Courier':fontsize=38:fontcolor=green@0.85:text='USE DAY BOOK PAGE ${PAGE}':x=(w-text_w)/2:y=170, \
drawtext=font='Courier':fontsize=46:fontcolor=lime@0.95:text='${MESSAGE}':x=(w-text_w)/2:y=280:box=1:boxcolor=black@0.75:boxborderw=15, \
drawtext=font='Courier':fontsize=32:fontcolor=green@0.75:text='DESTROY PAGE AFTER USE — SUBSCRIBERS ONLY':x=(w-text_w)/2:y=640" \
-c:v libx264 -c:a aac -shortest "$OUTFILE" -y

# Cleanup
rm -f temp_voice.wav temp_bg.mp4

echo "✅ Done! → ${OUTFILE}"
echo "Now each group should be spoken letter-by-letter like a real numbers station."
