{% from 'macros/common_macros.lua.j2' import string_table, boolean %}
{% from 'macros/policy_macros.lua.j2' import policy_get_tagset, policy_todname %}


{%- macro local_data_ttl(ttl) -%}
{%- if ttl -%}
{{ ttl.seconds() }}
{%- else -%}
{{ 'C.KR_RULE_TTL_DEFAULT' }}
{%- endif -%}
{%- endmacro -%}


{% macro kr_rule_local_address(name, address, nodata, ttl, tags=none) -%}
assert(C.kr_rule_local_address('{{ name }}', '{{ address }}',
    {{ boolean(nodata) }}, {{ local_data_ttl(ttl)}}, {{ policy_get_tagset(tags) }},
    C.KR_RULE_OPTS_DEFAULT) == 0)
{%- endmacro -%}


{% macro local_data_addresses(pairs, nodata, ttl) -%}
{% for name, addresses in pairs.items() %}
{% for address in addresses %}
{{ kr_rule_local_address(name, address, nodata, ttl) }}
{% endfor %}
{% endfor%}
{%- endmacro %}


{% macro kr_rule_local_hosts(file, nodata, ttl, tags=none) -%}
assert(C.kr_rule_local_hosts('{{ file }}', {{ boolean(nodata) }},
    {{ local_data_ttl(ttl)}}, {{ policy_get_tagset(tags) }}, C.KR_RULE_OPTS_DEFAULT) == 0)
{%- endmacro %}


{% macro local_data_addresses_files(files, nodata, ttl, tags) -%}
{% for file in files %}
{{ kr_rule_local_hosts(file, nodata, ttl, tags) }}
{% endfor %}
{%- endmacro %}


{% macro local_data_records(input_str, is_rpz, nodata, ttl, extra, id='rrs') -%}
{{ id }} = ffi.new('struct kr_rule_zonefile_config')
{{ id }}.ttl = {{ local_data_ttl(ttl) }}
{{ id }}.tags = {{ policy_get_tagset(extra.tags) }}
{{ id }}.nodata = {{ boolean(nodata) }}
{{ id }}.is_rpz = {{ boolean(is_rpz) }}
{% if is_rpz -%}
{{ id }}.filename = '{{ input_str }}'
{% else %}
{{ id }}.input_str = [[
{{ input_str.multiline() }}
]]
{% endif %}
{# .opts are complicated #}
{{ id }}.opts = C.KR_RULE_OPTS_DEFAULT
{% if extra is not none -%}
{%   if false and extra.dry_run is not none and extra.dry_run -%}
{{     id }}.opts.score = 4
{%   else %}
{{     id }}.opts.score = 9
{%   endif %}
{%   if 'log' in extra and extra.log is not none -%}
{{     id }}.opts.log_level = 3  -- notice
{%     if 'ip' in extra.log -%}
{{       id }}.opts.log_ip = true
{%     endif %}
{%     if 'name' in extra.log -%}
{{       id }}.opts.log_name = true
{%     endif %}
{%   endif %}
{% endif %}
assert(C.kr_rule_zonefile({{ id }})==0)
{%- endmacro %}


{% macro kr_rule_local_subtree(name, type, ttl, tags=none) -%}
assert(C.kr_rule_local_subtree(todname('{{ name }}'),
    C.KR_RULE_SUB_{{ type.upper() }}, {{ local_data_ttl(ttl) }}, {{ policy_get_tagset(tags) }},
    C.KR_RULE_OPTS_DEFAULT) == 0)
{%- endmacro %}


{% macro local_data_rules(items, nodata, ttl) -%}
{% for item in items %}
{% if item.name %}
{% for name in item.name %}
{% if item.address %}
{% for address in item.address %}
{{ kr_rule_local_address(name, address, nodata if item.nodata is none else item.nodata, item.ttl or ttl, item.tags) }}
{% endfor %}
{% endif %}
{% if item.subtree %}
{{ kr_rule_local_subtree(name, item.subtree, item.ttl or ttl, item.tags) }}
{% endif %}
{% endfor %}
{% elif item.file %}
{% for file in item.file %}
{{ kr_rule_local_hosts(file, nodata if item.nodata is none else item.nodata, item.ttl or ttl, item.tags) }}
{% endfor %}
{% elif item.records %}
{{ local_data_records(item.records, false, nodata if item.nodata is none else item.nodata, item.ttl or ttl, item) }}
{% endif %}
{% endfor %}
{%- endmacro %}
