#!/bin/bash

# Check if the folder is provided
if [ -z "$1" ]; then
  echo "Usage: $0 /path/to/folder"
  exit 1
fi

# Folder path
folder="$1"

# Check if the folder exists
if [ ! -d "$folder" ]; then
  echo "Directory $folder not found!"
  exit 1
fi

# Loop through all image files in the folder
for img in "$folder"/*.{jpg,jpeg,png,gif,tiff}; do
  if [ -f "$img" ]; then
    # Get the file name without extension
    filename=$(basename "$img")
    filename_noext="${filename%.*}"

    # Convert the image to WebP format
    cwebp -quiet "$img" -o "$folder/$filename_noext.webp"
    echo "Converted: $img -> $folder/$filename_noext.webp"
  fi
done

echo "Conversion complete!"
