{% set part_list = [] -%}
{% set ipv6 = (ipv6 | list)[0] -%}
{% set ipv4 = (ipv4 | list)[0] -%}

[directory]
recipe = slapos.cookbook:mkdirectory
etc = ${buildout:directory}/etc
bin = ${buildout:directory}/bin
srv = ${buildout:directory}/srv
var = ${buildout:directory}/var
tmp = ${buildout:directory}/tmp
log = ${:var}/log
vnc = ${buildout:directory}/.vnc
themes = ${buildout:directory}/.themes
config = ${buildout:directory}/.config
gtk3-config = ${:config}/gtk-3.0
scripts = ${:etc}/run
services = ${:etc}/service
promise = ${:etc}/promise
ssl = ${:etc}/ssl
auth = ${:tmp}/auth
workdir = ${:srv}/workdir
framebuffer = ${:srv}/framebuffer
fluxbox = ${buildout:directory}/.fluxbox

[gen-certificate]
recipe = plone.recipe.command
command = "{{ openssl_bin }}" req -newkey rsa -batch -new -x509 -days 3650 -nodes -keyout "${:key-file}" -out "${:cert-file}"
stop-on-error = true
cert-file = ${directory:ssl}/beremiz.crt
key-file = ${directory:ssl}/beremiz.key

[nginx-tempdir]
recipe = slapos.cookbook:mkdirectory
tmp = ${buildout:directory}/tmp
client-body-temp-path = ${:tmp}/client_body_temp_path
proxy-temp-path = ${:tmp}/proxy_temp_path
fastcgi-temp-path = ${:tmp}/fastcgi_temp_path
uwsgi-temp-path = ${:tmp}/uwsgi_temp_path
scgi-temp-path = ${:tmp}/scgi_temp_path

[nginx-launcher]
recipe = slapos.cookbook:wrapper
command-line = {{ nginx_executable }} -c ${nginx-config:output}
wrapper-path = ${directory:services}/nginx

[nginx-config]
recipe = slapos.recipe.template:jinja2
url = {{ template_nginx }}
output = ${directory:etc}/nginx.conf
context =
    section params nginx-params
    section ca gen-certificate
    section tempdir nginx-tempdir
    raw docroot {{ novnc_location }}
    raw mime {{ nginx_mime }}

[nginx-params]
path-pid = ${directory:run}/nginx.pid
path-error-log = ${directory:log}/nginx-error.log
path-access-log = ${directory:log}/nginx-access.log
ip = {{ ipv6 }}
port = 6080
websocket-ip = {{ ipv4 }}
websocket-port = ${x11vnc:port}
websocket-path = websockify
nb-workers = 2

[nginx-graceful]
recipe = slapos.recipe.template
output = ${directory:scripts}/nginx-graceful
inline =
  #!/bin/sh
  exec kill -s SIGHUP $(cat ${nginx-params:path-pid})

[novnc-promise]
<= monitor-promise-base
promise = check_socket_listening
name = novnc_promise.py
config-host = ${nginx-params:ip}
config-port = ${nginx-params:port}

[x11vnc]
recipe = slapos.cookbook:wrapper
port = 5901
command-line = {{ x11vnc_bin }} -forever -display ${xserver:display} -ncache 10
  -noxdamage -rfbport ${:port} -no6 -noipv6 -reopen -o ${directory:log}/x11vnc.log
  -usepw -rfbauth ${x11vnc-passwd:passfile} -desktop BeremizVNC
  -listen {{ ipv4 }} -xkb
wrapper-path = ${directory:services}/x11vnc
hash-existing-files = ${buildout:directory}/software_release/buildout.cfg
environment =
  XORG_LOCK_DIR=${directory:run}
wait-for-files =
  ${xserver:lock-file}

[x11vnc-listen-promise]
<= monitor-promise-base
promise = check_socket_listening
name = x11vnc_listening.py
config-host = {{ ipv4 }}
config-port = ${x11vnc:port}

[random-password]
recipe = slapos.cookbook:generate.password
storage-path = ${directory:etc}/.passwd
bytes = 8

[x11vnc-passwd]
recipe = slapos.recipe.template
passfile = ${directory:vnc}/passwd
inline =
  #!/bin/sh -e
  if [ -s "${:passfile}" ]; then
    echo "Password initialized.";
  else
    chmod 700 $(dirname ${:passfile});
    {{ x11vnc_bin }} -storepasswd ${random-password:passwd} ${:passfile};
  fi
output = ${directory:bin}/x11vnc_passwd

[generate-vnc-password]
recipe = plone.recipe.command
stop-on-error = true
command = ${x11vnc-passwd:output}
update-command = ${:command}

# Generate a fonts.conf file.
[font-config]
recipe = slapos.recipe.template:jinja2
url = {{ font_config_tmplt }}
output = ${directory:etc}/fonts.conf
context =
  key cachedir :cache-dir
  key fonts :fonts
  key includes :includes
cache-dir =
  ${directory:etc}/.fontconfig.cache
fonts =
  {{ font_dejavu }}
  {{ font_liberation }}
includes =
  {{ font_config_loc }}/etc/fonts/conf.d

[xserver]
recipe = slapos.cookbook:wrapper
command-line = {{ xvfb_bin }} ${:display} -screen 0 ${:resolution}x24
  -fbdir ${directory:framebuffer}
