#!/bin/bash

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

DIRECTORY=$1

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

# Set variables
IMAGE_ DIR="$DIRECTORY"
OUTPUT_FILE="index.html"

# Generate gallery with captions
count=1
for img in $IMAGE_DIR/*.{jpg,jpeg,png,gif,webp}; do
    if [ -f "$img" ]; then
        filename=$(basename "$img")
        caption_file="${filename%.*}.txt"
        
        # Read caption if available, otherwise use default
        if [ -f "$caption_file" ]; then
            caption=$(cat "$caption_file")
        else
            caption=""
        fi

        echo " <div class=\"gallery-item\">" >> $OUTPUT_FILE
        echo "  <a href=\"#lightbox$count\"><figure class=\"image is-square\"> <img src=\"$IMAGE_DIR/$filename\" alt=\"$caption\"></figure></a>" >> $OUTPUT_FILE
        echo "  <div class=\"caption\">$caption</div>" >> $OUTPUT_FILE
        echo " </div>" >> $OUTPUT_FILE

        count=$((count+1))
    fi
done

# Add closing tags for the gallery
cat >> $OUTPUT_FILE <<EOL
 </div>
</div>
<footer class="footer has-background-primary-light">
 <div class="content has-text-centered">
  <h7>Copyright &copy; <span id="year"></span> Beach Party Balloons</h7>
  <h7>All images & content are property of Beach Party Balloons. Use of images without written permission is prohibited.</h7>
 </div>
</footer>
<div id="footer"></div>
<script src="$IMAGE_DIR/script.js"></script>
EOL

# Add lightbox overlays with captions
count=1
for img in $IMAGE_DIR/*.{jpg,jpeg,png,gif,webp}; do
    if [ -f "$img" ]; then
        filename=$(basename "$img")
        caption_file="${filename%.*}.txt"

        if [ -f "$caption_file" ]; then
            caption=$(cat "$caption_file")
        else
            caption=""
        fi

        echo " <div id=\"lightbox$count\" class=\"lightbox\">