#!/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"

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

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

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