{% if software_type == slap_software_type -%}

{% set cached_server_dict = {} -%}
{% set part_list = [] -%}
{% set cache_access = "http://%s:%s" % (local_ipv4, cache_port) -%}
{% set TRUE_VALUES = ['y', 'yes', '1', 'true'] -%}
{% set generic_instance_parameter_dict = {'cache_access': cache_access,} -%}
{% set slave_log_dict = {} -%}
{% if extra_slave_instance_list -%}
{%   set slave_instance_information_list = [] -%}
{%   set slave_instance_list = slave_instance_list + json_module.loads(extra_slave_instance_list) -%}
{% endif -%}
[jinja2-template-base]
recipe = slapos.recipe.template:jinja2
rendered = {{ apache_configuration_directory }}/${:filename}
extra-context =
context =
    key eggs_directory buildout:eggs-directory
    key develop_eggs_directory buildout:develop-eggs-directory
    ${:extra-context}

{% do logrotate_dict.pop('recipe') %}
[logrotate]
{% for key, value in logrotate_dict.iteritems() -%}
{{ key }} = {{ value }}
{% endfor %}

# Loop trhought slave list to set up slaves
{% for slave_instance in slave_instance_list -%}
{%   set slave_reference = slave_instance.get('slave_reference') -%}
{%   set slave_section_title = 'dynamic-template-slave-instance-%s' % slave_reference -%}
{%   set slave_parameter_dict = generic_instance_parameter_dict.copy() -%}
{%   set slave_publish_dict = {} -%}
{%   do part_list.append(slave_section_title) -%}

############################
#### Set Slave Log Directory and access

{%   set slave_directory_section = slave_reference + "-directory" -%}
{%   set slave_log_folder = logrotate_dict.get('backup') + '/' + slave_reference + "-logs" -%}
[{{slave_directory_section}}]
recipe = slapos.cookbook:mkdirectory
log-folder = {{slave_log_folder}}

# Set Up log files
{%   do slave_parameter_dict.__setitem__('access_log', '/'.join([apache_log_directory, '%s_access_log' % slave_reference])) -%}
{%   do slave_parameter_dict.__setitem__('error_log', '/'.join([apache_log_directory, '%s_error_log' % slave_reference])) -%}
{%       do slave_instance.__setitem__('access_log', slave_parameter_dict.get('access_log')) -%}
{%       do slave_instance.__setitem__('error_log', slave_parameter_dict.get('error_log')) -%}

# Set slave logrotate entry
{%   set slave_logrotate_section = slave_reference + "-logs" -%}
{%   do part_list.append(slave_logrotate_section) -%}
[{{slave_logrotate_section}}]
<= logrotate
recipe = slapos.cookbook:logrotate.d
name = ${:_buildout_section_name_}
log = {{slave_parameter_dict.get('access_log')}} {{slave_parameter_dict.get('error_log')}}
backup = {{ '${' + slave_directory_section + ':log-folder}' }}
frequency = daily
rotatep-num = 30
post = {{ apache_configuration.get('frontend-graceful-command') }}
sharedscripts = true
notifempty = true
create = true

# integrate current logs inside
{%   set slave_ln_section = slave_reference + "-ln" -%}
{%   do part_list.append(slave_ln_section) -%}
[{{slave_ln_section}}]
recipe = plone.recipe.command
stop-on-error = false
command = ln -s {{slave_parameter_dict.get('error_log')}} {{ '${' + slave_directory_section + ':log-folder}' }}/apache-error.log && ln -s {{slave_parameter_dict.get('access_log')}} {{ '${' + slave_directory_section + ':log-folder}' }}/apache-access.log

# Set password for slave
{%   set slave_password_section = slave_reference + "-password" -%}
[{{slave_password_section}}]
recipe = slapos.cookbook:generate.password
storage-path = {{apache_configuration_directory}}/.{{slave_reference}}.passwd
bytes = 8

