#!/bin/bash
# Gate 24, part 1 (VPS, ffmpeg/ffprobe): run each published command on the test file and an edge input.
# Usage: bash cmds-vps.sh   (run from the folder holding in/)
LOG=commands-ffmpeg.log; : > $LOG; mkdir -p fix2
run() { # label, command with IN and OUT placeholders, input, output
  c=${2//IN/$3}; c=${c//OUT/$4}
  pub=${2//IN/cover.png}; pub=${pub//OUT/fixed.${4##*.}}
  echo "published: $pub" >> $LOG
  out=$(bash -c "$c" 2>&1); ec=$?; out=$(echo "$out" | tail -2)
  echo "run on $3: ${out:-(no output)}" >> $LOG; echo "exit=$ec" >> $LOG; echo "edge: $1" >> $LOG; echo >> $LOG
}
PROBE='ffprobe -v error -show_entries stream=width,height,pix_fmt -of csv=p=0 IN'
run "test file, PNG" "$PROBE" in/01-ok-3000.png x
run "CMYK JPEG: pix_fmt reads gbrap, same word an alpha PNG gives (see 07)" "$PROBE" in/06-cmyk-3000.jpg x
run "PNG with alpha: pix_fmt reads rgba" "$PROBE" in/07-alpha-corners-3000.png x
run "file that does not exist" "$PROBE" in/nofile.png x
CROP='ffmpeg -v error -y -i IN -vf "crop=min(iw\,ih):min(iw\,ih),scale=3000:3000:flags=lanczos" -frames:v 1 -update 1 OUT'
run "test file 3000x2800" "$CROP" in/05-nonsquare-3000x2800.png fix2/crop.png
run "portrait 2000x3000" "$CROP" in/12-portrait-2000x3000.png fix2/crop-portrait.png
PAD='ffmpeg -v error -y -i IN -vf "pad=max(iw\,ih):max(iw\,ih):(ow-iw)/2:(oh-ih)/2:color=0x141438,scale=3000:3000:flags=lanczos" -frames:v 1 -update 1 OUT'
run "test file 3000x2800" "$PAD" in/05-nonsquare-3000x2800.png fix2/pad.png
run "portrait 2000x3000" "$PAD" in/12-portrait-2000x3000.png fix2/pad-portrait.png
FLAT='ffmpeg -v error -y -i IN -vf "split[a][b];[a]drawbox=c=0x141438:t=fill[bg];[bg][b]overlay,format=rgb24" -frames:v 1 -update 1 OUT'
run "test file, transparent corners" "$FLAT" in/07-alpha-corners-3000.png fix2/flat.png
run "alpha channel present but fully opaque" "$FLAT" in/07b-alpha-opaque-3000.png fix2/flat-opaque.png
CMYK='ffmpeg -v error -y -i IN -pix_fmt yuvj420p -q:v 2 OUT'
run "test file, CMYK JPEG" "$CMYK" in/06-cmyk-3000.jpg fix2/cmyk.jpg
run "already RGB JPEG" "$CMYK" in/11-ok-3000.jpg fix2/rgb-again.jpg
WEBP='ffmpeg -v error -y -i IN -frames:v 1 -update 1 OUT'
run "test file, WebP" "$WEBP" in/08-webp-3000.webp fix2/webp.png
run "file that does not exist" "$WEBP" in/nofile.webp fix2/none.png
JPG='ffmpeg -v error -y -i IN -q:v 2 OUT'
run "test file, PNG saved under a .jpg name" "$JPG" in/09-png-renamed-3000.jpg fix2/renamed.jpg
SCALE='ffmpeg -v error -y -i IN -vf scale=3000:3000:flags=lanczos -frames:v 1 -update 1 OUT'
run "test file 3001x3001 (one pixel over)" "$SCALE" in/04-large-3001.png fix2/big.png
run "1399 JPEG scaled up to 3000 (pixels are only enlarged, not restored)" "$SCALE" in/03-small-1399.jpg fix2/small-up.png
