#!/bin/bash

# Base directories to search
base_dirs=(
    "/home/insuraz1/public_html/bookbildr.com/bildr/media/bildr"
    "/home/insuraz1/public_html/edu.bookbildr.com/bildr/media/bildr"
    "/home/insuraz1/public_html/emagicum.com/bildr/media/bildr"
)

for base_dir in "${base_dirs[@]}"; do
    # Get all first-level subdirectories within base_dir
    if [[ -d "$base_dir" ]]; then
        subdirs=("$base_dir"/*)
        
        for DIR in "${subdirs[@]}"; do
            # Ensure it's a directory
            if [[ -d "$DIR" ]]; then
                echo "Scanning directory: $DIR"
                
                # Find all image files (only in the specified directory, no subdirectories beyond it)
                find "$DIR" -maxdepth 1 -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" \) ! -name "*_thumb.*" ! -name "__book_*" | while read -r img; do
                    # Generate expected thumbnail filename
                    thumb="${img%.*}_thumb.${img##*.}"

                    # Check if the thumbnail file exists
                    if [[ ! -f "$thumb" ]]; then
                        echo "Missing thumbnail: $thumb"
                        echo "Creating thumbnail..."

                        # Convert image to thumbnail (150px width)
                        convert "$img" -geometry 150x "$thumb"

                        # Check if conversion was successful
                        if [[ -f "$thumb" ]]; then
                            echo "Thumbnail created: $thumb"
                        else
                            echo "Failed to create thumbnail for: $img"
                        fi
                    fi
                done
            fi
        done
    fi
done
