version: '3.8'

services:

  php:
    extends:
      file: docker-compose-base.yml
      service: php
    build: 
      context: ./php/${IMAGE_PHP_DIR}
      target: php_dev
      args:
        contao_version: ${CONTAO_VERSION}
        app_secret: 1234567890123456789012
    environment:
      PRJ_NAME: ${PRJ_NAME}
    tty: true
    entrypoint: /bin/bash -c
    container_name: ${COMPOSE_PROJECT_NAME_SHORT}_php
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "${PHP_FPM_EXPOSED_PORT}:9000"
    # hack to verify composer has finished its job and phpunit is available
    healthcheck:
      test: pidof php-fpm
      interval: 10s
      timeout: 10s
      retries: 20
    command:
    - >-
        echo "> ==== INIT START ====" &&
        cd ${WORKDIR_INIT} &&
        mkdir -p /var/www/.config/composer/ &&
        chmod 777 /var/www/.config/composer/ &&
        runuser -u www-data -- ./contao-init.sh &&
        cd -- &&
        echo "> ==== INIT STOP ====" &&
        echo "> ==== TEMPLATES MANAGEMENT START ====" && 
        mkdir -p ../${PRJ_NAME}/templates &&
        echo "nothing" > ../${PRJ_NAME}/templates/nothing &&
        mkdir -p ../${PRJ_NAME}/templates_save &&
        mv ../${PRJ_NAME}/templates/* ../${PRJ_NAME}/templates_save &&
        chown -R www-data:www-data ${WORKDIR_CONTAO}/var && 
        runuser -u www-data -- ${WORKDIR_CONTAO}/vendor/bin/contao-console cache:clear &&
        chown -R www-data:www-data ${WORKDIR_CONTAO}/var && 
        runuser -u www-data -- ${WORKDIR_CONTAO}/vendor/bin/contao-console contao:migrate  -vv --no-interaction --with-deletes --no-backup &&
        mv ../${PRJ_NAME}/templates_save/* ../${PRJ_NAME}/templates &&
        rm -r ../${PRJ_NAME}/templates_save/ &&
        rm ../${PRJ_NAME}/templates/nothing &&
        echo "> ==== TEMPLATES MANAGEMENT STOP ====" &&
        echo "> ==== INIT START ====" &&
        cd ${WORKDIR_INIT} &&
        mkdir -p /var/www/.config/composer/ &&
        chmod 777 /var/www/.config/composer/ &&
        runuser -u www-data -- ./contao-init.sh &&
        echo "> ==== INIT STOP ====" &&
        echo "> ==== COMPOSER INSTALL START ====" &&
        cd ${WORKDIR_CONTAO}/vendor/webexmachina/${BUNDLE_NAME}/ &&
        runuser -u www-data -- composer install --dev --prefer-dist --no-progress &&
        echo "> ==== COMPOSER INSTALL STOP ====" &&
        chown -R www-data:www-data vendor &&
        cd ${WORKDIR_CONTAO} &&
        runuser -u www-data -- ${WORKDIR_CONTAO}/vendor/bin/contao-console contao:filesync &&
        php-fpm -F
    networks:
      - common
      - db
      - php

  db:
    extends:
      file: docker-compose-base.yml
      service: db
    build: 
      context: ./db/${IMAGE_DB_DIR}
      target: db_dev
    restart: unless-stopped
    tty: true
    container_name: ${COMPOSE_PROJECT_NAME_SHORT}_db
    ports:
      - "${DB_EXPOSED_PORT}:3306"
    healthcheck:
      test: "/usr/bin/mysql --user=root --password=$DB_ROOT_PASSWORD --execute \"SHOW DATABASES;\""
      timeout: 10s
      interval: 10s
      retries: 100
    networks:
      - db

  web:
    extends:
      file: docker-compose-base.yml
      service: web
    container_name: ${COMPOSE_PROJECT_NAME_SHORT}_web
    restart: unless-stopped
    tty: true
    ports:
      - "${WEBSERVER_EXPOSED_PORT}:80"
      # - "${WEBSERVER_EXPOSED_PORT_SSL}:443"
      - "${WEBSERVER_EXPOSED_PORT_SSL}:${WEBSERVER_EXPOSED_PORT_SSL}"
    depends_on:
      - php

  
  # this container will only be built 
  # when the "php" one is built and is considered "healthy"
  # (stupid hack to force docker-compose to wait for composer to finish its work in the "php" container)
  php_dependencies_loaded:
    container_name: ${COMPOSE_PROJECT_NAME_SHORT}_php_dependencies_loaded
    image: alpine:latest
    depends_on:
      php:
        condition: service_healthy

#Docker Networks
networks:
  common:
    name: ${COMPOSE_PROJECT_NAME_SHORT}_network_common
  php:
    name: ${COMPOSE_PROJECT_NAME_SHORT}_network_php
  db:
    name: ${COMPOSE_PROJECT_NAME_SHORT}_network_db

volumes:
  static-content:
    name: ${COMPOSE_PROJECT_NAME_SHORT}-volume-shared-php-web-dev