#!/bin/bash

set -eux

examples=(
    nospace-1
    nospace-2
    diff-1
    diff-2
)

ELPA=~/Downloads/sources/emacs-meta/elpa
adaptive_wrap_dir=${ELPA}/packages/adaptive-wrap
patch=$(realpath $1)
base=${2:-master}
emacs=emacs

comment=$(basename ${patch} .patch)

(
    cd ${ELPA}
    git checkout ${base} -- packages/adaptive-wrap/adaptive-wrap.el
)

for e in ${examples[@]}
do
    ${emacs} -Q -rv -l repro.el -L ${adaptive_wrap_dir} \
          --eval "(bug41810-$e \"before\")"
done

(
    cd ${ELPA}
    git apply ${patch}
)

for e in ${examples[@]}
do
    ${emacs} -Q -rv -l repro.el -L ${adaptive_wrap_dir} \
          --eval "(bug41810-$e \"after\")"

    magick convert ${e}-before.png ${e}-after.png +append ${comment}-${e}.png
    rm ${e}-{before,after}.png
done
