#!/bin/sh
# Build Clawdie-ISO and publish a styled HTML build log to the website.
#
# Usage:
#   sudo ./scripts/build-and-log.sh [build.sh args...]
#
# Publishes to: /usr/local/www/clawdie/docs/iso-build-log.html
# URL:          https://clawdie.si/docs/iso-build-log.html
#
# A timestamped archive copy is also kept alongside so past builds
# are not overwritten:
#   /usr/local/www/clawdie/docs/iso-build-YYYY-MM-DD.html

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
TMP_DIR="${SCRIPT_DIR}/tmp"
WEBROOT="/usr/local/www/clawdie/docs"
BUILD_DATE="$(date '+%Y-%m-%d')"
BUILD_TS="$(date '+%Y-%m-%d %H:%M:%S %Z')"
LOG_TMP="${TMP_DIR}/clawdie-iso-build-$$.log"
HTML_LATEST="${WEBROOT}/iso-build-log.html"
HTML_DATED="${WEBROOT}/iso-build-${BUILD_DATE}.html"

mkdir -p "${WEBROOT}" "${TMP_DIR}"

echo "=== Clawdie-ISO Build ==="
echo "Started: ${BUILD_TS}"
echo "Log:     ${LOG_TMP}"
echo "Output:  ${HTML_LATEST}"
echo ""

# ── Run the build, capture everything ────────────────────────────────────
START_EPOCH="$(date +%s)"

set +e
cd "${SCRIPT_DIR}"
./build.sh "$@" 2>&1 | tee "${LOG_TMP}"
BUILD_EXIT=$?
set -e

END_EPOCH="$(date +%s)"
DURATION_S=$(( END_EPOCH - START_EPOCH ))
DURATION_M=$(( DURATION_S / 60 ))
DURATION_S=$(( DURATION_S % 60 ))
DURATION="${DURATION_M}m ${DURATION_S}s"

if [ "${BUILD_EXIT}" -eq 0 ]; then
  STATUS_TEXT="SUCCESS"
  STATUS_CLASS="success"
else
  STATUS_TEXT="FAILED (exit ${BUILD_EXIT})"
  STATUS_CLASS="failure"
fi

# ── Escape HTML special chars in log output ───────────────────────────────
LOG_HTML="$(sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g' "${LOG_TMP}")"

# ── Generate HTML ─────────────────────────────────────────────────────────
cat > "${HTML_LATEST}" << HTMLEOF
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>ISO Build Log — Clawdie AI</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link href="https://fonts.googleapis.com/css2?family=Cormorant+Garamond:ital,wght@0,300;0,400;0,600;1,300;1,400&family=DM+Mono:wght@300;400&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="/css/shared.css">
  <style>
    .build-header {
      padding: 2rem 0 1.5rem;
      border-bottom: 1px solid var(--grey-light);
      margin-bottom: 2rem;
    }
    .build-header h1 {
      font-family: 'Cormorant Garamond', serif;
      font-size: 2rem;
      font-weight: 300;
      color: var(--charcoal);
      margin: 0 0 0.5rem;
    }
    .build-meta {
      display: flex;
      gap: 2rem;
      flex-wrap: wrap;
      font-family: 'DM Mono', monospace;
      font-size: 0.8rem;
      color: var(--grey);
    }
    .build-meta span { white-space: nowrap; }
    .build-meta .label { color: var(--muted); margin-right: 0.4rem; }
    .status-badge {
      display: inline-block;
      font-family: 'DM Mono', monospace;
      font-size: 0.75rem;
      font-weight: 400;
      padding: 0.2rem 0.7rem;
      border-radius: 2px;
      letter-spacing: 0.05em;
    }
    .status-badge.success {
      background: rgba(74, 222, 128, 0.12);
      color: var(--green);
      border: 1px solid rgba(74, 222, 128, 0.25);
    }
    .status-badge.failure {
      background: rgba(248, 113, 113, 0.12);
      color: var(--red);
      border: 1px solid rgba(248, 113, 113, 0.25);
    }
    .log-block {
      background: #060a0f;
      border: 1px solid var(--grey-light);
      padding: 1.5rem;
      overflow-x: auto;
      margin: 1.5rem 0;
    }
    .log-block pre {
      font-family: 'DM Mono', monospace;
      font-size: 0.78rem;
      line-height: 1.65;
      color: #8fa8c8;
      margin: 0;
      white-space: pre-wrap;
      word-break: break-word;
    }
    .log-block pre .ok   { color: var(--green); }
    .log-block pre .err  { color: var(--red); }
    .log-block pre .warn { color: var(--yellow); }
    .archive-note {
      font-family: 'DM Mono', monospace;
      font-size: 0.78rem;
      color: var(--muted);
      margin-top: 2rem;
      padding-top: 1rem;
      border-top: 1px solid var(--grey-light);
    }
    .archive-note a { color: var(--amber); text-decoration: none; }
    .archive-note a:hover { text-decoration: underline; }
  </style>
</head>
<body>
  <nav class="top-nav">
    <div class="nav-inner">
      <a class="brand" href="/">Clawdie<span>.</span></a>
      <div class="nav-links">
        <a href="/docs/">Docs</a>
        <a href="https://code.smilepowered.org/clawdie/clawdie-iso" target="_blank">ISO</a>
      </div>
    </div>
  </nav>

  <main class="page-content">
    <div class="build-header">
      <h1>ISO Build Log</h1>
      <div class="build-meta">
        <span><span class="label">date</span>${BUILD_TS}</span>
        <span><span class="label">duration</span>${DURATION}</span>
        <span><span class="label">status</span><span class="status-badge ${STATUS_CLASS}">${STATUS_TEXT}</span></span>
        <span><span class="label">branch</span>implementation</span>
      </div>
    </div>

    <div class="log-block">
      <pre>${LOG_HTML}</pre>
    </div>

    <div class="archive-note">
      Permalink for this build:
      <a href="/docs/iso-build-${BUILD_DATE}.html">iso-build-${BUILD_DATE}.html</a>
      &nbsp;·&nbsp;
      <a href="/docs/iso-build-log.html">Latest build</a>
    </div>
  </main>
</body>
</html>
HTMLEOF

# Copy dated archive
cp "${HTML_LATEST}" "${HTML_DATED}"

# Cleanup temp log
rm -f "${LOG_TMP}"

echo ""
echo "=== Build log published ==="
echo "  Latest:  https://clawdie.si/docs/iso-build-log.html"
echo "  Archive: https://clawdie.si/docs/iso-build-${BUILD_DATE}.html"
echo "  Status:  ${STATUS_TEXT}"
echo "  Duration: ${DURATION}"

exit "${BUILD_EXIT}"
