#!/bin/bash

# Check if the directory is provided
if [ -z "$1" ]; then
    echo "Usage: $0 directory"
    exit 1
fi

DIRECTORY=$1
OUTPUT_DIRECTORY="./output"

# Check if the provided directory exists
if [ ! -d "$DIRECTORY" ]; then
    echo "The directory $DIRECTORY does not exist."
    exit 1
fi

# Create the output directory if it doesn't exist
mkdir -p "$OUTPUT_DIRECTORY"

# Iterate over all PDF files in the directory
for pdf_file in "$DIRECTORY"/*.pdf
do
    # Check if there are no PDF files
    if [ ! -e "$pdf_file" ]; then
        echo "No PDF files found in the directory."
        exit 1
    fi

    # Get the filename without extension
    filename=$(basename "$pdf_file" .pdf)

    # Convert the PDF to HTML and save it to the output directory
    pdftohtml "$pdf_file" "$OUTPUT_DIRECTORY/$filename.html"

    echo "Converted $pdf_file to $OUTPUT_DIRECTORY/$filename.html"
done

echo "All PDF files have been converted."
