{% include 'roles/docker-compose/templates/base.yml.j2' %}

{% set service_name = NEXTCLOUD_PROXY_SERVICE %}
  {{ service_name }}:
    image: "{{ NEXTCLOUD_PROXY_IMAGE }}:{{ NEXTCLOUD_PROXY_VERSION }}"
    container_name: "{{ NEXTCLOUD_PROXY_CONTAINER }}"
    logging:
      driver: {{ "json-file" if IS_CONTAINER | bool else 'journald' }}
    restart: {{ DOCKER_RESTART_POLICY }}
    ports:
      - "127.0.0.1:{{ NEXTCLOUD_PORT }}:{{ container_port }}"
    volumes:
      - "{{ NEXTCLOUD_HOST_NGINX_PATH }}:/etc/nginx/nginx.conf:ro"
    volumes_from:
      - {{ NEXTCLOUD_SERVICE }}
{% include 'roles/docker-container/templates/healthcheck/curl.yml.j2'  %}
    networks:
      default:
        ipv4_address: 192.168.102.67
{% if NEXTCLOUD_HPB_NETWORK_MODE == 'host' %}
    extra_hosts:
      - "host.docker.internal:host-gateway"
{% endif %}

{% set service_name = NEXTCLOUD_SERVICE %}
  {{ service_name }}:
    image: "{{ NEXTCLOUD_CUSTOM_IMAGE }}"
    container_name: {{ NEXTCLOUD_CONTAINER }}
    {{ lookup('template', 'roles/docker-container/templates/build.yml.j2') | indent(4) }}

    volumes:
      - data:{{ NEXTCLOUD_DOCKER_WORK_DIRECTORY }}
      - {{ NEXTCLOUD_HOST_CONF_ADD_PATH }}:{{ NEXTCLOUD_DOCKER_CONF_ADD_PATH }}:ro
    healthcheck:
      test: ["CMD", "su", "www-data", "-s", "/bin/sh", "-c", "php {{ NEXTCLOUD_INTERNAL_OCC_COMMAND }} status"]
      interval: 1m
      timeout: 10s
      retries: 3
{% include 'roles/docker-container/templates/base.yml.j2' %}
{% include 'roles/docker-container/templates/depends_on/dmbs_excl.yml.j2' %}
{% include 'roles/docker-container/templates/networks.yml.j2' %}
        ipv4_address: 192.168.102.69

{% if NEXTCLOUD_HPB_SIGNALING_ENABLED | bool %}
{% set service_name = NEXTCLOUD_HPB_SERVICE %}
  {{ service_name }}:
{% set container_port = NEXTCLOUD_HPB_SIGNALING_PORT %}
{% include 'roles/docker-container/templates/base.yml.j2' %}
{% include 'roles/docker-container/templates/healthcheck/tcp.yml.j2' %}
    image: "{{ NEXTCLOUD_HPB_IMAGE }}:{{ NEXTCLOUD_HPB_VERSION }}"
    container_name: {{ NEXTCLOUD_HPB_CONTAINER }}
    init: true
{% if NEXTCLOUD_HPB_NETWORK_MODE == 'bridge' and NEXTCLOUD_HPB_TURN_ONBOARD_ENABLED | bool %}
    ports:
      - {{ networks.internet.ip4 }}:{{ NEXTCLOUD_HPB_TURN_ONBOARD_PORT }}:{{ NEXTCLOUD_HPB_TURN_ONBOARD_PORT }}/tcp
      - {{ networks.internet.ip4 }}:{{ NEXTCLOUD_HPB_TURN_ONBOARD_PORT }}:{{ NEXTCLOUD_HPB_TURN_ONBOARD_PORT }}/udp
    expose:
      - "{{ container_port }}"
    networks:
      default:
        ipv4_address: 192.168.102.68
{% else %}
  network_mode: {{ NEXTCLOUD_HPB_NETWORK_MODE }}
{% endif %}
{% endif %}