wrapper-path = ${directory:services}/xserver
hash-existing-files = ${buildout:directory}/software_release/buildout.cfg
environment =
  XORG_LOCK_DIR=${:lock-dir}
  FONTCONFIG_FILE=${font-config:output}
display-num = 0
display = :${:display-num}
resolution = {{ slapparameter_dict.get('screen-resolution', '1280x1024') }}
lock-dir = ${directory:run}
lock-file = ${:lock-dir}/.X${:display-num}-lock

[xserver-promise]
recipe = slapos.cookbook:wrapper
wrapper-path = ${directory:promise}/xserver-is-up
command-line = bash -c "[ -S ${xserver:lock-dir}/.X11-unix/X${xserver:display-num} ]"

[install-gtk-materia-theme]
recipe = plone.recipe.command
command =
  cp -r {{ gtk_materia_theme }}/share/themes/* ${directory:themes}/
update-command = ${:command}
stop-on-error = true

[enable-matieria-theme]
recipe = slapos.recipe.template
inline =
  [Settings]
  gtk-font-name=FreeSans,10
  gtk-theme-name=Materia
output = ${directory:gtk3-config}/settings.ini

[fluxbox-menu]
recipe = slapos.recipe.template:jinja2
url = {{ fluxbox_menu }}
output = ${directory:fluxbox}/menu
context =
  key beremiz_bin      beremiz-x11:output
  key xterm_bin        xterm:output
  raw fluxbox_location {{ fluxbox_location }}

[fluxbox-toolbar]
recipe = plone.recipe.command
command =
  if [ ! -s "${directory:fluxbox}/init" ]; then
    echo "session.screen0.toolbar.placement:      TopCenter" > ${directory:fluxbox}/init;
  fi

[fluxbox-bin]
recipe = slapos.cookbook:wrapper
command-line = {{ fluxbox_location }}/bin/fluxbox -display ${xserver:display}
wrapper-path = ${directory:bin}/fluxbox
environment =
  HOME=${buildout:directory}
  XORG_LOCK_DIR=${xserver:lock-dir}
  FONTCONFIG_FILE=${font-config:output}
  LANG=C.UTF-8
  LC_ALL=C.UTF-8

depends =
  ${fluxbox-menu:recipe}
  ${fluxbox-toolbar:recipe}

[fluxbox]
recipe = slapos.cookbook:wrapper
command-line = ${fluxbox-bin:wrapper-path} -log ${directory:log}/fluxbox.log
wrapper-path = ${directory:services}/fluxbox

[beremiz-env.sh]
recipe = slapos.recipe.template
inline =
  export LD_LIBRARY_PATH={{ mesa_location }}/lib
  export PATH=${directory:bin}:{{ git_bin_dir }}:{{ cmake_bin }}:{{ autoconf_bin }}:{{ automake_bin }}:{{ matiec_location }}/bin:{{ bison_location }}/bin:{{ flex_location }}/bin:{{ bin_directory }}:{{ gcc_location }}/bin:$PATH
  export XDG_DATA_DIR={{ gtk3_location }}/share
  export HOME=${buildout:directory}
  export GSETTINGS_SCHEMA_DIR={{ gtk3_location }}/share/glib-2.0/schemas
  export FONTCONFIG_FILE=${font-config:output}
  export DISPLAY=${xserver:display}
  export XORG_LOCK_DIR=${xserver:lock-dir}
  export LANG=C.UTF-8
  export LC_ALL=C.UTF-8
  export BEREMIZPYTHONPATH={{ python_bin }}
  export OPCUA_DEFAULT_HOST={{ ipv4 }}
  export BEREMIZ_LOCAL_HOST={{ ipv4 }}

output = ${directory:bin}/beremiz-env.sh

[beremiz-x11]
recipe = slapos.recipe.template
inline =
  #!/bin/sh -e
  . ${beremiz-env.sh:output}
  # wait a bit for xserver
  sleep 1
  exec {{ python_bin }} {{ beremiz_location }}/Beremiz.py
output = ${directory:bin}/beremiz-x11

[xterm]
recipe = slapos.recipe.template
inline =
  #!/bin/sh -e
  . ${beremiz-env.sh:output}
  export SHELL={{bash_bin}}
  exec {{ xterm_bin }}
output = ${directory:bin}/xterm

[request-vnc-frontend]
<= slap-connection
recipe = slapos.cookbook:requestoptional
shared = true
config-https-only = True
config-type = websocket
config-url = https://[${nginx-params:ip}]:${nginx-params:port}
config-websocket-path-list = ${nginx-params:websocket-path}
return = secure_access domain
software-url = http://git.erp5.org/gitweb/slapos.git/blob_plain/HEAD:/software/apache-frontend/software.cfg
software-type = RootSoftwareInstance
name = Beremiz VNC


[publish-connection-information]
<= monitor-publish
recipe = slapos.cookbook:publish
backend-url = https://[${nginx-params:ip}]:${nginx-params:port}/vnc.html?encrypt=1&password=${random-password:passwd}
url = ${request-vnc-frontend:connection-secure_access}/vnc.html?encrypt=1&password=${random-password:passwd}

[buildout]
extends =
  {{ template_logrotate }}
{{ '  ' ~ template_monitor }}

parts =
  monitor-base
  fluxbox
  novnc-promise
  xserver-promise
  x11vnc-listen-promise
  enable-matieria-theme
  install-gtk-materia-theme
  beremiz-x11
  nginx-launcher
  nginx-graceful
  request-vnc-frontend
  generate-vnc-password
  publish-connection-information

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