#!/usr/bin/env bash
# research/the-seventh-part-of-the-man/make-facsimile-crops.sh
#
# The three strips of Leonardo's handwriting on the page are cut from the facsimile J. P. Richter
# printed in 1883 (The Literary Works of Leonardo da Vinci, vol. 1, Plate XVIII), as scanned by the
# Internet Archive (item literaryworksofl01leonuoft, page n252). Leonardo wrote right to left, so the
# plate is mirrored first (ImageMagick -flop), then cut. Needs curl and ImageMagick.
#
# Runs anywhere. Inside the repository it writes the page's own img/ directory; anywhere else (a
# copy downloaded from /checks/ into an empty directory) it fetches the plate into
# ./sources/richter-1883/ and writes the strips to ./img beside this script, and the SHA-256 it
# prints can be compared with the strips the page serves, e.g.
#   curl -s https://artwaste.land/strata/the-seventh-part-of-the-man/img/facsimile-forearm.jpg | sha256sum
set -euo pipefail
here="$(cd "$(dirname "$0")" && pwd)"
plate="$here/sources/richter-1883/plate-xviii-n252.jpg"
page="$here/../../public/strata/the-seventh-part-of-the-man"
if [ -d "$page" ]; then out="$page/img"; else out="$here/img"; fi
[ -s "$plate" ] || curl -sSL --create-dirs -o "$plate" "https://archive.org/download/literaryworksofl01leonuoft/page/n252.jpg"
mkdir -p "$out"
# geometry on the mirrored plate: WxH+X+Y
cut() { convert "$plate" -flop -crop "$2" +repage -quality 82 "$out/$1"; }
cut facsimile-forearm.jpg 390x56+888+3198   # "mano · fia · la quarta (or quinta?) · parte"
cut facsimile-knee.jpg    450x58+1588+3352  # "ginochio · fia · la quarta · parte"
cut facsimile-foot.jpg    400x58+558+3302   # "piè · fia · la sectima · parte"
sha256sum "$plate" "$out"/facsimile-*.jpg
