#!/bin/bash
# Generate an HTML file listing for a directory tree.
# Usage: ./make_listing.sh [directory] [base_url]
# Example: ./make_listing.sh /path/to/nswart https://crd-data-donnees-rdc.ec.gc.ca/CCCMA/ftp/nswart

DIR="${1:-.}"
BASE_URL="${2:-}"
OUTPUT="index.html"

cat > "$OUTPUT" <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Neil C. Swart — CCCma Data</title>
  <meta name="description" content="Research data and model configurations from Neil C. Swart, Research Scientist at CCCma, Environment and Climate Change Canada. CanESM5, CMIP6." />
  <style>
    body { font-family: monospace; max-width: 900px; margin: 2em auto; padding: 0 1em; }
    a { color: #0066cc; }
    li { margin: 0.2em 0; }
    h2 { margin-top: 2em; border-bottom: 1px solid #ccc; }
  </style>
</head>
<body>
  <h1>Neil C. Swart — CCCma Research Data</h1>
  <p>Research Scientist, Canadian Centre for Climate Modelling and Analysis (CCCma),
  Environment and Climate Change Canada.</p>
  <p>Personal website: <a href="https://swartn.github.io">swartn.github.io</a></p>
  <hr/>
  <h2>Files</h2>
  <ul>
EOF

# List top-level items only; dirs link to their Apache listing, files link directly
find "$DIR" -mindepth 1 -maxdepth 1 | sort | while read -r item; do
    rel="${item#$DIR/}"

    if [[ -n "$BASE_URL" ]]; then
        href="${BASE_URL}/${rel}"
    else
        href="${rel}"
    fi

    if [[ -d "$item" ]]; then
        echo "    <li>📁 <a href=\"${href}/\">${rel}/</a></li>" >> "$OUTPUT"
    else
        echo "    <li>📄 <a href=\"${href}\">${rel}</a></li>" >> "$OUTPUT"
    fi
done

cat >> "$OUTPUT" <<EOF
  </ul>
</body>
</html>
EOF

echo "Written to $OUTPUT"

