{% set publish_dict = {} -%}
{% 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
scripts = ${:etc}/run
services = ${:etc}/service
ssl = ${:etc}/ssl
auth = ${:tmp}/auth
ca-dir = ${:srv}/ssl
novnc-conf = ${:etc}/novnc
prerm = ${:etc}/prerm
pwrp = ${buildout:directory}/pwrp
common = ${:pwrp}/common
x86-64-linux = ${:pwrp}/x86_64_linux
inc = ${:common}/inc
load = ${:common}/load
log = ${:common}/log
loghist = ${:common}/loghist
db = ${:common}/db
web = ${:common}/web
exe = ${:x86-64-linux}/exe
lib = ${:x86-64-linux}/lib
obj = ${:x86-64-linux}/obj
lis = ${:x86-64-linux}/lis


[certificate-authority]
recipe = slapos.cookbook:certificate_authority
openssl-binary = {{ openssl_executable_location }}
ca-dir = ${directory:ca-dir}
requests-directory = ${cadirectory:requests}
wrapper = ${directory:bin}/certificate_authority
ca-private = ${cadirectory:private}
ca-certs = ${cadirectory:certs}
ca-newcerts = ${cadirectory:newcerts}
ca-crl = ${cadirectory:crl}

[certificate-authority-service]
recipe = slapos.cookbook:wrapper
command-line = ${certificate-authority:wrapper}
wrapper-path = ${directory:services}/certificate_authority
hash-existing-files = ${buildout:directory}/software_release/buildout.cfg

[cadirectory]
recipe = slapos.cookbook:mkdirectory
requests = ${directory:ca-dir}/requests/
private = ${directory:ca-dir}/private/
certs = ${directory:ca-dir}/certs/
newcerts = ${directory:ca-dir}/newcerts/
crl = ${directory:ca-dir}/crl/

[novnc-instance]
recipe = slapos.cookbook:novnc
path = ${ca-novnc:executable}
ip = {{ ipv6 }}
port = 6080
vnc-ip = {{ ipv4 }}
vnc-port = 5901
novnc-location = {{ novnc_location }}
websockify-path = {{ websockify_executable_location }}
ssl-key-path = ${ca-novnc:key-file}
ssl-cert-path = ${ca-novnc:cert-file}

[websockify-sighandler]
recipe = slapos.cookbook:signalwrapper
wrapper-path = ${directory:bin}/websockify-sighandler
wrapped-path = ${novnc-instance:path}

[websockify-sighandler-service]
recipe = slapos.cookbook:wrapper
command-line = ${websockify-sighandler:wrapper-path}
wrapper-path = ${directory:services}/websockify
hash-existing-files = ${buildout:directory}/software_release/buildout.cfg
wait-for-files =
  ${ca-novnc:key-file}
  ${ca-novnc:cert-file}

[ca-novnc]
<= certificate-authority
recipe = slapos.cookbook:certificate_authority.request
key-file = ${directory:novnc-conf}/novnc.key
cert-file = ${directory:novnc-conf}/novnc.crt
executable = ${directory:bin}/novnc
wrapper = ${directory:bin}/websockify

[novnc-promise]
<= monitor-promise-base
module = check_port_listening
name = novnc_promise.py
config-hostname = ${novnc-instance:ip}
config-port = ${novnc-instance:port}

[x11vnc]
recipe = slapos.cookbook:wrapper
command-line = {{ x11vnc_bin }} -forever -display ${xserver:display} -ncache 10
  -autoport 5901 -no6 -reopen -o ${directory:log}/x11vnc.log
  -listen {{ ipv4 }} -flag ${proview-prepare:pwr-flag} -xkb
  -auth ${directory:auth}.Xauthority
wrapper-path = ${directory:services}/x11vnc
hash-existing-files = ${buildout:directory}/software_release/buildout.cfg

[xserver]
recipe = slapos.cookbook:wrapper
command-line = {{ xvfb_bin }} ${:display} -screen 0 1280x1024x24
  -cc 4 -nolisten tcp -auth ${directory:auth}.Xauthority -fp {{ xorg_font_util}}/share/fonts/X11/Type1
wrapper-path = ${directory:services}/xserver
hash-existing-files = ${buildout:directory}/software_release/buildout.cfg
display = :98

[proview-prepare]
recipe = plone.recipe.command
pwr-db = ${directory:pwrp}/db
pwr-flag = ${directory:pwrp}/flag
command = 
  cp -r {{ proview_directory }}/local/adm/db/ ${:pwr-db}/.. #cp will make extra 'db' folder
  echo PORT=5901 > ${:pwr-flag}
  cat <<EOF > ${buildout:directory}/.rtt_start
    #!/bin/bash
    source ${pwrp-profile:rendered}
    $pwr_exe/rt_rtt
  EOF
  cat <<EOF > ${buildout:directory}/.xtt_start
    #!/bin/bash
    source ${pwrp-profile:rendered}
    $pwr_exe/rt_xtt &
  EOF

update-command =
stop-on-error = true

[proview-environment]
recipe = collective.recipe.template
input = inline:#!/bin/bash
  source ${pwrp-profile:rendered}
  export pwra_db=${proview-prepare:pwr-db}
  source $pwra_db/pwr_setup.sh
  export PWR_BUS_ID=999
  export DISPLAY=${xserver:display}
  export LD_LIBRARY_PATH={{ ld_library_path }}
  export TMPDIR=${directory:tmp}

  if [ "$PS1" ]; then
    eval `dircolors -b`
    alias ls='ls --color=auto'
    # set a fancy prompt

    PS1='\u@\h:\w\$ '
  fi

  # Set Base (XXX - hard coded version)
  pwrp set base V5.7.1

output = ${buildout:directory}/.bashrc
mode = 600

[bash-profile]
recipe = collective.recipe.template
input = inline:#!/bin/bash
  umask 022
  export HOME=${buildout:directory}
  # include .bashrc if it exists

  if [ -f ~/.bashrc ]; then
      source ~/.bashrc
  fi

output = ${buildout:directory}/.bash_profile
mode = 600

[pwrp-profile]
recipe = slapos.recipe.template:jinja2
rendered = ${directory:etc}/pwrp_profile
extensions = jinja2.ext.do
mode = 0744
template = {{ pwrp_profile }}
context =
  key pwra_db proview-prepare:pwr-db
  key base_directory buildout:directory
  raw pwr_exp_dir {{ proview_compile_dir }}/rls/os_linux/hw_x86_64/exp
  raw pwr_bus_id 999
  raw java_location {{ java_location }}


[pwrrt-wrapper]
recipe = slapos.recipe.template:jinja2
rendered = ${directory:bin}/pwrrt
extensions = jinja2.ext.do
mode = 0744
template = {{ template_pwrrt }}
context =
  key tmp_directory directory:tmp
  key pwrp_profile pwrp-profile:rendered
  raw ld_library_path {{ ld_library_path }}

[pwrrt-service]
recipe = slapos.cookbook:wrapper
command-line = ${pwrrt-wrapper:rendered} start
wrapper-path = ${directory:services}/pwrrt
hash-existing-files = ${buildout:directory}/software_release/buildout.cfg
environment = 
  LD_LIBRARY_PATH={{ ld_library_path }}

[shellinabox]
recipe = slapos.recipe.template:jinja2
socket = ${directory:run}/siab.sock
mode = 0700
rendered = ${directory:bin}/shellinaboxd
template = inline:
  #!/bin/sh
  # XXXXXXXXXXXXXXXXXXXXXXX WORKAROUND XXXXXXXXXXXXXXXXXXXXXxx
  export HOME=${buildout:directory}
  # XXXXXXXXXXXXXXXXXXXXXXX WORKAROUND XXXXXXXXXXXXXXXXXXXXXxx
  exec {{ shellinabox_bin }} \
    --unixdomain-only=${:socket}:$(id -u):$(id -g):0600 \
    --service "/:$(id -u):$(id -g):${buildout:directory}:${shell-environment:shell} --rcfile ${bash-profile:output} -i"

[shellinabox-service]
recipe = slapos.cookbook:wrapper
command-line = ${directory:bin}/shellinaboxd
wrapper-path = ${directory:services}/shellinaboxd
hash-existing-files = ${buildout:directory}/software_release/buildout.cfg

[shell-environment]
#shell = /bin/bash
shell = {{ bash_location.replace('//','/') }}

[ca-nginx]
<= certificate-authority
recipe = slapos.cookbook:certificate_authority.request
key-file = ${cadirectory:certs}/nginx.key
cert-file = ${cadirectory:certs}/nginx.crt
executable = ${nginx-launcher:wrapper-path}
wrapper = ${directory:services}/nginx
# Put domain name
name = example.com

[nginx-htpasswd]
recipe = plone.recipe.command
stop-on-error = true
htpasswd-path = ${directory:etc}/.htpasswd
command = if [ ! -f "${:htpasswd-path}" ]; then {{ apache_htpasswd_bin }} -cb ${:htpasswd-path} ${:user} ${monitor-htpasswd:passwd}; fi
update-command = ${:command}
user = proviewr

[nginx-parameter-conf]
local-ip = ${slap-network-information:local-ipv4}
global-ip = ${slap-network-information:global-ipv6}
global-port = 8899
ssl-certificate = ${ca-nginx:cert-file}
ssl-key = ${ca-nginx:key-file}
pid-file = ${directory:run}/nginx.pid
access-log = ${directory:log}/nginx.access.log
error-log = ${directory:log}/nginx.error.log
etc-dir = ${directory:etc}
htpasswd-file = ${nginx-htpasswd:htpasswd-path}
client-body-temp-path = ${directory:tmp}/client_body_temp_path
proxy-temp-path = ${directory:tmp}/proxy_temp_path
fastcgi-temp-path = ${directory:tmp}/fastcgi_temp_path
uwsgi-temp-path = ${directory:tmp}/uwsgi_temp_path
scgi-temp-path = ${directory:tmp}/scgi_temp_path

[nginx_conf]
recipe = slapos.recipe.template:jinja2
template = {{ template_nginx }}
rendered = ${directory:etc}/nginx.conf
context =
    key shellinabox_socket shellinabox:socket
    section parameter_dict nginx-parameter-conf

[nginx-launcher]
recipe = slapos.cookbook:wrapper
wrapper-path = ${directory:bin}/nginx-launcher
command-line = {{ nginx_bin }} -p ${buildout:directory} -c ${nginx_conf:rendered}

[logrotate-entry-nginx]
<= logrotate-entry-base
name = nginx
log = ${nginx-parameter-conf:access-log} ${nginx-parameter-conf:error-log}
post = kill -USR1 $(cat ${nginx-parameter-conf:pid-file})

[request-slave-frontend]
<= slap-connection
recipe = slapos.cookbook:requestoptional
slave = true
config-https-only = True
config-type = websocket
config-url = https://[${novnc-instance:ip}]:${novnc-instance:port}
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 = ProviewR VNC
sla-instance_guid =

[request-shellinabox-frontend]
<= slap-connection
recipe = slapos.cookbook:requestoptional
name = Proview Shellinabox Frontend
# XXX We have hardcoded SR URL here.
software-url = http://git.erp5.org/gitweb/slapos.git/blob_plain/HEAD:/software/apache-frontend/software.cfg
slave = true
config-url = https://[${nginx-parameter-conf:global-ip}]:${nginx-parameter-conf:global-port}
config-domain = 
return = secure_access domain


[publish-connection-information]
<= monitor-publish
recipe = slapos.cookbook:publish
backend-url = https://[${novnc-instance:ip}]:${novnc-instance:port}/vnc.html?host=[${novnc-instance:ip}]&port=${novnc-instance:port}&encrypt=1
url = ${request-slave-frontend:connection-secure_access}/vnc.html?host=${request-slave-frontend:connection-domain}&port=443&encrypt=1
shell-url = ${request-shellinabox-frontend:connection-secure_access}
shell-backend = ${request-shellinabox-frontend:config-url}
{% for name, value in publish_dict.items() -%}
{{   name }} = {{ value }}
{% endfor %}

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

parts =
  monitor-base
  publish-connection-information
  x11vnc
  xserver
  websockify-sighandler
  websockify-sighandler-service
  novnc-promise
  shellinabox
  shellinabox-service
  ca-nginx
  logrotate-entry-nginx
  proview-environment
  bash-profile
  pwrrt-service

# Complete parts with sections
  {{ part_list | join('\n  ') }}
 
eggs-directory = {{ eggs_directory }}
develop-eggs-directory = {{ develop_eggs_directory }}
offline = true
