#!/usr/bin/env bash

# Folder with full-size wallpapers
WALLPAPER_DIR="$HOME/Pictures/Wallpapers"

# Folder to store thumbnails
THUMB_DIR="$HOME/.cache/wallpaper-thumbs"

if [ ! -d $THUMB_DIR ]; then
  mkdir -p $THUMB_DIR;
fi


# Make sure the thumb directory exists
mkdir -p "$THUMB_DIR"

# Max width/height of the thumbnails
MAX_DIM=300

num=0

# Process each image
for img in "$WALLPAPER_DIR"/*.{jpg,jpeg,png}; do
    # Skip if no files match
    [ -e "$img" ] || continue

    # Get just the filename
    fname=$(basename "$img")

    # Output path
    thumb="$THUMB_DIR/$fname"

    # If thumbnail already exists and is newer than the original, skip
    if [ -f "$thumb" ] && [ "$thumb" -nt "$img" ]; then
        continue
    fi

    num+=1

    # Generate thumbnail using ImageMagick
    magick "$img" -resize "${MAX_DIM}x${MAX_DIM}" "$thumb"
done

echo "$num Thumbnails generated in $THUMB_DIR"