#!/usr/bin/env bash

# Rebuild the hub home page, validate it, push it, and refresh the private OSS copy.
set -euo pipefail

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$repo_root"

bash scripts/pre_question_sync.sh
python3 scripts/refresh_hub_index.py

if rg -n 'file://|/Users/' index.html; then
  printf 'ERROR: generated index contains a local path.\n' >&2
  exit 1
fi
git diff --check

if ! git diff --quiet -- index.html; then
  git add index.html
  git commit -m "docs: refresh research institute hub home page"
  git push origin master
fi

bash scripts/publish_html_to_research_institute_oss.sh index.html index.html
for page in imports/*.html; do
  [[ -f "$page" ]] || continue
  bash scripts/publish_html_to_research_institute_oss.sh "$page" "$page"
done

printf 'HUB_REFRESH_OK updated=%s\n' "$(date '+%Y-%m-%d %H:%M:%S %z')"
