---
- name: Check that the somefile.conf exists
  stat:
    path: /usr/share/jitsi-meet/scripts/install-letsencrypt-cert-with-param.sh
  register: ssl_file

- name: Conf SSL MultiDomain - Duplication du script Let's Encrypt d'origine
  copy:
    src: /usr/share/jitsi-meet/scripts/install-letsencrypt-cert.sh
    dest: /usr/share/jitsi-meet/scripts/install-letsencrypt-cert-with-param.sh
    mode: u+x
    remote_src: true
  when: not ssl_file.stat.exists

- name: Conf SSL MultiDomain - rend le nom de domaine paramétrable dans le script certbot de jitsi
  replace:
    path: /usr/share/jitsi-meet/scripts/install-letsencrypt-cert-with-param.sh
    regexp: '^DOMAIN="\${DEB_CONF_RESULT##\*:}"'
    replace: "DOMAIN=$1"

- name: Conf SSL MultiDomain - Exécution du script lets encrypt
  command: "/usr/share/jitsi-meet/scripts/install-letsencrypt-cert-with-param.sh {{ item }}"
  args:
    creates: /etc/letsencrypt/live/{{ item }}/fullchain.pem  # Execute le script que si ce fichier n'existe pas
  loop: "{{ jitsi_multidomain_domain }}"

- name: Conf Jitsi MultiDomain - Change le ServerName des conf nginx
  replace:
    path: /etc/nginx/sites-available/{{ item }}.conf
    regexp: "[^?]ssl_certificate /etc/letsencrypt/live/{{ inventory_hostname }}.*$"
    replace: "ssl_certificate /etc/letsencrypt/live/{{ item }}/fullchain.pem;"
  loop: "{{ jitsi_multidomain_domain }}"
  notify:
    - restart nginx

- name: Conf Jitsi MultiDomain - Change le ServerName des conf nginx
  replace:
    path: /etc/nginx/sites-available/{{ item }}.conf
    regexp: "[^?]ssl_certificate_key /etc/letsencrypt/live/{{ inventory_hostname }}.*$"
    replace: "ssl_certificate_key /etc/letsencrypt/live/{{ item }}/privkey.pem;"
  loop: "{{ jitsi_multidomain_domain }}"
  notify:
    - restart nginx
