#!/bin/bash
# Runs every command published on the page on the test photo and on edge inputs; writes commands.log.
# Usage: bash commands.sh (needs ffmpeg 6.x; photo.jpg = test photo with EXIF rotation applied, small-3x4.jpg = 600x800 edge input)
cd "$(dirname "$0")"; : > commands.log; mkdir -p /tmp/ig-cmd
PROBE='ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0'
run() { # published-text, command-as-run, edge-label
  { echo "published: $1"; echo "run: $2"; [ -n "$3" ] && echo "edge: $3"
    out=$(eval "$2" 2>&1); code=$?
    echo "$out" | tail -4; echo "exit=$code"; echo; } >> commands.log
}
P1="$PROBE photo.jpg"
run "$P1" "$P1" ""
run "$P1" "$PROBE small-3x4.jpg" "smaller 600x800 portrait input"
P2='ffmpeg -i photo.jpg -vf "scale=1080:1350:force_original_aspect_ratio=increase,crop=1080:1350" -q:v 2 post-4x5.jpg'
SC='scale=1080:1350:force_original_aspect_ratio=increase,crop=1080:1350'
run "$P2" "ffmpeg -y -v error -i photo.jpg -vf \"$SC\" -q:v 2 /tmp/ig-cmd/post-4x5.jpg && $PROBE /tmp/ig-cmd/post-4x5.jpg" ""
run "$P2" "ffmpeg -y -v error -i small-3x4.jpg -vf \"$SC\" -q:v 2 /tmp/ig-cmd/post-4x5-small.jpg && $PROBE /tmp/ig-cmd/post-4x5-small.jpg" "600x800 input is smaller than the target: scaled UP to 1080 wide, so no detail is added"
run "$P2" "ffmpeg -y -v error -i wright-mountain-cc0.jpg -vf \"$SC\" -q:v 2 /tmp/ig-cmd/post-4x5-exif.jpg && $PROBE /tmp/ig-cmd/post-4x5-exif.jpg" "original file with EXIF orientation 3: ffmpeg applies the rotation, output still 1080x1350"
P3='ffmpeg -i post-4x5.jpg -vf "crop=ih*3/4:ih" grid-preview.jpg'
run "$P3" "ffmpeg -y -v error -i /tmp/ig-cmd/post-4x5.jpg -vf \"crop=ih*3/4:ih\" /tmp/ig-cmd/grid-preview.jpg && $PROBE /tmp/ig-cmd/grid-preview.jpg" ""
run "$P3" "ffmpeg -y -v error -i /tmp/ig-cmd/post-4x5-small.jpg -vf \"crop=ih*3/4:ih\" /tmp/ig-cmd/grid-preview-small.jpg && $PROBE /tmp/ig-cmd/grid-preview-small.jpg" "input made from the 600x800 file"
