#!/bin/sh -ex

# SPDX-FileCopyrightText: Chris Pressey, the original author of this work, has dedicated it to the public domain.
# For more information, please refer to <https://unlicense.org/>
# SPDX-License-Identifier: Unlicense

# this uses https://pypi.org/project/heif-convert/

target=$1
shift

if [ ! -d "$target" ]; then
    echo "Not a directory: $target"
    exit 1
fi

for d in *; do
    mkdir -p "$target/$d"
    cd $d
    for file in *.HEIC; do
        jpgfile="${file%.*}.jpg"
        if [ ! -r "../$target/$d/$jpgfile" ]; then
            heif-convert -q 97 -p "../$target/$d/" "$file"
            mogrify -resize 25% "../$target/$d/$jpgfile"
        fi
    done
    cd ..
done
