# {{ ansible_managed }}

{% set opendkim__tpl_config = (opendkim__combined_config | parse_kv_config) %}
{% set opendkim__tpl_sections = ((opendkim__tpl_config | map(attribute='section')) | list | unique) %}
{% for section in opendkim__config_sections|d([ {'name': 'unknown'} ]) %}
{%   if section.state|d('present') != 'absent' %}
{%     set section_loop = [] %}
{%     set section_comment_prefix = ('' if loop.first|bool else '\n') %}
{%     if section.name in opendkim__tpl_sections %}
{%       if section.title|d() %}

{{ '{:-^70}'.format(' ' + section.title + ' ') | comment(prefix=section_comment_prefix, postfix='') }}
{%       endif %}
{%       for element in opendkim__tpl_config %}
{%         if element.section == section.name and element.state|d('present') != 'absent' %}
{%           set comment_prefix =   ('' if not section_loop else '\n') %}
{%           set option_commented = ('#' if element.state|d('present') == 'comment' else '') %}
{%           set option_indent =    ('27' if element.state|d('present') == 'comment' else '28') %}
{%           set element_name =     (element.option | d(element.name)) %}
{%           if element.value is defined %}
{%             if element.value | bool and element.value is not iterable %}
{%               if element.value | int and element.value | string != 'True' %}
{%                 set element_value = element.value %}
{%               else %}
{%                 set element_value = 'yes' %}
{%               endif %}
{%             elif not element.value | bool and element.value is not iterable %}
{%               if element.value is not none %}
{%                 if element.value | int or element.value | string == '0' %}
{%                   set element_value = element.value %}
{%                 else %}
{%                   set element_value = 'no' %}
{%                 endif %}
{%               endif %}
{%             elif element.value is not string and element.value is not mapping and element.value is iterable %}
{%               set element_value = [] %}
{%               for option in element.value %}
{%                 if option.state|d('present') != 'absent' %}
{%                   set _ = element_value.append(option.name) %}
{%                 endif %}
{%               endfor %}
{%             else %}
{%               set element_value = element.value %}
{%             endif %}
{%           else %}
{%             set element_value = [] %}
{%           endif %}
{%           if element.comment|d() %}
{{ element.comment | regex_replace('\n$','') | comment(prefix=comment_prefix, postfix='') -}}
{%             if element.state|d('present') == 'hidden' %}

{%             endif %}
{%           elif element.separator is defined and element.separator|bool %}

{%           endif %}
{%           if element.state|d('present') != 'hidden' %}
{%             if element_value is not string and element_value is not mapping and element_value is iterable %}
{{ ('{}{:<' + option_indent + '} {}').format(option_commented, element_name, element_value | join(',')) }}
{%             else %}
{{ ('{}{:<' + option_indent + '} {}').format(option_commented, element_name, element_value) }}
{%             endif %}
{%             set _ = section_loop.append(True) %}
{%           endif %}
{%         endif %}
{%       endfor %}
{%     endif %}
{%   endif %}
{% endfor %}
