---

- name: (Deprecation) Warn about ma1sd variables that are not used anymore
  fail:
    msg: >
      The `{{ item }}` variable defined in your configuration is not used by this playbook anymore!
      You'll need to adapt to the new way of extending ma1sd configuration.
      See the CHANGELOG and the `matrix_ma1sd_configuration_extension_yaml` variable for more information and examples.
  when: "item in vars"
  with_items:
    - 'matrix_ma1sd_ldap_enabled'
    - 'matrix_ma1sd_ldap_connection_host'
    - 'matrix_ma1sd_ldap_connection_tls'
    - 'matrix_ma1sd_ldap_connection_port'
    - 'matrix_ma1sd_ldap_connection_baseDn'
    - 'matrix_ma1sd_ldap_connection_baseDns'
    - 'matrix_ma1sd_ldap_connection_bindDn'
    - 'matrix_ma1sd_ldap_connection_bindPassword'
    - 'matrix_ma1sd_ldap_filter'
    - 'matrix_ma1sd_ldap_attribute_uid_type'
    - 'matrix_ma1sd_ldap_attribute_uid_value'
    - 'matrix_ma1sd_ldap_connection_bindPassword'
    - 'matrix_ma1sd_ldap_attribute_name'
    - 'matrix_ma1sd_ldap_attribute_threepid_email'
    - 'matrix_ma1sd_ldap_attribute_threepid_msisdn'
    - 'matrix_ma1sd_ldap_identity_filter'
    - 'matrix_ma1sd_ldap_identity_medium'
    - 'matrix_ma1sd_ldap_auth_filter'
    - 'matrix_ma1sd_ldap_directory_filter'
    - 'matrix_ma1sd_template_config'

- name: Ensure ma1sd configuration does not contain any dot-notation keys
  fail:
    msg: >
      Since version 1.3.0, ma1sd will not accept property-style configuration keys.
      You have defined a key (`{{ item.key }}`) which contains a dot.
      Instead, use nesting. See: https://github.com/ma1uta/ma1sd/wiki/Upgrade-Notes#v130
  when: "'.' in item.key"
  with_dict: "{{ matrix_ma1sd_configuration }}"

- name: Fail if required ma1sd settings not defined
  fail:
    msg: >
      You need to define a required configuration setting (`{{ item }}`) for using ma1sd.
  when: "vars[item] == ''"
  with_items:
    - "matrix_ma1sd_threepid_medium_email_connectors_smtp_host"

- name: (Deprecation) Catch and report renamed ma1sd variables
  fail:
    msg: >-
      Your configuration contains a variable, which now has a different name.
      Please change your configuration to rename the variable (`{{ item.old }}` -> `{{ item.new }}`).
  when: "vars | dict2items | selectattr('key', 'match', item.old) | list | items2dict"
  with_items:
    - {'old': 'matrix_ma1sd_container_expose_port', 'new': '<superseded by matrix_ma1sd_container_http_host_bind_port>'}

- name: (Deprecation) Catch and report mxisd variables
  fail:
    msg: >-
      mxisd is deprecated and has been replaced with ma1sd (https://github.com/ma1uta/ma1sd), a compatible fork.
      The playbook will migrate your existing mxisd configuration and data automatically, but you need to adjust variable names.
      Please change your configuration (vars.yml) to rename all mxisd variables (`{{ item.old }}` -> `{{ item.new }}`).
  when: "vars | dict2items | selectattr('key', 'match', item.old) | list | items2dict"
  with_items:
    - {'old': 'matrix_mxisd_.*', 'new': 'matrix_ma1sd_.*'}