{% if slap_software_type in software_type -%}

# Create all needed directories
[directory]
recipe = slapos.cookbook:mkdirectory

bin = $${buildout:directory}/bin/
etc = $${buildout:directory}/etc/
srv = $${buildout:directory}/srv/
var = $${buildout:directory}/var/
url = $${buildout:directory}/template/

backup = $${:srv}/backup
log = $${:var}/log
run = $${:var}/run
service = $${:etc}/service
etc-run = $${:etc}/run

logrotate-backup = $${:backup}/logrotate
logrotate-entries = $${:etc}/logrotate.d

cron-entries = $${:etc}/cron.d
crontabs = $${:etc}/crontabs
cronstamps = $${:etc}/cronstamps
ca-dir = $${:srv}/ssl

# Instance parameters
[instance-parameter]
# Fetches parameters defined in SlapOS Master for this instance.
# Always the same.
recipe = slapos.cookbook:slapconfiguration.serialised
computer = $${slap-connection:computer-id}
partition = $${slap-connection:partition-id}
url = $${slap-connection:server-url}
key = $${slap-connection:key-file}
cert = $${slap-connection:cert-file}


# Generic jinja section to extend
[jinja2-template-base]
recipe = slapos.recipe.template:jinja2
output = $${buildout:directory}/$${:filename}
extra-context =
context =
    import json_module json
    key eggs_directory buildout:eggs-directory
    key develop_eggs_directory buildout:develop-eggs-directory
    key slap_software_type instance-parameter:slap-software-type
    key slapparameter_dict instance-parameter:configuration
    section directory directory
    $${:extra-context}

####################
# PowerDNS sections
#
[pdns]
configuration = $${pdns-directory:configuration}/pdns.conf
ipv4 = $${instance-parameter:ipv4-random}
ipv6 = $${instance-parameter:ipv6-random}
port = 5353
socket-directory = $${pdns-directory:socket-directory}
webserver-port = 8088

[geo]
zones-file = $${zones-file-template:output}
database = ${geolite2-country:location}/GeoLite2-Country.mmdb

[pdns-directory]
recipe = slapos.cookbook:mkdirectory
configuration = $${directory:etc}/pdns
socket-directory = $${directory:run}/

[pdns-configuration-template]
< = jinja2-template-base
url = ${template-pdns-configuration:target}
output = $${pdns:configuration}
extra-context =
  section pdns pdns
  section geo geo

[asia]
japan = jp
hong-kong = hk
china-telecom = cn-t
china-unicom = cn-u
china-mobile = cn-m
west-asia = ae af am az bh cc cy ge il iq ir jo kg kw kz lb om pk qa ru sa sy tj tm tr uz ye
east-asia = bn bt cx id in io kh kp kr la lk mm mn mo mv my np ph sg th to tw vn

[china]
recipe = slapos.recipe.build
iso-list = ${iso-list:target}
init =
  import re
  ip_split = []
  match = re.compile(r"(.*) :.*:(cn-\w)\n").fullmatch
  with open(options["iso-list"]) as f:
    for line in f:
      m = match(line)
      if m is None:
        continue
      ip_split.append(m.groups())
  options["ip-split"] = ip_split

[zones-file-template]
< = jinja2-template-base
url = ${template-zones-file:target}
extensions = jinja2.ext.do
output = $${pdns-directory:configuration}/zones-file.yml
extra-context =
  section asia asia
  key china china:ip-split

# Executables
[pdns-server]
recipe = slapos.cookbook:wrapper
command-line = ${powerdns:location}/sbin/pdns_server --config-dir=$${pdns-directory:configuration}
wrapper-path = $${directory:service}/pdns
hash-existing-files = $${buildout:directory}/software_release/buildout.cfg

[pdns-reload]
recipe = slapos.cookbook:wrapper
command-line = ${powerdns:location}/bin/pdns_control reload --config-dir=$${pdns-directory:configuration}
wrapper-path = $${directory:etc-run}/pdns-reload

# Port redirection
[port-redirection]
<= jinja2-template-base
inline =
{%- raw %}
  [{"srcPort": 53, "destPort": {{ pdns['port'] }}}]
{% endraw -%}
output = $${buildout:directory}/.slapos-port-redirect
extra-context =
  section pdns pdns

# Promises
[pdns-promise-listen-port]
<= monitor-promise-base
promise = check_socket_listening
name = pdns-port-listening.py
config-host = $${pdns:ipv4}
config-port = $${pdns:port}

[publish-connection-informations]
recipe = slapos.cookbook:publish
monitor-url = $${monitor-publish-parameters:monitor-url}
monitor-user = $${monitor-publish-parameters:monitor-user}
monitor-password = $${monitor-publish-parameters:monitor-password}
powerdns-ipv4 = $${pdns:ipv4}
powerdns-ipv6 = $${pdns:ipv6}
powerdns-port = $${pdns:port}

[buildout]
parts =
  pdns-configuration-template
  pdns-server
  pdns-reload
  port-redirection
  pdns-promise-listen-port
  monitor-base
  publish-connection-informations

extends = ${monitor-template:output}

eggs-directory = {{ eggs_directory }}
develop-eggs-directory = {{ develop_eggs_directory }}
offline = true
{% endif%}