# Set up htaccess file for slave
{%   set slave_htaccess_section = slave_reference + '-htaccess' %}
{%   do part_list.append(slave_htaccess_section) -%}
[{{slave_htaccess_section}}]
recipe = plone.recipe.command
stop-on-error = true
htaccess-path = {{apache_configuration_directory}}/.{{slave_reference}}.htaccess
command = {{frontend_configuration.get('apache-directory')}}/bin/htpasswd -cb ${:htaccess-path} {{ slave_reference }} {{ '${' + slave_password_section + ':passwd}' }}

# Add slave log directory to the slave log access dict
{%   do slave_log_dict.__setitem__(slave_reference, slave_log_folder) %}

{%   set slave_log_access_url = 'https://' + slave_reference + ':${'+ slave_password_section +':passwd}@[' + frontend_configuration.get('apache-ipv6') + ']:' + frontend_configuration.get('apache-https-port') + '/' + slave_reference.lower() + '/' %}
{%   do slave_publish_dict.__setitem__('log-access', slave_log_access_url) %}

############################
#### Set Slave Certificates if needed

# Set ssl certificates for each slave
{%   for cert_name in ('ssl_key', 'ssl_crt', 'ssl_ca_crt', 'ssl_csr')-%}
{%     if cert_name in slave_instance -%}
{%       set cert_title = '%s-%s' % (slave_reference, cert_name.replace('ssl_', '')) -%}
{%       set cert_file = '/'.join([custom_ssl_directory, cert_title.replace('-','.')]) -%}
{%       do part_list.append(cert_title) -%}
{%       do slave_parameter_dict.__setitem__(cert_name, cert_file) -%}
{%       do slave_instance.__setitem__('path_to_' + cert_name, cert_file) -%}
# Store certificates on fs
[{{ cert_title }}]
< = jinja2-template-base
template = {{ empty_template }}
rendered = {{ cert_file }}
extra-context =
    key content {{ cert_title + '-config:value' }}
# Store certificate in config
[{{ cert_title + '-config' }}]
value = {{ dumps(slave_instance.get(cert_name)) }}
{%     endif -%}
{%   endfor -%}

############################
#### Set Slave Configuration

{%   if slave_instance.has_key('apache_custom_http') %}
#### Set Configuration for custom slaves

# Set up apache configuration file for slave
[{{ slave_section_title }}]
< = jinja2-template-base
template = {{ template_custom_slave_configuration }}
filename = {{ '%s.conf' % slave_reference }}
extra-context =
    key apache_custom_https {{ 'slave-instance-%s-configuration:apache_custom_https' % slave_reference }}
    key apache_custom_http {{ 'slave-instance-%s-configuration:apache_custom_http' % slave_reference }}
    raw https_port {{ https_port }}
    raw http_port {{ http_port }}
{{ '\n' }}


# Set apache configuration value for slave
[{{ ('slave-instance-%s-configuration' % slave_reference) }}]
{%   set apache_custom_http = ((slave_instance.get('apache_custom_http', '')) % slave_parameter_dict) -%}
{%   set apache_custom_https = ((slave_instance.get('apache_custom_https', '')) % slave_parameter_dict) -%}
apache_custom_http = {{ dumps(apache_custom_http) }}
apache_custom_https = {{ dumps(apache_custom_https) }}
{{ '\n' }}

# The slave use cache
{%     if 'enable_cache' in slave_instance and 'url' in slave_instance and 'domain' in slave_instance -%}
{%       do cached_server_dict.__setitem__(slave_instance.get('domain'), slave_instance.get('url')) -%}
{%     endif -%}

# Publish information
{%     do slave_publish_dict.update(**{'slave-reference':slave_instance.get('slave_reference'), 'public-ipv4':public_ipv4, 'log-access': slave_log_access_url}) %}

{%   else %}
#### Set Configuration for default slaves

# Set slave domain if none was defined
{%     if slave_instance.get('custom_domain', None) == None -%}
{%       do slave_instance.__setitem__('custom_domain', "%s.%s" % (slave_instance.get('slave_reference').replace("-", "").lower(), slapparameter_dict.get('domain'))) -%}
{%     endif -%}