{% if NEXTCLOUD_WHITEBOARD_ENABLED %}
{% set service_name = NEXTCLOUD_WHITEBOARD_SERVICE %}
  {{ service_name }}:
{% set container_port = NEXTCLOUD_WHITEBOARD_PORT_INTERNAL %}
{% include 'roles/docker-container/templates/base.yml.j2' %}
    build:
      context: .
      dockerfile: {{ NEXTCLOUD_WHITEBOARD_SERVICE_DOCKERFILE }}
    pull_policy: never
{% include 'roles/docker-container/templates/healthcheck/nc.yml.j2' %}
    image: "{{ NEXTCLOUD_WHITEBOARD_CUSTOM_IMAGE }}"
    container_name: {{ NEXTCLOUD_WHITEBOARD_CONTAINER }}
    volumes:
      - whiteboard_tmp:/tmp
      - whiteboard_fontcache:/var/cache/fontconfig
    environment:
      - NODE_OPTIONS=--max-old-space-size={{ NEXTCLOUD_WHITEBOARD_MAX_OLD_SPACE_SIZE }}
    expose:
      - "{{ container_port }}"
    shm_size: 1g
    networks:
      default:
        ipv4_address: 192.168.102.71
    depends_on:
      redis:
        condition: service_healthy
{% endif %}


{% set service_name = NEXTCLOUD_CRON_SERVICE %}
  {{ service_name }}:
    container_name: "{{ NEXTCLOUD_CRON_CONTAINER }}"
    image: "{{ NEXTCLOUD_IMAGE }}:{{ NEXTCLOUD_VERSION }}"
    restart: {{ DOCKER_RESTART_POLICY }}
    logging:
      driver: {{ "json-file" if IS_CONTAINER | bool else 'journald' }}
    volumes:
      - data:{{ NEXTCLOUD_DOCKER_WORK_DIRECTORY }}
    entrypoint: /cron.sh
    healthcheck:
      test: ["CMD", "su", "www-data", "-s", "/bin/sh", "-c", "php {{ NEXTCLOUD_INTERNAL_OCC_COMMAND }} status"]
      interval: 1m
      timeout: 10s
      retries: 3
{% include 'roles/docker-container/templates/depends_on/dmbs_excl.yml.j2' %}
{% include 'roles/docker-container/templates/networks.yml.j2' %}
        ipv4_address: 192.168.102.70

{% if NEXTCLOUD_RECORDING_ENABLED | bool %}
{% set service_name = 'talk_recording' %}
  {{ service_name }}:
{% include 'roles/docker-container/templates/base.yml.j2' %}
    image: "{{ NEXTCLOUD_RECORDING_IMAGE }}:{{ NEXTCLOUD_RECORDING_VERSION }}"
    container_name: "{{ NEXTCLOUD_RECORDING_CONTAINER }}"
    environment:
      - HPB_DOMAIN={{ NEXTCLOUD_HPB_DOMAIN }}
      - NC_DOMAIN={{ NEXTCLOUD_DOMAIN }}
      - RECORDING_SECRET={{ NEXTCLOUD_RECORDING_SECRET }}
      - INTERNAL_SECRET={{ NEXTCLOUD_HPB_INTERNAL_SECRET }}
      - TZ={{ HOST_TIMEZONE }}
    expose:
      - "{{ NEXTCLOUD_RECORDING_PORT }}"
    networks:
      default:
        ipv4_address: 192.168.102.72
    volumes:
      - talk_recording_tmp:/tmp
{% endif %}

{% include 'roles/docker-compose/templates/volumes.yml.j2' %}
  data:
    name: {{ NEXTCLOUD_VOLUME }}
{% if NEXTCLOUD_WHITEBOARD_ENABLED %}
  whiteboard_tmp:
    name: {{ NEXTCLOUD_WHITEBOARD_TMP_VOLUME }}
  whiteboard_fontcache:
    name: {{ NEXTCLOUD_WHITEBOARD_FRONTCACHE_VOLUME }}
{% endif %}
{% if NEXTCLOUD_RECORDING_ENABLED | bool %}
  talk_recording_tmp:
    name: {{ NEXTCLOUD_RECORDING_TMP_VOLUME }}
{% endif %}

{% include 'roles/docker-compose/templates/networks.yml.j2' %}
