#!/usr/bin/env bash
set -Eeuo pipefail

ROOT="/mnt/bigstor/www/usecases"
VENV="$ROOT/.venv"
LOG_DIR="$ROOT/logs"
LOCK_FILE="$ROOT/.publish_weekly.lock"
STAMP="$(date '+%Y%m%d-%H%M%S')"
LOG_FILE="$LOG_DIR/weekly-$STAMP.log"
CORRELATIONS="evidence/threat_correlations.json"
REGISTER="data/threats_register.json"
STATUS_FILE="$ROOT/site/update_status.json"

mkdir -p "$LOG_DIR"
cd "$ROOT"

exec 9>"$LOCK_FILE"
if ! flock -n 9; then
    echo "[$(date '+%F %T')] ERROR weekly publication already running" >> "$LOG_FILE"
    exit 75
fi

exec >> >(tee -a "$LOG_FILE") 2>&1

trap 'rc=$?; echo "[$(date "+%F %T")] ERROR line=$LINENO command=$BASH_COMMAND exit=$rc"; exit $rc' ERR

echo "============================================================"
echo "[$(date '+%F %T')] WEEKLY CORRELATION / PUBLICATION START"
echo "Root: $ROOT"
echo "Log:  $LOG_FILE"
echo "============================================================"

if [ -f "$VENV/bin/activate" ]; then
    # shellcheck disable=SC1091
    source "$VENV/bin/activate"
fi

run_step() {
    local name="$1"
    shift
    echo
    echo "[$(date '+%F %T')] START $name"
    "$@"
    local rc=$?
    echo "[$(date '+%F %T')] DONE  $name exit=$rc"
}

# Correlate first. Site and export are only rebuilt if this succeeds.
run_step "Evidence correlation" python3 correlate_evidence.py --force

# Validate the correlation products before publishing them.
for json_file in \
    evidence/threat_correlations.json \
    evidence/evidence_index.json \
    evidence/correlation_run.json \
    evidence/correlation_review.json
do
    if [ -f "$json_file" ]; then
        echo "[$(date '+%F %T')] VALIDATE $json_file"
        python3 -m json.tool "$json_file" >/dev/null
    fi
done

# Rebuild the individual dashboard source pages when their builders are present.
for builder in \
    build_github_page.py \
    build_cves_page.py \
    build_papers_page.py \
    build_signals_page.py \
    build_incidents_page.py
do
    if [ -f "$builder" ]; then
        run_step "$builder" python3 "$builder" --correlations "$CORRELATIONS"
    else
        echo "[$(date '+%F %T')] SKIP $builder not present"
    fi
done

# Build Threat Register index + T-xxx detail pages.
run_step "Threat Register site" \
    python3 build_site.py site \
        --register "$REGISTER" \
        --correlations "$CORRELATIONS"

# About is maintained as a stable static page. Do not overwrite it here.
if [ -f about.html ]; then
    cp -p about.html site/about.html
    echo "[$(date '+%F %T')] COPIED about.html -> site/about.html"
fi

# Update correlation/publication status only after correlation and all site builds succeeded.
mkdir -p "$(dirname "$STATUS_FILE")"
python3 - "$STATUS_FILE" <<'PY_STATUS'
import json
import os
import sys
import tempfile
from datetime import datetime
from pathlib import Path

path = Path(sys.argv[1])
data = {
    "collection_schedule": "Daily",
    "correlation_schedule": "Weekly",
    "last_collection": None,
    "last_correlation": None,
    "last_publication": None,
}
if path.exists():
    try:
        current = json.loads(path.read_text(encoding="utf-8"))
        if isinstance(current, dict):
            data.update(current)
    except (OSError, json.JSONDecodeError):
        pass

now = datetime.now().astimezone().isoformat(timespec="seconds")
data["collection_schedule"] = "Daily"
data["correlation_schedule"] = "Weekly"
data["last_correlation"] = now
data["last_publication"] = now

fd, tmp_name = tempfile.mkstemp(prefix=path.name + ".", suffix=".tmp", dir=path.parent)
try:
    with os.fdopen(fd, "w", encoding="utf-8") as handle:
        json.dump(data, handle, indent=2, ensure_ascii=False)
        handle.write("\n")
        handle.flush()
        os.fsync(handle.fileno())
    os.replace(tmp_name, path)
except Exception:
    try:
        os.unlink(tmp_name)
    except FileNotFoundError:
        pass
    raise
PY_STATUS
python3 -m json.tool "$STATUS_FILE" >/dev/null
echo "[$(date '+%F %T')] UPDATED $STATUS_FILE"

# Make the shareable, validated data snapshot only after the site/status update succeeds.
run_step "NCRC data export" ./make_ncrc_export.sh

echo
echo "[$(date '+%F %T')] WEEKLY PUBLICATION COMPLETE"
