{% set hardware_timestamps = slapparameter_dict.get('hardware-timestamps', 'n') -%}
{% set interface_ip_string_list = [] -%}
{% set interface_list = slapparameter_dict.get('interface-list', '').split() -%}
{% set ip_list = slapparameter_dict.get('ip-list', '').split() -%}
{% set part_list = [] -%}
{% set server_guid_list = slapparameter_dict.get('server-guid-list', '').split() -%}
{% set server_interface_list = slapparameter_dict.get('server-interface-list', '').split() -%}
{% set server_list = [] -%}
{% set script_list = [] -%}

[directory]
recipe = slapos.cookbook:mkdirectory
etc = $${buildout:directory}/etc
scripts = $${:etc}/tsn
run = $${:etc}/run

[tsn-script-template]
recipe  = slapos.recipe.template:jinja2
rendered= $${directory:scripts}/$${:_buildout_section_name_}
mode    = 755

{% for server_nb, server_guid in enumerate(server_guid_list) -%}
{%   set request_tsn_server_section_title = 'request-tsn-server' ~ server_nb -%}
{%   do interface_ip_string_list.extend([interface_list[server_nb], '$${' ~ request_tsn_server_section_title ~ ':connection-ip}']) -%}
{%   do part_list.append(request_tsn_server_section_title) -%}
[{{ request_tsn_server_section_title }}]
<= slap-connection
recipe = slapos.cookbook:request
name = TSNMeasurementsServer{{ server_nb }}
software-url = $${:software-release-url}
software-type = tsn-server
return = ip
sla-computer_guid = {{ server_guid }}
config-ip = {{ ip_list[server_nb] }}
{%   if 'gpio-port' in slapparameter_dict -%}
config-gpio-port = {{ slapparameter_dict.get('gpio-port') }}
{%   endif -%}
{%   if server_nb < len(server_interface_list) -%}
config-interface = {{ server_interface_list[server_nb] }}
{%   endif %}

{%   if hardware_timestamps == 'y' -%}
{%     set phc2sys_launcher_section_title = 'phc2sys-launcher-' ~ interface_list[server_nb] -%}
{%     do part_list.append(phc2sys_launcher_section_title) -%}
{%     do script_list.append(phc2sys_launcher_section_title) -%}
[{{ phc2sys_launcher_section_title }}]
<= tsn-script-template
template= inline:
  #!/bin/sh
  ${linuxptp:location}/sbin/pmc -u -b 0 -i {{ interface_list[server_nb] }} "SET GRANDMASTER_SETTINGS_NP clockClass 248 clockAccuracy 0xfe offsetScaledLogVariance 0xffff currentUtcOffset 37 leap61 0 leap59 0 currentUtcOffsetValid 1 ptpTimescale 1 timeTraceable 1 frequencyTraceable 0 timeSource 0xa0"
  exec {{ chrt }} -f 95 ${linuxptp:location}/sbin/phc2sys -s CLOCK_REALTIME -c {{ interface_list[server_nb] }} --step_threshold=1 -m -w
{%   endif %}

{%   set ptp4l_launcher_section_title = 'ptp4l-launcher-' ~ interface_list[server_nb] -%}
{%   do part_list.append(ptp4l_launcher_section_title) -%}
{%   do script_list.append(ptp4l_launcher_section_title) -%}
[{{ ptp4l_launcher_section_title }}]
<= tsn-script-template
template= inline:
  #!/bin/sh
  exec {{ chrt }} -f 97 ${linuxptp:location}/sbin/ptp4l {{ '-H'  if hardware_timestamps=='y' else '-S' }} -i {{ interface_list[server_nb] }} --step_threshold=1 -m

{% endfor -%}

[tsn-script]
<= tsn-script-template
template= inline:
  #!/bin/sh
  exec ${tsn-rt-measures:location}/bin/master -a1 -p97 -t4000 {{ interface_ip_string_list | join(' ') }}

[buildout]
parts =
{% for part in part_list -%}
{{ '  ' ~ part }}
{% endfor %}
  tsn-script
  publish-connection-information

[publish-connection-information]
recipe = slapos.cookbook:publish
{% for script in script_list -%}
{{ script }} = {{ '$${' ~ script ~ ':rendered}' }}
{% endfor -%}
tsn-script = $${tsn-script:rendered}
