CONFIG_FILE="$1"

MQTT_BROKER="host"
MQTT_TOPIC="borgmatic/stats/$CONFIG_FILE"
MQTT_USER="borgmatic"
MQTT_PASSWORD="eH2dAiG7siCIFdB7qX0MHwEFzcr9aqUi"

# Run Borgmatic and extract archive stats as JSON
PAYLOAD=$(borgmatic \
    --stats \
    -v 0 \
    -c "/config/$CONFIG_FILE.yaml" \
    --log-json \
    --no-color \
    --json \
    | tee /dev/tty \
    | sed --quiet '/^\[/p' \
    | jq '.[0].archive'
)

# Publish to MQTT
mosquitto_pub -h "$MQTT_BROKER" \
               -t "$MQTT_TOPIC" \
               -u "$MQTT_USER" \
               -P "$MQTT_PASSWORD" \
               -m "$PAYLOAD"
