#!/bin/bash

# Libary needed: ImageMagick

PARAMS=('-quality 60')

if [ $# -ne 0 ]; then
	PARAMS=$@;
fi

cd $(pwd)

shopt -s nullglob nocaseglob extglob
shopt -s globstar

# Resize
sizes=(320 640 960 1280)
for FILE in public/**/*.@(jpg|jpeg|tif|tiff|png|gif); do
    DIR=$(dirname "$FILE")
    NAME=$(basename "$FILE" | cut -d. -f1)
    EXTENSION=$(basename "$FILE" | cut -d. -f2)
    for size in ${sizes[@]}; do
        convert "$FILE" -resize ${size}x${size}\> "${DIR}/${NAME}-${size}.${EXTENSION}" 
    done
done

# Web Optimized Formats
for FILE in public/**/*.@(jpg|jpeg|tif|tiff|png|gif); do 
    convert $PARAMS "$FILE" "${FILE}".webp;
    convert $PARAMS "$FILE" "${FILE}".avif;
done