#!/usr/bin/env bash
# 高中英文、數學:改由「怡君老師」出鏡(只換人物圖,聲音不變:曉臻,語音快取沿用,所以很省資源)。
# 用法:tools/queue-hs-yijun.sh [科目 english|math ...]   做過的(標記檔 ~/mkvideo/yijun-done)會跳過;先寫暫存檔,成功才覆蓋 public/videos。
cd "$(dirname "$0")/.."
export SHOT_PAR=${SHOT_PAR:-2}
subjects=("$@"); [ ${#subjects[@]} -eq 0 ] && subjects=(english math)
mkdir -p ~/mkvideo/yijun-done
for s in "${subjects[@]}"; do for t in g1-1 g1-2 g2-1 g2-2 g3-1 g3-2; do for n in 01 02 03 04 05 06; do
  dir=database/seeders/courses/high-school-$s/$t; out=public/videos/hs-$s-$t-ch$n.mp4; mark=~/mkvideo/yijun-done/hs-$s-$t-ch$n
  [ -e "$mark" ] && continue
  [ -s $dir/ch$n.narration.json ] || continue
  node tools/validate-narration.mjs $dir/ch$n.php $dir/ch$n.narration.json --allow-english 2>&1 | tail -1 | grep -q '^OK' || continue
  tmp=$HOME/mkvideo/yj-$s-$t-$n.mp4; log=/tmp/mkv-yj-$s-$t-ch$n.log
  if nice -n 10 node tools/make-video2.mjs $dir/ch$n.php $tmp --narration=$dir/ch$n.narration.json --strict-narration \
       --avatar=public/images/teacher-yijun-sheet.jpg --avatar-grid=4x2:6,2,4,1,3,7,8,5 --avatar-height=340 --cycle=1,3,0,4,2,5 --avatar-cache=storage/app/avatar-yijun > $log 2>&1 && [ -s "$tmp" ]; then
    mv "$tmp" "$out" && { [ -s "${tmp%.mp4}-b.mp4" ] && mv "${tmp%.mp4}-b.mp4" "${out%.mp4}-b.mp4"; true; } && touch "$mark" && echo "OK   $out"
  else echo "FAIL $out: $(grep -m1 -E 'Error|錯誤|找不到' $log | cut -c1-150)"; fi
done; done; done
echo "怡君老師高中影片完成 $(date +%H:%M)"
