/*
  {{ ansible_managed }}

  This file must be valid JSON. But comments are allowed
*/
{#
Pro/contra for generating this file with to_nice_json:

Pro:
* Much cleaner
* Less error prone (JSON is picky)

Contro:
* No comments in the generated file possible for the different settings options

Result: For now continue to use the template file.

See also: https://github.com/debops/ansible-etherpad/pull/16/files#r41485963

#}
{
  // Name your instance!
  "title": "{{ etherpad_title }}",

  // favicon default name
  // alternatively, set up a fully specified Url to your own favicon
  "favicon": "favicon.ico",

  // IP and port which etherpad should bind at
  "ip": "{{ etherpad_bind }}",
  "port" : {{ etherpad_port }},

  /*
  // Node native SSL support
  // this is disabled by default
  //
  // make sure to have the minimum and correct file access permissions set
  // so that the Etherpad server can access them

  "ssl" : {
            "key"  : "/path-to-your/epl-server.key",
            "cert" : "/path-to-your/epl-server.crt",
            "ca": ["/path-to-your/epl-intermediate-cert1.crt", "/path-to-your/epl-intermediate-cert2.crt"]
          },

  */

  // The Type of the database. You can choose between dirty, postgres, sqlite and mysql
  // You shouldn't use "dirty" for for anything else than testing or development
  "dbType" : "{{ etherpad__database }}",
  "dbSettings" : {
  {% if etherpad__database in ['dirty', 'sqlite'] -%}
      "filename" : "{{ etherpad_database_config[etherpad__database].filename }}"
  {% elif etherpad__database in ['mysql', 'postgres'] -%}
      "user"    : "{{ etherpad_database_config[etherpad__database].username }}",
      "host"    : "{{ etherpad_database_config[etherpad__database].hostname }}",
      {% if etherpad_database_connection == 'port' %}
        "port"    : "{{ etherpad_database_config[etherpad__database].port }}",
      {% else %}
        {% if etherpad__database in ['mysql'] -%}
         "port"    : "{{ etherpad_database_config[etherpad__database].socket }}",
        {% endif %}
      {% endif %}
      "password": "{{ etherpad_database_config[etherpad__database].password | default('') }}",
      "database": "{{ etherpad_database_config[etherpad__database].database }}"
   {% endif %}
   },

  //the default text of a pad
  "defaultPadText" : "{{ etherpad_welcome_text|replace('\n', '\\n') }}",

  /* Default Pad behavior, users can override by changing */
  "padOptions": {
    "noColors": false,
    "showControls": true,
    "showChat": true,
    "showLineNumbers": true,
    "useMonospaceFont": false,
    "userName": false,
    "userColor": false,
    "rtl": false,
    "alwaysShowChat": false,
    "chatAndUsers": false,
    "lang": "en-gb"
  },

  /* Should we suppress errors from being visible in the default Pad Text? */
  "suppressErrorsInPadText" : false,

  /* Users must have a session to access pads. This effectively allows only group pads to be accessed. */
  "requireSession" : {{ etherpad_require_session | lower }},

  /* Users may edit pads but not create new ones. Pad creation is only via the API. This applies both to group pads and regular pads. */
  "editOnly" : {{ etherpad_edit_only | lower }},

  /* Users, who have a valid session, automatically get granted access to password protected pads */
  "sessionNoPassword" : false,

  /* if true, all css & js will be minified before sending to the client. This will improve the loading performance massivly,
     but makes it impossible to debug the javascript/css */
  "minify" : {{ etherpad_minify | lower }},

  /* How long may clients use served javascript code (in seconds)? Without versioning this
     may cause problems during deployment. Set to 0 to disable caching */
  "maxAge" : {{ etherpad_max_age }},

  /* This is the path to the Abiword executable. Setting it to null, disables abiword.
     Abiword is needed to advanced import/export features of pads*/
  "abiword" : {% if etherpad_abiword %}"/usr/bin/abiword"{% else %}null{% endif %},

  /* This is the path to the Tidy executable. Setting it to null, disables Tidy.
     Tidy is used to improve the quality of exported pads*/
  "tidyHtml" : null,

  /* Allow import of file types other than the supported types: txt, doc, docx, rtf, odt, html & htm */
  "allowUnknownFileEnds" : true,

  /* This setting is used if you require authentication of all users.
     Note: /admin always requires authentication. */
  "requireAuthentication": {{ etherpad_require_authentication | lower }},

  /* Require authorization by a module, or a user with is_admin set, see below. */
  "requireAuthorization": {{ etherpad_require_authorization | lower }},

  /*when you use NginX or another proxy/ load-balancer set this to true*/
  "trustProxy": {{ etherpad_trust_proxy | lower }},

  /* Privacy: disable IP logging */
  "disableIPlogging": {{ etherpad_disable_ip_logging | lower }},

  /* Users for basic authentication. is_admin = true gives access to /admin.
     If you do not uncomment this, /admin will not be available! */
{% if (secret is defined and secret) and (etherpad_admins or etherpad_users) %}
  "users": {
{% for name in etherpad_admins %}
    "{{ name }}": {
{% if 'ep_hash_auth' in (etherpad__default_plugins + etherpad_plugins) %}
      "hash": "{{ lookup('pipe', 'printf "%s" "' + lookup('password', secret + '/credentials/' + inventory_hostname + '/etherpad/admins/' + name) + '" | sha512sum | cut -f1 -d" "') }}",
{% else %}
      "password": "{{ lookup('password', secret + '/credentials/' + inventory_hostname + '/etherpad/admins/' + name) }}",
{% endif %}
      "is_admin": true
    },
{% endfor %}
{% for name in etherpad_users %}
    "{{ name }}": {
{% if 'ep_hash_auth' in (etherpad__default_plugins + etherpad_plugins) %}
      "hash": "{{ lookup('pipe', 'printf "%s" "' + lookup('password', secret + '/credentials/' + inventory_hostname + '/etherpad/users/' + name) + '" | sha512sum | cut -f1 -d" "') }}",
{% else %}
      "password": "{{ lookup('password', secret + '/credentials/' + inventory_hostname + '/etherpad/users/' + name) }}",
{% endif %}
      "is_admin": false
    },
{% endfor %}
  },
{% endif %}

  // restrict socket.io transport methods
  "socketTransportProtocols" : ["xhr-polling", "jsonp-polling", "htmlfile"],

  // Allow Load Testing tools to hit the Etherpad Instance.  Warning this will disable security on the instance.
  "loadTest": false,

  /* The toolbar buttons configuration.
  "toolbar": {
    "left": [
      ["bold", "italic", "underline", "strikethrough"],
      ["orderedlist", "unorderedlist", "indent", "outdent"],
      ["undo", "redo"],
      ["clearauthorship"]
    ],
    "right": [
      ["importexport", "timeslider", "savedrevision"],
      ["settings", "embed"],
      ["showusers"]
    ],
    "timeslider": [
      ["timeslider_export", "timeslider_returnToPad"]
    ]
  },
  */

  "ep_hash_auth": {
    "hash_typ": "{{ etherpad_password_hashing_algo }}"
  },

  /* The log level we are using, can be: DEBUG, INFO, WARN, ERROR */
  "loglevel": "{{ etherpad_loglevel }}",

  //Logging configuration. See log4js documentation for further information
  // https://github.com/nomiddlename/log4js-node
  // You can add as many appenders as you want here:
  "logconfig" :
    { "appenders": [
        { "type": "console"
        //, "category": "access"// only logs pad access
        }
    /*
      , { "type": "file"
      , "filename": "your-log-file-here.log"
      , "maxLogSize": 1024
      , "backups": 3 // how many log files there're gonna be at max
      //, "category": "test" // only log a specific category
        }*/
    /*
      , { "type": "logLevelFilter"
        , "level": "warn" // filters out all log messages that have a lower level than "error"
        , "appender":
          {  Use whatever appender you want here  }
        }*/
    /*
      , { "type": "logLevelFilter"
        , "level": "error" // filters out all log messages that have a lower level than "error"
        , "appender":
          { "type": "smtp"
          , "subject": "An error occured in your EPL instance!"
          , "recipients": "bar@blurdybloop.com, baz@blurdybloop.com"
          , "sendInterval": 60*5 // in secs -- will buffer log messages; set to 0 to send a mail for every message
          , "transport": "SMTP", "SMTP": { // see https://github.com/andris9/Nodemailer#possible-transport-methods
              "host": "smtp.example.com", "port": 465,
              "secureConnection": true,
              "auth": {
                  "user": "foo@example.com",
                  "pass": "bar_foo"
              }
            }
          }
        }*/
      ]
{% if etherpad_custom_json %}
    },
    {{ (etherpad_custom_json | to_nice_json)[1:][:-1] | trim }}
{% else %}
    }
{% endif %}
}
