#!/usr/bin/env bash

if [ $# -eq 0 ]; then
    echo "Usage: $0 <website_url>"
    exit 1
fi

# Create a directory to store images
mkdir -p downloaded_images
cd downloaded_images

# Counter for image naming, start at 1
counter=1

# Extract image URLs and download
wget -q -O- "$1" | grep -oE 'https?://[^"]+\.(jpg|jpeg|png|gif)' | while read -r img_url; do
    # Download image
    wget -q "$img_url" -O "temp_image"
    
    # Get image width using file size and basic check (approximate)
    width=$(file "temp_image" | grep -oE '[0-9]+ x [0-9]+' | cut -d' ' -f1)
    
    # Check if width is over 400
    if [ -n "$width" ] && [ "$width" -gt 400 ]; then
        # Rename to sequential PNG starting from image1.png
        mv "temp_image" "image$counter.png"
        ((counter++))
    else
        # Remove images that don't meet criteria
        rm "temp_image"
    fi
done

echo "Downloaded $((counter-1)) images larger than 400px wide"