FROM php:7.4-fpm

ARG TIMEZONE
ARG BLACKFIRE_SERVER_ID
ARG BLACKFIRE_SERVER_TOKEN

ENV OS=linux
ENV BLACKFIRE_SOCKET unix:///var/run/blackfire/agent.sock
ENV BLACKFIRE_LOG_LEVEL 4
ENV COMPOSER_HOME /var/composer
ENV COMPOSER_ALLOW_SUPERUSER 1

RUN apt-get update && apt-get install -y wget curl git libcurl4-gnutls-dev zlib1g-dev libicu-dev g++ libxml2-dev libpq-dev zip libzip-dev unzip \
 libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libpng-dev libxpm-dev libjpeg-dev libwebp-dev gnupg2 poppler-utils \
 libmagickwand-dev imagemagick ghostscript \
 --no-install-recommends
RUN apt-get autoremove && apt-get autoclean \
 && rm -rf /var/lib/apt/lists/*

RUN pecl install redis apcu imagick

RUN docker-php-ext-enable redis apcu imagick
RUN docker-php-ext-configure gd  \
    --enable-gd \
    --with-webp \
    --with-jpeg \
    --with-xpm \
    --with-freetype
RUN docker-php-ext-install gettext sockets pdo pdo_mysql mysqli intl opcache gd zip bcmath

RUN #pecl install xdebug

# Install Composer
RUN mkdir /var/composer
RUN mkdir /var/composer/cache
RUN chmod -R 777 /var/composer/cache
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --2.2

# Symfony CLI
RUN wget https://get.symfony.com/cli/installer -O - | bash && mv /root/.symfony5/bin/symfony /usr/local/bin/symfony

# wkhtmltopdf requirements
RUN curl -sS http://security.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.0.0_1.0.2g-1ubuntu4_amd64.deb -o /tmp/libssl1.0.0_1.0.2g-1ubuntu4_amd64.deb \
    && dpkg -i /tmp/libssl1.0.0_1.0.2g-1ubuntu4_amd64.deb && rm /tmp/libssl1.0.0_1.0.2g-1ubuntu4_amd64.deb
# libssl-dev
RUN curl -sS http://security.ubuntu.com/ubuntu/pool/main/o/openssl/libssl-dev_1.0.2g-1ubuntu4_amd64.deb -o /tmp/libssl-dev_1.0.2g-1ubuntu4_amd64.deb \
    && dpkg -i /tmp/libssl-dev_1.0.2g-1ubuntu4_amd64.deb && rm /tmp/libssl-dev_1.0.2g-1ubuntu4_amd64.deb

# configure Imagick
RUN sed -i 's@<policy domain="coder" rights="none" pattern="PDF" />@<policy domain="coder" rights="read|write" pattern="PDF" />@g' /etc/ImageMagick-6/policy.xml

# Set timezone
RUN ln -snf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && echo ${TIMEZONE} > /etc/timezone \
    && printf '[PHP]\ndate.timezone = "%s"\n', ${TIMEZONE} > ${PHP_INI_DIR}/conf.d/tzone.ini \
    && "date"

RUN mkdir -p /opt/app/sessions && chown -R www-data:www-data /opt/app && chmod -R 777 /opt/app

RUN echo 'memory_limit = 6G' >> /usr/local/etc/php/php.ini

COPY infrastructure/conf/php/*.ini /usr/local/etc/php/conf.d/
COPY infrastructure/conf/php/www.conf /usr/local/etc/php-fpm.d/www.conf

#ARG XDEBUG_ENABLE=0
#ARG XDEBUG_MODE=debug
#RUN if [ "$XDEBUG_ENABLE" = "0" ] ; then echo "xdebug.mode=off" >> /usr/local/etc/php/conf.d/xdebug.ini ; fi
#RUN if [ "$XDEBUG_ENABLE" = "1" ] ; then echo "xdebug.mode=${XDEBUG_MODE}" >> /usr/local/etc/php/conf.d/xdebug.ini ; fi
#RUN if [ "$XDEBUG_ENABLE" = "1" ] ; then echo "zend_extension=xdebug" >> /usr/local/etc/php/conf.d/xdebug.ini ; fi
#
#RUN cat /usr/local/etc/php/conf.d/xdebug.ini
ARG USER_UID=1000
ARG USER_GID=1000
RUN addgroup --gid $USER_GID aptimap
RUN adduser --uid $USER_UID --gid $USER_GID --home /home/aptimap aptimap

USER aptimap
WORKDIR /var/www/html

COPY composer.lock composer.json ./
RUN composer install --no-scripts --no-autoloader --no-interaction

COPY ./ /var/www/html

#RUN php artisan storage:link
#ENTRYPOINT php artisan migrate:refresh

EXPOSE 9000
CMD ["php-fpm"]
