#!/bin/bash
# runs every command published on the page; run from the work folder holding base.mp4, emoji.srt, edge.srt, emoji-clean.srt
LOG=commands.log; : > $LOG
run(){ # label, published, command
  echo "== $1" >> $LOG; echo "published: $2" >> $LOG; echo "as run: $3" >> $LOG
  out=$(bash -c "$3" 2>&1); rc=$?; echo "exit=$rc" >> $LOG; echo "$out" | tail -n ${4:-8} >> $LOG; }
P1="ffmpeg -v verbose -i video.mp4 -vf \"subtitles=subs.srt\" -f null - 2>&1 | grep -o \"failed to find any fallback with glyph 0x[0-9A-F]*\" | sort -u"
run "P1 diagnose (emoji.srt)" "$P1" "ffmpeg -v verbose -i base.mp4 -vf \"subtitles=emoji.srt\" -f null - 2>&1 | grep -o \"failed to find any fallback with glyph 0x[0-9A-F]*\" | sort -u" 12
echo "edge: file without emoji" >> $LOG
run "P1 edge (plain cue file)" "$P1" "printf \"1\n00:00:00,000 --> 00:00:02,000\nPlain cue\n\n\" > plain.srt; ffmpeg -v verbose -i base.mp4 -vf \"subtitles=plain.srt\" -f null - 2>&1 | grep -o \"failed to find any fallback with glyph 0x[0-9A-F]*\" | sort -u"
run "P2 strip then burn" "ffmpeg -i video.mp4 -vf \"subtitles=subs-clean.srt\" -c:a copy burned.mp4" "ffmpeg -v error -y -i base.mp4 -vf \"subtitles=emoji-clean.srt\" -c:a copy burned.mp4; ffprobe -v error -show_entries stream=codec_type,codec_name -of csv=p=0 burned.mp4" 
echo "edge: no-audio video" >> $LOG
run "P2 edge no audio" "ffmpeg -i video.mp4 -vf \"subtitles=subs-clean.srt\" -c:a copy burned.mp4" "ffmpeg -v error -y -i base.mp4 -an noaudio.mp4; ffmpeg -v error -y -i noaudio.mp4 -vf \"subtitles=emoji-clean.srt\" -c:a copy burned2.mp4; ffprobe -v error -show_entries stream=codec_type -of csv=p=0 burned2.mp4"
echo "edge: edge.srt after Koldflux strip (clean file)" >> $LOG
run "P2 edge edge-clean.srt" "ffmpeg -i video.mp4 -vf \"subtitles=subs-clean.srt\" -c:a copy burned.mp4" "ffmpeg -v error -y -i base.mp4 -vf \"subtitles=edge-clean.srt\" -c:a copy burned3.mp4; ffmpeg -v error -i edge-clean.srt -f ass - | grep -c ^Dialogue"
# P3 font route in a temp HOME so system fonts stay untouched; includes the real download
P3="curl -L -o \"NotoEmoji[wght].ttf\" \"https://github.com/google/fonts/raw/main/ofl/notoemoji/NotoEmoji%5Bwght%5D.ttf\"; mkdir -p ~/.local/share/fonts && cp \"NotoEmoji[wght].ttf\" ~/.local/share/fonts/ && fc-cache -f; ffmpeg -i video.mp4 -vf \"subtitles=subs.srt\" -c:a copy burned.mp4"
run "P3 font route" "$P3" "rm -rf p3 && mkdir p3 && cd p3 && export HOME=\$PWD/home && mkdir -p \$HOME && curl -sSL -o \"NotoEmoji[wght].ttf\" \"https://github.com/google/fonts/raw/main/ofl/notoemoji/NotoEmoji%5Bwght%5D.ttf\" && sha256sum \"NotoEmoji[wght].ttf\" && mkdir -p ~/.local/share/fonts && cp \"NotoEmoji[wght].ttf\" ~/.local/share/fonts/ && fc-cache -f && ffmpeg -v verbose -y -i ../base.mp4 -vf \"subtitles=../emoji.srt\" -c:a copy burned.mp4 2>&1 | grep -c \"failed to find any fallback\"; echo missing-glyph-lines-above" 4
echo "edge: SRT with no emoji, font installed" >> $LOG
run "P3 edge plain.srt" "$P3" "cd p3 && export HOME=\$PWD/home && ffmpeg -v verbose -y -i ../base.mp4 -vf \"subtitles=../plain.srt\" -c:a copy burned-plain.mp4 2>&1 | grep -c \"failed to find any fallback\"; ffprobe -v error -show_entries stream=codec_name -of csv=p=0 burned-plain.mp4"
echo "edge: a second emoji font (Noto Color Emoji) also installed in the same folder" >> $LOG
run "P3 edge second font" "$P3" "cd p3 && export HOME=\$PWD/home && cp ../../fonts/NotoColorEmoji-Regular.ttf ~/.local/share/fonts/ && fc-cache -f && ffmpeg -v verbose -y -i ../base.mp4 -vf \"subtitles=../emoji.srt\" -c:a copy burned2.mp4 2>&1 | grep -c \"failed to find any fallback\"; ffprobe -v error -show_entries stream=codec_name -of csv=p=0 burned2.mp4"
echo "note: P3 ran with HOME=work/p3/home (not the real HOME); grep -c prints 0 and exits 1 when nothing matches, that is the expected result here. With the second font installed the log lists no missing glyphs, but how the emoji were drawn was not checked in frames." >> $LOG
echo "edge: P1 pipeline exit codes" >> $LOG
run "P1 edge edge.srt (contains an emoji, prints nothing)" "$P1" "ffmpeg -v verbose -i base.mp4 -vf \"subtitles=edge.srt\" -f null - 2>&1 | grep -o \"failed to find any fallback with glyph 0x[0-9A-F]*\" | sort -u; echo \"pipe status: \${PIPESTATUS[*]}\"; grep -c 😀 edge.srt"
echo "note: edge.srt contains U+1F600 but the P1 command prints nothing, because a font on this machine covers it. Empty output means no code point lacked a font here, not that the file has no emoji. The pipeline exits 0 either way (sort is last)." >> $LOG
