#!/bin/sh
#set -v -x
set -e -u -o pipefail

# Redirect output to stderr.
exec 1>&2

if ! which exiftool > /dev/null; then
    echo "exiftool is required for git pre-commit hook!"
    exit 1
fi

# don't commit photo metadata except colour settings
git diff-index --cached --diff-filter=A --name-only HEAD | ( grep -Ei '\.(HEIC|jpeg|jpg|tiff|tif|png|webp|avif|jxl)$' || true ) | while read file; do
    exiftool -all= --icc_profile:all -tagsfromfile @ -colorspacetags $file -o $file.tmp > /dev/null
    if ! diff $file $file.tmp; then
        echo "Apparently there is metadata still in $file, aborting!"
        echo "Try:"
        echo "exiftool -all= --icc_profile:all -tagsfromfile @ -colorspacetags $file"
        #rm $file.tmp
        exit 1
    fi
    rm $file.tmp
done
