#!/usr/bin/env bash
# Backfill Coraza WAF log rotation onto already-deployed servers on update.
# (Fresh installs / rebuilds get it via setup.sh.)
#
# Thin wrapper: the real, idempotent installer is the stable, never-trimmed
# scripts/setup-coraza-logrotate.sh. update.sh runs versioned update scripts
# BEFORE rsyncing scripts/ to /root/whp/scripts/, so resolve the installer from
# the extracted tarball layout first:
#   <root>/scripts/updates/<version>/setup-coraza-logrotate.sh  <- this file
#   <root>/scripts/setup-coraza-logrotate.sh                     <- installer
# falling back to the deployed path for re-runs after the rsync has happened.
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INSTALLER="$(cd "${SCRIPT_DIR}/../.." 2>/dev/null && pwd)/setup-coraza-logrotate.sh"
if [[ ! -f "${INSTALLER}" ]]; then
    INSTALLER="/root/whp/scripts/setup-coraza-logrotate.sh"
fi
if [[ ! -f "${INSTALLER}" ]]; then
    echo "ERROR: setup-coraza-logrotate.sh not found in tarball or /root/whp/scripts" >&2
    exit 1
fi

exec bash "${INSTALLER}"
