#!/bin/bash

# Gitea install hooks — mirror CFG_GITEA_METRICS_TOKEN into the Prometheus
# scrape fragment so the bearer token in the compose env matches what the
# Prometheus side sends.

gitea_install_post_compose()
{
    local app_name="$1"

    # The driver already ran monitoringToggleAppConfig "$app_name" docker-compose.yml,
    # so the metrics block reflects CFG_GITEA_MONITORING. /metrics rides
    # Gitea's public web port and is token-protected; sync the token into
    # the scrape config so the two sides agree.
    if monitoringAppEnabled "$app_name"; then
        if [[ -n "$CFG_GITEA_METRICS_TOKEN" ]]; then
            local result
            result=$(runFileOp sed -i "s|GITEA_METRICS_TOKEN_PLACEHOLDER|${CFG_GITEA_METRICS_TOKEN}|g" \
                "$containers_dir$app_name/resources/monitoring/prometheus-scrape.yml")
            checkSuccess "Synced Gitea /metrics token to the Prometheus scrape config"
        else
            isNotice "CFG_GITEA_METRICS_TOKEN is empty — Gitea /metrics scrape may 401."
        fi
    fi
}
