#!/bin/bash
# Runs every published shell command of the guide on the test photo and on an edge input; usage: bash commands-run.sh <workdir>
W=$1; cd "$W/files"; L=commands.log; : > $L
run() { # published, actual, edge
  out=$(bash -c "$2" 2>&1); code=$?
  printf 'published: %s\nran: %s\nexit=%s\nedge: %s\noutput: %s\n\n' "$1" "$2" "$code" "$3" "$(echo "$out" | tail -n 2 | tr '\n' ' ')" >> $L; }
P1='ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 photo.jpg'
P2='ffmpeg -i photo.jpg -vf "scale=2560:1440:force_original_aspect_ratio=increase,crop=2560:1440" -q:v 3 banner.jpg'
P3='wc -c < banner.jpg'
for pair in "lighthouse.jpg|test file: 4548x2988 photo (3:2)" "pango.jpg|edge: 1600x514 panorama, below the 2048x1152 minimum and wider than 16:9"; do
  f=${pair%%|*}; why=${pair#*|}
  cp $f photo.jpg; rm -f banner.jpg
  run "$P1" "$P1" "$why"
  run "$P2" "${P2/-i photo.jpg/-y -loglevel error -i photo.jpg}" "$why"
  run "$P3" "$P3" "$why"
  ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 banner.jpg | sed "s/^/banner dims after $f: /" >> $L; echo >> $L
done
rm -f photo.jpg; mv $L ../commands.log
