#!/bin/bash
# reorganize_images.sh - Move generated images from 2 levels down to 1 level down
# Usage: ./reorganize_images.sh [root_directory]
# Default root directory is current directory (.)

ROOT_DIR="${1:-.}"

echo "Reorganizing images in: $ROOT_DIR"
echo ""

# Find all *_generated.png files that are exactly 2 levels deep
# Pattern: root/level1/level2/*_generated.png -> root/level1/*_generated.png

find "$ROOT_DIR" -mindepth 3 -maxdepth 3 -name "generated.png" -type f | while read -r filepath; do
    # Get the directory structure
    # filepath = root/level1/level2/filename.png
    # We want to move to: root/level1/filename.png
    
    dir=$(dirname "$filepath")
    filename=$(basename "$filepath")
    
    # Get parent directory (1 level up from current location)
    # dir = root/level1/level2
    # parent = root/level1
    parent=$(dirname "$dir")
    
    filename="${dir}_${filename}"
    target="$parent/$filename"
    
    # Check if target already exists
    if [ -f "$target" ]; then
        echo "SKIP: $filepath -> $target (already exists)"
    else
        echo "MOVE: $filepath -> $target"
       mv "$filepath" "$filename"
    fi
done

echo ""
echo "Reorganization complete!"
