{#
 # The MIT License (MIT)
 #
 # Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
 #
 # Permission is hereby granted, free of charge, to any person obtaining
 # a copy of this software and associated documentation files (the
 # "Software"), to deal in the Software without restriction, including
 # without limitation the rights to use, copy, modify, merge, publish,
 # distribute, sublicense, and/or sell copies of the Software, and to
 # permit persons to whom the Software is furnished to do so, subject to
 # the following conditions:
 #
 # The above copyright notice and this permission notice shall be
 # included in all copies or substantial portions of the Software.
 #
 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #}
{% import 'common.j2' as common %}
/* CTF 1.8 */

{% include 'license-header.j2' %}


{#
 # Generates a root field type string named `name` for the field type
 # `rft` with all the lines except the first one indented with a single
 # tab.
 #}
{% macro root_ft(name, rft) %}
{{ name }} := {{ rft | ft_str }};
{%- endmacro %}
trace {
	major = 1;
	minor = 8;
	byte_order = {{ 'le' if cfg.trace.type.trace_byte_order == barectf_config.ByteOrder.LITTLE_ENDIAN else 'be' }};
{% if cfg.trace.type.uuid %}
	uuid = "{{ cfg.trace.type.uuid }}";
{% endif %}
{% if cfg.trace.type._pkt_header_ft %}
	{{ root_ft('packet.header', cfg.trace.type._pkt_header_ft) | indent_tab }}
{% endif %}
};

env {
{% for name, value in cfg.trace.environment.items() %}
	{{ name }} = {{ value if value is number else '"{}"'.format(value | escape_dq) }};
{% endfor %}
};

{# all clock types (data stream types's default clock types) #}
{% for clk_type in cfg.trace.type.clock_types | sort %}
clock {
	name = {{ clk_type.name }};
	{% if clk_type.description %}
	description = "{{ clk_type.description | escape_dq }}";
	{% endif %}
	{% if clk_type.uuid %}
	uuid = "{{ clk_type.uuid }}";
	{% endif %}
	freq = {{ clk_type.frequency }};
	precision = {{ clk_type.precision }};
	offset_s = {{ clk_type.offset.seconds }};
	offset = {{ clk_type.offset.cycles }};
	absolute = {{ 'true' if clk_type.origin_is_unix_epoch else 'false' }};
};

{% endfor %}
{# data stream types and their event record types #}
{% for dst in cfg.trace.type.data_stream_types | sort %}
/* Data stream type `{{ dst.name }}` */
stream {
	{% if cfg.trace.type.features.data_stream_type_id_field_type %}
	id = {{ dst.id }};
	{% endif %}
	{{ root_ft('packet.context', dst._pkt_ctx_ft) | indent_tab }}
	{% if dst._er_header_ft %}
	{{ root_ft('event.header', dst._er_header_ft) | indent_tab }}
	{% endif %}
	{% if dst.event_record_common_context_field_type %}
	{{ root_ft('event.context', dst.event_record_common_context_field_type) | indent_tab }}
	{% endif %}
};

	{# data stream type's event record types #}
	{% for ert in dst.event_record_types | sort %}
event {
	{% if cfg.trace.type.features.data_stream_type_id_field_type %}
	stream_id = {{ dst.id }};
	{% endif %}
	id = {{ ert.id }};
	name = "{{ ert.name }}";
		{% if ert.log_level %}
	loglevel = {{ ert.log_level }};
		{% endif %}
		{% if ert.specific_context_field_type %}
	{{ root_ft('context', ert.specific_context_field_type) | indent_tab }}
		{% endif %}
		{% if ert.payload_field_type %}
	{{ root_ft('fields', ert.payload_field_type) | indent_tab }}
		{% endif %}
};

	{% endfor %}
{% endfor %}
