---
- name: "include role nginx-domain-setup for {{application_id}}"
  include_role: 
    name: nginx-domain-setup
  loop:
    - "{{domains.matrix.element}}"
    - "{{domains.matrix.synapse}}"
  loop_control:
    loop_var: domain

- name: Clone the git repository
  git:
    repo: https://github.com/spantaleev/matrix-docker-ansible-deploy.git
    dest: "{{ local_repository_directory }}"
    update: yes
  delegate_to: localhost
  become: false

- name: create temporary inventory directory
  tempfile:
    state: directory
  register: matrix_inventory_tmp_dir
  delegate_to: localhost
  become: false

- name: protect inventory directory
  file:
    path: "{{ matrix_inventory_tmp_dir.path }}"
    mode: '0700'
  delegate_to: localhost
  become: false

# Host file

- name: set the hosts files path
  set_fact:
    hosts_path: "{{ matrix_inventory_tmp_dir.path }}/hosts.yml"

- name: create hosts.yml
  template: 
    src:  "hosts.yml.j2" 
    dest: "{{hosts_path}}"
  become: false
  delegate_to: localhost

# Vars file

- name: set the vars folder path
  set_fact:
    vars_folder_path: "{{ matrix_inventory_tmp_dir.path }}/host_vars/{{inventory_hostname}}/"

- name: create vars directory
  file:
    path: "{{vars_folder_path}}"
    state: directory
    recurse: yes
  delegate_to: localhost
  become: false

- name: set the vars path
  set_fact:
    vars_file_path: "{{ vars_folder_path }}vars.yml"

- name: create vars.yml
  template: 
    src:  "vars.yml.j2" 
    dest: "{{vars_file_path}}"
  become: false
  delegate_to: localhost

- name: show variable information
  debug:
    msg: "hosts_path: {{hosts_path}}\nmatrix_inventory_tmp_dir:{{ matrix_inventory_tmp_dir }}"
  when: enable_debug | bool

- name: install requirements
  local_action: command just roles
  args:
    chdir: "{{ local_repository_directory }}"
  become: false

- name: play matrix-docker-ansible-deploy
  local_action: "command ansible-playbook -i {{hosts_path}} {{local_repository_directory}}/setup.yml -vvv --tags={{applications.matrix.playbook_tags}}"
  become: false


  
#- name: delete inventory directory
#  file:
#    path: "{{ matrix_inventory_tmp_dir.path }}"
#    state: absent
#  delegate_to: localhost
#  become: false
#




#- name: "create {{docker_compose.directories.instance}}"
#  file:
#    path: "{{docker_compose.directories.instance}}"
#    state: directory
#    mode: 0755
#
#- name: "create {{docker_compose.directories.instance}}mautrix_whatsapp"
#  file:
#    path: "{{docker_compose.directories.instance}}mautrix_whatsapp"
#    state: directory
#    mode: 0755
#
#- name: add whatsapp-bridge-config.yml
#  template: 
#    src:  "whatsapp-bridge-config.yml.j2" 
#    dest: "{{docker_compose.directories.instance}}mautrix_whatsapp/config.yml"
#  notify: recreate matrix
#
#- name: add element-config.json
#  template: 
#    src:  "element-config.json.j2" 
#    dest: "{{docker_compose.directories.instance}}element-config.json"
#  notify: recreate matrix
#
#- name: add homeserver.yaml
#  template: 
#    src:  "homeserver.yaml.j2" 
#    dest: "{{docker_compose.directories.instance}}homeserver.yaml"
#  notify: recreate matrix
#
#- name: add log.config
#  template: 
#    src:  "log.config.j2" 
#    dest: "{{docker_compose.directories.instance}}{{domains.matrix.synapse}}.log.config"
#  notify: recreate matrix
#
## https://github.com/matrix-org/synapse/issues/6303
#- name: set correct folder permissions
#  command:
#    cmd: "docker run --rm --mount type=volume,src=matrix_synapse_data,dst=/data -e SYNAPSE_SERVER_NAME={{domains.matrix.synapse}} -e SYNAPSE_REPORT_STATS=no --entrypoint /bin/sh matrixdotorg/synapse:latest -c 'chown -vR 991:991 /data'"
#
#- name: add docker-compose.yml
#  template: 
#    src:  "docker-compose.yml.j2" 
#    dest: "{{docker_compose.directories.instance}}docker-compose.yml"
#  notify: recreate matrix
