#!/usr/bin/env bash
# Rebuild the README's demo video from made-up data: seed the demo, screenshot `jobwatch ui` on it, write the
# slides and narration, and render them with slidecast. See scripts/demo/README.md.
#
#   scripts/demo/make-video [OUT] [--music DIR]
#
# OUT defaults to scripts/demo/out (git ignores it). Set PYTHON or SLIDECAST to use other installs.
set -euo pipefail
cd "$(dirname "$0")/../.."

out="scripts/demo/out"
music=()
while [ $# -gt 0 ]; do
  case "$1" in
    --music) music=(--music "$2"); shift 2 ;;
    -h|--help) sed -n '2,7p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
    *) out="$1"; shift ;;
  esac
done
python="${PYTHON:-python}"
slidecast="${SLIDECAST:-slidecast}"

"$python" scripts/demo/seed.py -o "$out/demo"
"$python" scripts/screenshots.py --demo -c "$out/demo/jobwatch.yaml" -o "$out/shots" \
  --reply scripts/demo/chat-reply.md
"$python" scripts/demo/build.py "$out" ${music[@]+"${music[@]}"}
"$slidecast" render "$out/reel.yaml" -o "$out/jobwatch-demo.mp4" --poster
# The README's silent preview: 5 fps is plenty for slides, and keeps it under 2 MB.
ffmpeg -v error -y -i "$out/jobwatch-demo.mp4" -vf "fps=5,scale=960:-1:flags=lanczos,split[a][b];\
[a]palettegen=max_colors=128:stats_mode=diff[p];[b][p]paletteuse=dither=bayer:bayer_scale=4:diff_mode=rectangle" \
  "$out/demo.gif"
echo "Wrote $out/jobwatch-demo.mp4 and $out/demo.gif (copy both to docs/ to update the README)"
