#!/bin/bash
# Cache-busting: update ?v= query param in all HTML files when CSS/JS change.
# Version = first 8 chars of a combined hash of style.css + main.js.

REPO_ROOT=$(git rev-parse --show-toplevel)
CSS="$REPO_ROOT/css/style.css"
JS="$REPO_ROOT/js/main.js"

VERSION=$(cat "$CSS" "$JS" | git hash-object --stdin | cut -c1-8)

HTML_FILES=$(find "$REPO_ROOT" -name "index.html" -not -path "*/raw/*")

for file in $HTML_FILES; do
  sed -i -E "s|(\.\.\/)?css/style\.css(\?v=[^\"']*)?|\1css/style.css?v=$VERSION|g" "$file"
  sed -i -E "s|(\.\.\/)?js/main\.js(\?v=[^\"']*)?|\1js/main.js?v=$VERSION|g" "$file"
  git add "$file"
done

echo "Cache-bust version: $VERSION"
