#!/bin/bash
#
# This is an example. DO NOT SOURCE IT !

function enable_log_collection() {
    SERVICE=$(grep -oP '^\s{2}\w+' $1 | grep -Ev "nginx" |sort | uniq| xargs)
    sed -i -r '/^\s{4}logging:/,/^$/{/^$/!d}' $1
    for svc in ${SERVICE[@]}; do
cat >/tmp/enable_log_collection.tmp<<EOF
    logging:
      driver: "syslog"
      options:
        syslog-address: "udp://logserver:55514"
        syslog-format: "rfc3164"
        tag: "${svc}"

EOF
        sed -i -r "/^services/,/^networks/{
            /^\s{2}${svc}/,/^$/ {
                /^$/ r /tmp/enable_log_collection.tmp
        }}" $1
        sed -i -r "/^services/,/^networks/{
            /^\s{2}${svc}/,/^$/ {
                /^$/ d
        }}" $1
    done
}

enable_log_collection $@