
#!/bin/bash

CSV_FILE="contenus_seidn_images.csv"
DEST_FOLDER="images"

# Create destination folder if it doesn't exist
mkdir -p "$DEST_FOLDER"

# Read file line by line
while IFS=',' read -r name urls; do
    # Skip header lines
    if [[ "$name" == "Vignette" ]]; then
        continue
    fi

 # Skip header lines

    if [[ "$name" == "Vignette2" ]]; then
        continue
    fi


    # Clean quotes and whitespace
    urls=$(echo "$urls" | tr -d '"' | xargs)

    # Skip empty lines
    if [[ -z "$name" || -z "$urls" ]]; then
        continue
    fi

    # Split URLs and download each
    IFS=',' read -ra url_array <<< "$urls"
    for url in "${url_array[@]}"; do
        filename=$(basename "$url")
        curl -s "$url" -o "$DEST_FOLDER/$filename"
        echo "$name → $filename téléchargé"
    done
done < "$CSV_FILE"
