#!/bin/bash # Generate index.html files for HDF5 release directories # Usage: generate-index-html.sh <description> [parent_url] set -euo pipefail DIRECTORY="${1:-}" TITLE="${2:-Index}" DESCRIPTION="${3:-}" PARENT_URL="${4:-../}" if [ -z "$DIRECTORY" ]; then echo "Usage: $0 <directory> <title> <description> [parent_url]" exit 1 fi if [ ! -d "$DIRECTORY" ]; then echo "Error: Directory '$DIRECTORY' does not exist" exit 1 fi OUTPUT_FILE="$DIRECTORY/index.html" # Get file information with size and modification time generate_file_list() { local dir="$1" local files=() # Find all files and directories (excluding index.html itself) while IFS= read -r -d '' item; do if [ "$(basename "$item")" != "index.html" ]; then files+=("$item") fi done < <(find "$dir" -maxdepth 1 ! -path "$dir" -print0 | sort -z) # Generate HTML list items for item in "${files[@]}"; do local name=$(basename "$item") local rel_path="$name" local size="" local modified="" local item_type="file" if [ -d "$item" ]; then item_type="dir" rel_path="$name/" # Count items in directory local count=$(find "$item" -maxdepth 1 ! -path "$item" | wc -l) size="$count items" else # Get file size in human-readable format size=$(ls -lh "$item" | awk '{print $5}') # Get modification time if [[ "$OSTYPE" == "darwin"* ]]; then modified=$(stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$item") else modified=$(stat -c "%y" "$item" | cut -d'.' -f1) fi fi # Determine file description based on extension local desc="" case "$name" in *.tar.gz|*.tgz) desc="Source tarball" ;; *.zip) if [[ "$name" == *"doxygen"* ]]; then desc="Doxygen documentation" elif [[ "$name" == *"win"* ]] || [[ "$name" == *"WIN"* ]]; then desc="Windows binary package" else desc="Source archive" fi ;; *.msi) desc="Windows installer" ;; *.exe) desc="Windows executable installer" ;; *.dmg) desc="macOS disk image" ;; *.deb) desc="Debian/Ubuntu package" ;; *.rpm) desc="Red Hat/Fedora package" ;; *abi.reports*) desc="ABI compatibility reports" ;; SHA256*) desc="SHA256 checksums" ;; downloads) desc="Release binaries and source code" ;; documentation) desc="API documentation and user guides" ;; doxygen) desc="Doxygen API documentation" ;; compat_report) desc="ABI/API compatibility reports" ;; *) if [ "$item_type" == "dir" ]; then desc="Directory" else desc="File" fi ;; esac # Output HTML row if [ "$item_type" == "dir" ]; then echo " <tr class='dir'>" echo " <td class='name'><a href='$rel_path'>📁 $name/</a></td>" echo " <td class='size'>$size</td>" echo " <td class='modified'>-</td>" echo " <td class='description'>$desc</td>" echo " </tr>" else echo " <tr class='file'>" echo " <td class='name'><a href='$rel_path'>📄 $name</a></td>" echo " <td class='size'>$size</td>" echo " <td class='modified'>$modified</td>" echo " <td class='description'>$desc</td>" echo " </tr>" fi done } # Generate the index.html file cat > "$OUTPUT_FILE" << 'EOF' <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>INDEX_TITLE_PLACEHOLDER

INDEX_TITLE_PLACEHOLDER

HDF5 (Hierarchical Data Format 5) Software Library and Utilities
INDEX_DESCRIPTION_PLACEHOLDER
FILE_LIST_PLACEHOLDER
Name Size Modified Description
EOF # Replace placeholders sed -i.bak "s|INDEX_TITLE_PLACEHOLDER|$TITLE|g" "$OUTPUT_FILE" sed -i.bak "s|INDEX_DESCRIPTION_PLACEHOLDER|$DESCRIPTION|g" "$OUTPUT_FILE" sed -i.bak "s|PARENT_URL_PLACEHOLDER|$PARENT_URL|g" "$OUTPUT_FILE" # Generate and insert file list FILE_LIST=$(generate_file_list "$DIRECTORY") if [ -z "$FILE_LIST" ]; then FILE_LIST=" No files or directories found" fi # Create secure temporary file TEMP_FILE=$(mktemp) || { echo "Error: Failed to create temporary file" exit 1 } # Ensure cleanup on exit trap 'rm -f "$TEMP_FILE"' EXIT # Use a different delimiter for sed since the content contains slashes echo "$FILE_LIST" > "$TEMP_FILE" sed -i.bak "/FILE_LIST_PLACEHOLDER/r $TEMP_FILE" "$OUTPUT_FILE" sed -i.bak "/FILE_LIST_PLACEHOLDER/d" "$OUTPUT_FILE" # Clean up backup files rm -f "$OUTPUT_FILE.bak" echo "✅ Generated index.html at: $OUTPUT_FILE"