#!/bin/bash

# Directory containing PDF files
pdf_directory=`pwd`

# Function to process each PDF
process_pdf() {
    pdf_file=$1
    echo "Processing $pdf_file"
    base_name=$(basename "$pdf_file" .pdf)

    local base_name=$(basename "$pdf_file" .pdf)

    # Create a directory with the base name in the same location as the PDF
    local dir_path=$(dirname "$pdf_file")  # Get the directory path of the PDF
    mkdir -p "$dir_path/png/$base_name"

    pushd "$dir_path/png/$base_name" 
    # Convert the PDF to a set of images
    magick -resize x960 -quality 100 -density 200 -colorspace sRGB "$pdf_file" "Slide%02d.png"

    # Return to the original directory
   popd > /dev/null
}

export -f process_pdf

# Find all PDFs and pass them to GNU Parallel
find "$pdf_directory" -name "*.pdf" | parallel process_pdf