#!/usr/bin/env bash
# Backfill the backup I/O throttle (whp-backup.slice cgroup v2 io.max +
# whp-backup-runner) onto already-deployed servers on update.
#
# The real, idempotent installer is the stable, never-trimmed
# scripts/setup-backup-throttle.sh — the same one setup.sh runs for fresh
# installs/rebuilds. This thin wrapper just invokes it so every existing server
# picks up the throttle on its next update (it was previously installed only by
# a one-time versioned update script that release-trimming has since dropped, so
# rebuilt/fresh servers — e.g. whp02 — were running backups un-throttled).
#
# Resolve the stable installer $BASH_SOURCE-relative: update.sh runs versioned
# scripts BEFORE rsyncing scripts/ to /root/whp/scripts/, so /root/whp/scripts/
# is stale during the run. Within the extracted tarball the layout is:
#   <root>/scripts/updates/<version>/setup-backup-throttle.sh   <- this file
#   <root>/scripts/setup-backup-throttle.sh                     <- stable installer
#   <root>/scripts/whp-backup-runner.sh
#   <root>/scripts/apply-backup-throttle.php
#
# Non-fatal: I/O throttling is hardening, not a panel prerequisite. A server
# without cgroup v2 (the installer's only hard failure on a healthy host) must
# not fail the whole update.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
STABLE="${SCRIPT_DIR}/../../setup-backup-throttle.sh"

if [[ ! -f "${STABLE}" ]]; then
    echo "ERROR: stable installer not found at ${STABLE} (tarball layout changed?)" >&2
    exit 1
fi

if bash "${STABLE}"; then
    echo "setup-backup-throttle.sh (staging backfill): OK"
else
    echo "WARN: backup I/O throttle setup skipped (see output above); continuing update." >&2
fi
