{% if not helpers.empty('OPNsense.Syslog.destinations.destination') %}
{%    for destination in helpers.toList('OPNsense.Syslog.destinations.destination') %}
{%        if destination.enabled|default('0')  == '1' %}
{%            set dest_key = destination['@uuid']|replace('-', '')%}
{%            set applied_filters = [] %}

### log target {{destination['@uuid']}} : {{destination.description|default("")}} ####
{%            for key in ['program', 'level', 'facility'] %}
{%                if destination[key] %}
filter f_{{dest_key}}_{{key}} {
{%                    if key == 'program' %}
{#                        program filters need to use separate clauses, a bit of magic to wrap the value in program("ITEM") #}
{%                        set prog_list = ('program("%s")' % destination[key]).replace(',', '"),program("').split(',') %}
    {{ prog_list|join(' or ') }}
{%                    else %}
{#                        comma separated lists #}
    {{ "%s(%s)" % (key, destination[key]) }}
{%                    endif %}
};
{%                    do applied_filters.append(key) %}
{%                endif %}
{%            endfor %}

destination d_{{dest_key}} {
{%           if destination.transport in ['udp4', 'udp6', 'tcp4', 'tcp6', 'tls4', 'tls6'] %}
    network(
        "{{destination.hostname}}"
        transport("{{destination.transport[:3]}}")
        port({{destination.port}})
        ip-protocol({{destination.transport[3]}})
{%             if destination.rfc5424|default('0') == '1' %}
        flags(syslog-protocol)
{%             endif %}
        persist-name("{{dest_key}}")
{%             if destination.transport in ['tls4', 'tls6'] %}
        tls(
                ca-dir("/etc/ssl/certs")
                crl-dir("/etc/ssl/certs")
                key-file("/usr/local/etc/syslog-ng/cert.d/{{dest_key}}.key")
                cert-file("/usr/local/etc/syslog-ng/cert.d/{{dest_key}}.crt")
        )
{%             endif %}
    );
{%           endif %}
};

log {
    source(s_all);
{%            for filter in applied_filters %}
    filter(f_{{dest_key}}_{{filter}});
{%            endfor %}
    destination(d_{{dest_key}});
};
{%        endif %}
{%    endfor%}
{% endif %}
