[buildout]
parts =
  qjs-launcher
  publish-connection-information

[directory]
recipe = slapos.cookbook:mkdirectory

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

log = $${:var}/log
public = $${:srv}/public
service = $${:etc}/service

[js-dynamic-template]
recipe = slapos.recipe.template:jinja2
rendered = $${directory:etc}/$${:_buildout_section_name_}.js
extra-context =
context =
  import json_module json
  raw gwsocket_bin ${gwsocket:location}/bin/gwsocket
  raw qjs_wrapper ${qjs-wrapper:location}/lib/libqjswrapper.so
  raw configuration {{ configuration }}
  $${:extra-context}

[main]
<= js-dynamic-template
template = ${main:target}
extra-context =
  key log_dir directory:log
  key pubsub_script pubsub:rendered
  key worker_script worker:rendered

[pubsub]
<= js-dynamic-template
template = ${pubsub:target}

[worker]
<= js-dynamic-template
template = ${worker:target}

[user]
recipe = slapos.recipe.build:download
url = {{ parameter_dict['flightScript'] }}
destination = $${directory:etc}/user.js
offline = false

[qjs-launcher]
recipe = slapos.cookbook:wrapper
wrapper-path = $${directory:service}/qjs-launcher
command-line = ${quickjs:location}/bin/qjs $${main:rendered} $${user:target}

[script-js]
recipe = slapos.recipe.template:jinja2
template = ${script-js:target}
rendered = $${directory:public}/script.js
websocket-url = [{{ ipv6 }}]:{{ websocket_port }}
context =
  raw websocket_url $${:websocket-url}

[index-html]
recipe = slapos.recipe.template:jinja2
template = ${index-html:target}
rendered = $${directory:public}/index.html
context =
  raw nb_drones {{ parameter_dict['numberOfDrone'] }}

[httpd-port]
recipe = slapos.cookbook:free_port
minimum = 8080
maximum = 8090
ip = {{ ipv6 }}

[httpd]
recipe = slapos.cookbook:simplehttpserver
host = {{ ipv6 }}
port = $${httpd-port:port}
base-path = $${directory:public}
wrapper = $${directory:service}/http-server
log-file = $${directory:log}/httpd.log
use-hash-url = false
depends = $${index-html:rendered}

[publish-connection-information]
recipe = slapos.cookbook:publish.serialised
instance-path = $${directory:home}
{% if not parameter_dict['isADrone'] -%}
httpd-url = [$${httpd:host}]:$${httpd:port}
websocket-url = ws://$${script-js:websocket-url}
{% endif -%}