# The slave use cache
# Next line is forbidden and people who copy it will be hanged short
{%     set enable_cache = ('' ~ slave_instance.get('enable_cache', '')).lower() in TRUE_VALUES -%}
{%     if enable_cache -%}
{%       do cached_server_dict.__setitem__(slave_instance.get('custom_domain'), slave_instance.get('url')) -%}
{%       do slave_instance.__setitem__('url', cache_access) -%}
{%     endif -%}
{%     do part_list.append(slave_section_title) -%}


[{{ ('slave-instance-%s-configuration' % slave_reference) }}]
{%     for key, value in slave_instance.iteritems() -%}
{{ key }} = {{ dumps(value) }}
{%     endfor %}

# Set up slave configuration file
[{{ slave_section_title }}]
< = jinja2-template-base
template = {{ template_default_slave_configuration }}
filename = {{ '%s.conf' % slave_reference }}

extensions = jinja2.ext.do
extra-context =
    section slave_parameter {{ 'slave-instance-%s-configuration' % slave_reference }}
    raw https_port {{ https_port }}
    raw http_port {{ http_port }}
{{ '\n' }}

{%     do slave_publish_dict.update(**{'slave-reference':slave_instance.get('slave_reference'), 'public-ipv4':public_ipv4, 'domain':slave_instance.get('custom_domain'), 'url':"http://%s" % slave_instance.get('custom_domain'), 'site_url':"http://%s" % slave_instance.get('custom_domain'), 'secure_access': 'https://%s' % slave_instance.get('custom_domain')}) %}

{%   endif -%}

############################
#### Publish Slave Information

# Publish slave information
{%   if not extra_slave_instance_list -%}
{%     set publish_section_title = 'publish-%s-connection-information' % slave_instance.get('slave_reference') -%}
{%     do part_list.append(publish_section_title) -%}
[{{ publish_section_title }}]
recipe = slapos.cookbook:publish
{%     for key, value in slave_publish_dict.iteritems() %}
{{ key }} = {{ value }}
{%     endfor %}
{%   else -%}
{%     do slave_instance_information_list.append(slave_publish_dict) -%}
{%   endif -%}
{% endfor -%}

[slave-log-directories]
{% for key, value in slave_log_dict.iteritems() -%}
{{ key }} = {{ value }}
{% endfor %}

# Define log access
{% set log_access_section = "apache-log-access" %}
{% do part_list.append(log_access_section) -%}
[{{log_access_section}}]
< = jinja2-template-base
template = {{frontend_configuration.get('template-log-access')}}
rendered = {{frontend_configuration.get('log-access-configuration')}}
extra-context =
    section slave_log_directory slave-log-directories
    raw apache_log_directory {{apache_log_directory}}
    raw apache_configuration_directory {{apache_configuration_directory}}

# Publish information for the instance
{% set publish_section_title = 'publish-apache-information' -%}
{% do part_list.append(publish_section_title) -%}
[{{ publish_section_title }}]
recipe = slapos.cookbook:publish
public-ipv4 = {{ public_ipv4 }}
private-ipv4 = {{ local_ipv4 }}
{% if extra_slave_instance_list -%}
slave-instance-information-list = {{ json_module.dumps(slave_instance_information_list) }}
{% endif -%}
monitor_url = {{ monitor_url }}

{% do part_list.append('cached-rewrite-rules') -%}
[cached-rewrite-rules]
< = jinja2-template-base
template = {{ template_rewrite_cached }}
rendered = {{ rewrite_cached_configuration }}
extra-context =
    import json_module json
    key server_dict rewrite-rules:rules

[rewrite-rules]
rules = {{ dumps(cached_server_dict) }}

[buildout]
parts +=
{% for part in part_list -%}
{{ '    %s' % part }}
{% endfor -%}

eggs-directory = {{ eggs_directory }}
develop-eggs-directory = {{ develop_eggs_directory }}
offline = true
cache-access = {{ cache_access }}

{% endif -%}
