ARG ENV

FROM registry.gitlab.com/ademe-group/docker-base-images/php-for-drupal/php:8.1-apache-alpine as SOCLE

RUN set -x \
   && apk update \
   && apk upgrade

COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint

COPY ./docker/httpd/000-default.conf /etc/apache2/sites-enabled/000-default.conf
COPY ./docker/httpd/mailcatcher.conf /etc/apache2/conf-available/
COPY ./docker/httpd/phpinfo.php /var/www/html/index.php

RUN if [ "$ENV" != "production" ] ; then \
  sed -i '/^<\/VirtualHost>/i\       \Include /etc/apache2/conf-available/mailcatcher.conf' /etc/apache2/sites-enabled/000-default.conf; \
fi

# Integrate NewRelic
# New Relic Install, and automated version of the instructions at https://docs.newrelic.com/docs/agents/php-agent/installation/php-agent-installation-tar-file
# configures some of the .ini values to use environment variables
RUN cd ~ \
    && apk add curl \
    && export NEWRELIC_VERSION="$(curl -sS https://download.newrelic.com/php_agent/release/ | sed -n 's/.*>\(.*linux-musl\).tar.gz<.*/\1/p')" \
    && curl -sS "https://download.newrelic.com/php_agent/release/${NEWRELIC_VERSION}.tar.gz" | gzip -dc | tar xf - \
    && cd "${NEWRELIC_VERSION}" \
    && NR_INSTALL_USE_CP_NOT_LN=true NR_INSTALL_SILENT=true ./newrelic-install install \
    && cd .. \
    && unset NEWRELIC_VERSION

# Override some PHP parameters (security, etc)
COPY ./docker/php/php-ini-overrides.ini /etc/php81/conf.d/php-ini-overrides.ini

WORKDIR /var/www/html/

EXPOSE 80

CMD ["apache2-foreground"]

ENTRYPOINT ["docker-entrypoint"]