[buildout]
parts =
  main
  user

eggs-directory = ${buildout:eggs-directory}
develop-eggs-directory = ${buildout:develop-eggs-directory}
offline = true

[directory]
recipe = slapos.cookbook:mkdirectory
home = $${buildout:directory}
etc = $${:home}/etc
var = $${:home}/var
log = $${:var}/log

[slap-configuration]
recipe = slapos.cookbook:slapconfiguration
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}

[drone]
recipe = slapos.recipe.build
slapparameter-dict = $${slap-configuration:configuration}
init =
  options['autopilot-ip'] = options['slapparameter-dict'].get('autopilot-ip', '192.168.27.1')
  options['id'] = options['slapparameter-dict'].get('id', 1)
  options['is-a-simulation'] = options['slapparameter-dict'].get('is-a-simulation', False)
  options['multicast-ipv6'] = options['slapparameter-dict'].get('multicast-ip', 'ff15::1111')
  options['net-if'] = options['slapparameter-dict'].get('net-if', 'eth0')
  options['drone-id-list'] = options['slapparameter-dict'].get('drone-id-list', [])
  options['is-a-drone'] = 'flight-script' in options['slapparameter-dict']

  subscription_script = '''
  me.onStart = function() {
    me.f = me.fdopen(me.in, "r");
    console.log("Use q to quit");
  };

  me.onUpdate= function(timestamp) {
    while(me.f.getline() != "q") {
      continue;
    }
    try {
      me.f.close();;
    } catch (error) {
      console.error(error);
    }
    me.exit(0);
  };
  '''

  options['flight-script'] = options['slapparameter-dict'].get('flight-script', subscription_script)

[js-dynamic-template]
recipe = slapos.recipe.template:jinja2
rendered = $${directory:etc}/$${:_buildout_section_name_}.js
template = ${buildout:directory}/$${:_buildout_section_name_}.js
extra-context =
context =
  raw qjs_wrapper ${qjs-wrapper:location}/lib/libqjswrapper.so
  $${:extra-context}

[main]
<= js-dynamic-template
extra-context =
  key autopilot_ip drone:autopilot-ip
  key id drone:id
  key is_a_simulation drone:is-a-simulation
  key is_a_drone drone:is-a-drone
  key log_dir directory:log
  key pubsub_script pubsub:rendered
  key worker_script worker:rendered

[pubsub]
<= js-dynamic-template
extra-context =
  key ipv6 drone:multicast-ipv6
  key net_if drone:net-if

[user]
recipe = slapos.recipe.template:jinja2
output = $${directory:etc}/user.js
context =
  key script drone:flight-script
inline = {{ script }}

[worker]
<= js-dynamic-template
extra-context =
  key drone_id_list drone:drone-id-list
  key id drone:id
  key is_a_drone drone:is-a-drone
