#!/usr/bin/env bash
# 舊版佇列(拆集功能之前啟動的)會把「下集」(-b.mp4)留在 ~/mkvideo:定期依檔名規則搬到 public/videos。
cd "$(dirname "$0")/.."
while true; do
  for f in ~/mkvideo/*-b.mp4; do [ -e "$f" ] || continue
    b=$(basename "$f" .mp4); b=${b%-b}; dest=""
    case "$b" in
      vis-*) dest=${b#vis-} ;;
      dv-*)  dest=${b#dv-} ; dest=$(echo "$dest" | sed -E 's/-([0-9]{2})$/-ch\1/') ;;
      yj-*)  dest=$(echo "${b#yj-}" | sed -E 's/^([a-z]+-g[0-9]-[0-9])-([0-9]{2})$/hs-\1-ch\2/; s/^(chinese|english|math)-/hs-\1-/; s/-([0-9]{2})$/-ch\1/') ;;
      qq-*)  dest=$(echo "${b#qq-}" | sed -E 's/^(chinese|english|math)-/jh-\1-/; s/-([0-9]{2})$/-ch\1/') ;;
    esac
    [ -n "$dest" ] && [ -s "public/videos/$dest.mp4" ] && [ -s "$f" ] && sleep 5 && mv "$f" "public/videos/$dest-b.mp4" && echo "$(date +%H:%M) 搬 $dest-b.mp4"
  done
  sleep 300
done
