#!/bin/bash

# Configuration
DUMP_DIR="dump"
OUTPUT_DIR="output"
TEX_CMD="pdflatex"

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

# Check if dump directory exists
if [ ! -d "$DUMP_DIR" ]; then
    echo "Error: $DUMP_DIR directory does not exist"
    exit 1
fi

# Process all .tex files in the dump directory
echo "Processing .tex files from $DUMP_DIR..."
for tex_file in "$DUMP_DIR"/*.tex; do
    # Check if there are any .tex files
    if [ ! -f "$tex_file" ]; then
        echo "No .tex files found in $DUMP_DIR"
        exit 0
    fi
    
    filename=$(basename "$tex_file")
    echo "Processing $filename..."
    
    # Run pdflatex
    $TEX_CMD -output-directory="$OUTPUT_DIR" "$tex_file"
    
    # Check if successful
    if [ $? -eq 0 ]; then
        echo "Successfully generated PDF for $filename"
    else
        echo "Error generating PDF for $filename"
    fi
done

echo "All processing complete. Output files are in $OUTPUT_DIR"