#!/bin/sh
set -eu

BASE="/opt/router-ops"
PUBLIC="$BASE/public"
TOKEN="$(cat "$PUBLIC/.router-public-token")"
ROOT="$PUBLIC/r/$TOKEN"
INDEX="$ROOT/index.html"

{
  echo '<!doctype html><html><head><meta charset="utf-8"><title>router-ops reports</title></head><body>'
  echo '<h1>router-ops reports</h1>'
  echo '<p>Sanitized published files only. Do not publish raw captures, secrets, keys, .env, WireGuard configs, or HMN access codes.</p>'

  echo '<h2>Latest</h2>'
  echo '<ul>'
  if [ -d "$ROOT/latest" ]; then
    find "$ROOT/latest" -maxdepth 1 -type f | sort | while read -r f; do
      b="$(basename "$f")"
      echo "<li><a href=\"latest/$b\">latest/$b</a></li>"
    done
  fi
  echo '</ul>'

  echo '<h2>Report folders</h2>'
  echo '<ul>'
  find "$ROOT" -mindepth 1 -maxdepth 1 -type d ! -name latest | sort -r | while read -r d; do
    b="$(basename "$d")"
    echo "<li><a href=\"$b/\">$b/</a></li>"
  done
  echo '</ul>'

  echo '<h2>Root files</h2>'
  echo '<ul>'
  find "$ROOT" -maxdepth 1 -type f ! -name index.html | sort | while read -r f; do
    b="$(basename "$f")"
    echo "<li><a href=\"$b\">$b</a></li>"
  done
  echo '</ul>'

  echo '</body></html>'
} > "$INDEX"

chmod 644 "$INDEX"
echo "INDEX=$INDEX"
