ARG DEBIAN_FRONTEND=noninteractive
ARG DEBCONF_NOWARNINGS=yes

# locales ~ 20 Mb
# libicu-dev ~ 50 Mb
# libpq-dev ~ 14 Mb
# libzip-dev ~ 2 Mb
# libbz2-dev ~ 1 Mb
# libfreetype6-dev ~ 6 Mb
# libpng-dev ~ 3 Mb
# libwebp-dev ~ 2 Mb
# libjpeg62-turbo-dev ~ 2 Mb

# apt install does not support -qq options and not to be quited long starter message with accumulative info about installed packages
# also apt upgrade and apt install with -qq option non propose automatic -y option, instead of apt-get install or apt-get upgrade
# But apt update is supporting -qq options for quiting output
RUN apt -qq update &&  \
    apt-get install locales libicu-dev libpq-dev libzip-dev libbz2-dev \
                    libfreetype6-dev libpng-dev libwebp-dev libjpeg62-turbo-dev -qq

# For php7.4-fpm image - need to be upgraded apt packages
RUN if [ $(php -r "echo PHP_MAJOR_VERSION;") = "7" ]; then \
    apt-get -qq upgrade \
;fi

###########################################################################
# Locale:
###########################################################################

RUN locale-gen en_US.UTF-8

###########################################################################
# Install PHP-extensions:
###########################################################################

RUN docker-php-ext-install -j$(nproc) iconv

RUN docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp && \
    docker-php-ext-install -j$(nproc) gd

# opcache already installed and only need enable to
RUN docker-php-ext-enable opcache

# some extension already loaded by default (of course if or after is installed)
ARG EXTENSIONS='pdo intl mysqli pgsql zip bcmath bz2 pdo_mysql pdo_pgsql'

# Extension automatic enable in helper install script
# See: https://github.com/devilbox/docker-php-fpm-7.4/blob/master/data/docker-php-ext-install#L111
RUN docker-php-ext-install ${EXTENSIONS}

###########################################################################
# xDebug:
###########################################################################

# For PHP 7.4 xdebug 3.1 also is supported, but staty xdebug 2 version for compatibility experiences
# See https://xdebug.org/docs/compat
# And see exactly xdebug minor versions: https://pecl.php.net/package/xdebug
RUN PHP_MAJOR_VERSION=$(php -r 'echo PHP_MAJOR_VERSION;') && \
    PHP_MINOR_VERSION=$(php -r 'echo PHP_MINOR_VERSION;') && \
    if [ $PHP_MAJOR_VERSION = '8' ]; then \
      pecl install xdebug-3.4.1; \
    elif [ $PHP_MAJOR_VERSION = '7' ]; then \
      pecl install xdebug-2.9.8; \
    else \
      echo "Not support that major PHP version: $PHP_MAJOR_VERSION"; \
      exit 1; \
    fi && \
    docker-php-ext-enable xdebug && \
    sed -i 's/^zend_extension=/;zend_extension=/g' "$PHP_INI_DIR/conf.d/docker-php-ext-xdebug.ini"

###########################################################################
# Redis:
###########################################################################

RUN echo '' | pecl install redis && \
    docker-php-ext-enable redis

###########################################################################
# Prepend nginx 502 on showing errors:
# @see https://stackoverflow.com/questions/55260221/laravel-php-7-3-nginx-502-upstream-prematurely-closed-fastcgi-stdout
###########################################################################

RUN sed -i 's/^log_limit = .*/log_limit = 1024/g' "$PHP_INI_DIR/../php-fpm.d/docker.conf"

###########################################################################
# php.ini opts:
###########################################################################

# https://github.com/php/php-src/blob/master/php.ini-development
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

###########################################################################
# Tune opts:
###########################################################################

ARG PHP_OPT_MEMORY_LIMIT=256M
RUN sed -i "s/^memory_limit = .*/memory_limit = $PHP_OPT_MEMORY_LIMIT/g" "$PHP_INI_DIR/php.ini"

ARG PHP_OPT_POST_MAX_SIZE=300M
RUN sed -i "s/^post_max_size = .*/post_max_size = $PHP_OPT_UPLOAD_MAX_FILESIZE/g" "$PHP_INI_DIR/php.ini"

ARG PHP_OPT_UPLOAD_MAX_FILESIZE=100M
RUN sed -i "s/^upload_max_filesize = .*/upload_max_filesize = $PHP_OPT_UPLOAD_MAX_FILESIZE/g" "$PHP_INI_DIR/php.ini"

ARG PHP_OPT_MAX_EXECUTION_TIME=600
RUN sed -i "s/^max_execution_time = .*/max_execution_time = $PHP_OPT_MAX_EXECUTION_TIME/g" "$PHP_INI_DIR/php.ini"

#
#--------------------------------------------------------------------------
# Final Touch
#--------------------------------------------------------------------------
#

# Clean up
RUN apt clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
    rm /var/log/lastlog /var/log/faillog
