---
- name: create docker-compose.yml for bigbluebutton
  command:
    cmd: bash ./scripts/generate-compose
    chdir: "{{docker_compose.directories.instance}}"
  environment:
    COMPOSE_HTTP_TIMEOUT: 600
    DOCKER_CLIENT_TIMEOUT: 600
  listen: setup bigbluebutton

- name: replace postgres bind mount by volume mount
  replace:
    path: "{{docker_compose_file}}"
    regexp: '\./postgres-data:/var/lib/postgresql/data'
    replace: 'database:/var/lib/postgresql/data'
  listen: setup bigbluebutton

- name: replace greenlight bind mount by volume mount
  replace:
    path: "{{docker_compose_file}}"
    regexp: '\./greenlight-data:/usr/src/app/storage'
    replace: 'greenlight:/usr/src/app/storage'
  listen: setup bigbluebutton

- name: add volume to redis
  lineinfile:
    path: "{{ docker_compose_file }}"
    insertafter: "^\\s*redis:"
    line: "    volumes:\n      - redis:/data"
    firstmatch: yes
  listen: setup bigbluebutton

- name: add volume to coturn
  lineinfile:
    path: "{{ docker_compose_file }}"
    insertafter: "- ./mod/coturn/turnserver.conf:/etc/coturn/turnserver.conf"
    line: "      - coturn:/var/lib/coturn"
  listen: setup bigbluebutton

# Implemented due to etherpad health bug.
# @todo Remove when health check is working fine
# @see https://chatgpt.com/c/67a0fc7e-5104-800f-bb6b-3731e2f83b7b
- name: "Update docker-compose.yml for Etherpad health check"
  lineinfile: 
    line: "    healthcheck:\n      test: [\"CMD\", \"curl\", \"-f\", \"http://127.0.0.1:9001\"]\n      interval: 30s\n      timeout: 10s\n      retries: 5\n      start_period: 10s"
    path: "{{docker_compose_file}}"
    insertafter: "etherpad:"
  listen: setup bigbluebutton

- name: add volumes to docker compose
  blockinfile:
    path: "{{docker_compose_file}}"
    block: |2
        database:
        greenlight:
        redis:
        coturn:
    marker: "# {mark} ANSIBLE MANAGED BLOCK FOR VOLUMES"
    insertafter: "html5-static:"
  listen: setup bigbluebutton

- name: docker compose up bigbluebutton
  command:
    cmd: "docker-compose -p bigbluebutton up -d --force-recreate{% if mode_cleanup | bool %} --remove-orphans{% endif %}"
    # Don't use the --build flag here. This leads to bugs
    chdir: "{{docker_compose.directories.instance}}"
  environment:
    COMPOSE_HTTP_TIMEOUT: 600
    DOCKER_CLIENT_TIMEOUT: 600
  listen: setup bigbluebutton