#!/bin/bash
# Runs every command published on the guide and writes commands.log. Mac part: hook-timer.py and say (macOS only).
# The ffprobe part runs on a Linux box with ffmpeg 6.1.1 (set FFHOST to an ssh host, or leave empty when ffprobe is local).
# usage: bash commands.sh   (run inside the folder that holds hook-timer.py and hooks-meal-prep.txt)
LOG=commands.log; : > "$LOG"
FFHOST=${FFHOST:-}
ff() { if [ -n "$FFHOST" ]; then scp -q "$1" "$FFHOST:/tmp/kf-hook.wav"; ssh "$FFHOST" "ffprobe -v error -show_entries format=duration -of csv=p=0 /tmp/kf-hook.wav"; else ffprobe -v error -show_entries format=duration -of csv=p=0 "$1"; fi; }
run() { # run <published command> <command as run> <label>
  echo "published: $1" >> "$LOG"; echo "run: $2" >> "$LOG"
  out=$(bash -c "$2" 2>&1); code=$?
  echo "$out" | head -6 | sed 's/^/  /' >> "$LOG"; echo "exit=$code" >> "$LOG"; echo "$3" >> "$LOG"; echo >> "$LOG"
}
run 'python3 hook-timer.py hooks-meal-prep.txt --wpm 155 --limit 3.0' 'python3 hook-timer.py hooks-meal-prep.txt --wpm 155 --limit 3.0 | head -3; python3 hook-timer.py hooks-meal-prep.txt --wpm 155 --limit 3.0 | tail -1' 'edge: test file (30 hook lines, the meal-prep fill); first 3 lines and the last line shown'
run 'python3 hook-timer.py hooks-meal-prep.txt --wpm 155 --limit 3.0' 'python3 hook-timer.py edge-hooks.txt --wpm 155 --limit 3.0' 'edge: a file with an emoji, a blank line, a lone dash, "1,000" and "30-second"; the dash line counts 0 words, the emoji is not a word'
run 'python3 hook-timer.py hooks-meal-prep.txt --wpm 155 --limit 3.0' 'python3 hook-timer.py edge-hooks.txt --wpm 0 --limit 3.0' 'edge: pace 0 must be refused, not divide by zero (exit 1 with a message)'
run 'say -v Samantha -r 155 --data-format=LEI16@22050 -o hook.wav "The meal prep rule nobody tells parents."' 'say -v Samantha -r 155 --data-format=LEI16@22050 -o hook.wav "The meal prep rule nobody tells parents."; ls -l hook.wav | awk "{print \$5, \$9}"' 'edge: test line, 7 words'
run 'say -v Samantha -r 155 --data-format=LEI16@22050 -o hook.wav "The meal prep rule nobody tells parents."' 'say -v Samantha -r 155 --data-format=LEI16@22050 -o hook1.wav "Stop."; ls -l hook1.wav | awk "{print \$5, \$9}"' 'edge: a one-word line'
echo "published: ffprobe -v error -show_entries format=duration -of csv=p=0 hook.wav" >> "$LOG"; echo "run: ffprobe -v error -show_entries format=duration -of csv=p=0 hook.wav (on ${FFHOST:-this host})" >> "$LOG"
d=$(ff hook.wav 2>&1); c=$?; echo "  $d" >> "$LOG"; echo "exit=$c" >> "$LOG"; echo "edge: test line, 7 words (seconds of the whole file)" >> "$LOG"; echo >> "$LOG"
echo "published: ffprobe -v error -show_entries format=duration -of csv=p=0 hook.wav" >> "$LOG"; echo "run: same on hook1.wav" >> "$LOG"
d=$(ff hook1.wav 2>&1); c=$?; echo "  $d" >> "$LOG"; echo "exit=$c" >> "$LOG"; echo "edge: a one-word line, to see the shortest case" >> "$LOG"
