#!/bin/sh
# strip all EXIF metadata from images under IMG_DIR, keeping ICC profile and orientation.
# run inside an alpine container with the image dir bind-mounted.
IMG_DIR="${1:-/images}"
CHOWN_TO=""   # optional "uid:gid" to restore file ownership afterwards, leave empty to skip
EXTS="-ext jpg -ext jpeg -ext png -ext gif -ext webp"

apk add --no-cache exiftool >/dev/null 2>&1 || { echo "apk failed"; exit 1; }
echo "exiftool version: $(exiftool -ver)"

printf "files with GPS before: "
exiftool -q -if '$GPSLatitude' -p '$FilePath' -r $EXTS "$IMG_DIR" | wc -l

exiftool -all= -tagsfromfile @ -icc_profile -orientation -P -overwrite_original -q -r $EXTS "$IMG_DIR"

printf "files with GPS after: "
exiftool -q -if '$GPSLatitude' -p '$FilePath' -r $EXTS "$IMG_DIR" | wc -l

if [ -n "$CHOWN_TO" ]; then
    chown -R "$CHOWN_TO" "$IMG_DIR"
    echo "ownership restored"
fi
# src: ezra, contact: ezkru69@mail.com