{% import 'Dockerfile/docker.jinja2'    as docker %}
{% import 'Dockerfile/provision.jinja2' as provision %}
{% import 'Dockerfile/services.jinja2'  as services %}

{% macro official(role='', version='', distribution='') -%}
    # Install php environment
    && apt-install \
        # Install tools
        imagemagick \
        graphicsmagick \
        ghostscript \
        jpegoptim \
        libjpeg-turbo-progs \
        pngcrush \
        optipng \
        apngopt \
        pngnq \
        pngquant \
        # Libraries
        libvips42 \
        librabbitmq4 \
{%- if distribution == 'bookworm' %}
        libldap-2.5-0 \
{%- else %}        
        libldap-2.4-2 \
        zlibc \
{%- endif %}
        libxslt1.1 \
        libldap-common \
        zlib1g \
        libpq5 \
        libpng16-16 \
        libmcrypt4 \
        libzip4 \
        # Dev and headers
        libjpeg62-turbo-dev \
        libwebp-dev \
        libfreetype6-dev \
        libbz2-dev \
        libicu-dev \
        libldap2-dev \
        libldb-dev \
        libmcrypt-dev \
        libxml2-dev \
        libxslt1-dev \
        zlib1g-dev \
        libmemcached-dev \
        libpng-dev \
        libpq-dev \
        libzip-dev \
        libc-client-dev \
        libc-client2007e-dev \
        libkrb5-dev \
        libvips-dev \
        librabbitmq-dev \
        libmagick++-dev \
        libgmp-dev \
    # Install guetzli
    && wget https://github.com/google/guetzli/archive/master.zip \
    && unzip master.zip \
    && make -C guetzli-master \
    && cp guetzli-master/bin/Release/guetzli /usr/local/bin/ \
    && rm -rf master.zip guetzli-master \
    # Install new version of ICU
    && curl -sS -o /tmp/icu.tar.gz -L https://github.com/unicode-org/icu/releases/download/release-73-1/icu4c-73_1-src.tgz \
    && tar -zxf /tmp/icu.tar.gz -C /tmp && cd /tmp/icu/source && ./configure --prefix=/usr/local && make && make install && cd / && rm -rf /tmp/icu* \
    # Install extensions
{%- if version != '7.4' and version != '8.0' and version != '8.1' and version != '8.2' and version != '8.3' %}
    && docker-php-ext-configure intl --with-icu-dir=/usr/local \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-webp-dir=/usr/include/ \
{%- else %}
    && PKG_CONFIG_PATH=/usr/local docker-php-ext-configure intl \
    && docker-php-ext-configure gd --with-jpeg --with-freetype --with-webp \
{%- endif %}
{%- if version == '8.0' or version == '8.1' or version == '8.2' or version == '8.3' %}
    && git clone --branch latest --depth 1 https://github.com/php-amqp/php-amqp.git /usr/src/php/ext/amqp \
    && cd /usr/src/php/ext/amqp && git submodule update --init \
    && git clone --branch master --depth 1 https://github.com/Imagick/imagick.git /usr/src/php/ext/imagick \
{%- endif %}
    && docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ \
    && PHP_OPENSSL=yes docker-php-ext-configure imap --with-kerberos --with-imap-ssl \
    && docker-php-ext-install \
        bcmath \
{%- if version == '8.0' or version == '8.1' or version == '8.2' or version == '8.3' %}
        amqp \
        imagick \
        ffi \
{%- endif %}
        bz2 \
        calendar \
        exif \
        ftp \
        gmp \
        intl \
        gettext \
        ldap \
        mysqli \
{%- if version|int == 5 or version == '7.0' or version == '7.1' %}
        mcrypt \
{%- endif %}
        imap \
{%- if version != '7.4' and version != '8.0' and version != '8.1' and version != '8.2' and version != '8.3' %}
        hash \
{%- endif %}
        pcntl \
        pdo_mysql \
        pdo_pgsql \
        pgsql \
        soap \
        sockets \
{%- if version != '8.1' and version != '8.2' and version != '8.3' %}
        tokenizer \
{%- endif %}
        sysvmsg \
        sysvsem \
        sysvshm \
        shmop \
{%- if version != '8.0' and version != '8.1' and version != '8.2' and version != '8.3' %}
        xmlrpc \
{%- endif %}
        xsl \
        zip \
        gd \
        gettext \
        opcache \
{%- if version|int == 5 %}
    # Install extensions for PHP 5.x
    && pecl install apcu-4.0.11 \
    && printf "no --disable-memcached-sasl\n" | pecl install memcached-2.2.0 \
    && pecl install redis-4.3.0 \
    && pecl install mongodb \
    && pecl install imagick \
    && echo extension=apcu.so > /usr/local/etc/php/conf.d/apcu.ini \
    && echo extension=redis.so > /usr/local/etc/php/conf.d/redis.ini \
    && echo extension=imagick.so > /usr/local/etc/php/conf.d/imagick.ini \
    && echo extension=mongodb.so > /usr/local/etc/php/conf.d/mongodb.ini \
    && echo extension=memcached.so > /usr/local/etc/php/conf.d/memcached.ini \
{%- elif version == '8.0' or version == '8.1' or version == '8.2' or version == '8.3' %}
    && pecl install memcached \
    && pecl install apcu \
    && pecl install excimer \
    && pecl install opentelemetry \
    && pecl install protobuf \
    && pecl install xmlrpc-1.0.0RC3 \
    && pecl install vips \
    && pecl install redis \
    && pecl install mongodb \
    && echo extension=apcu.so > /usr/local/etc/php/conf.d/apcu.ini \
    && echo extension=excimer.so > /usr/local/etc/php/conf.d/excimer.ini \
    && echo extension=opentelemetry.so > /usr/local/etc/php/conf.d/opentelemetry.ini \
    && echo extension=protobuf.so > /usr/local/etc/php/conf.d/protobuf.ini \
    && echo extension=redis.so > /usr/local/etc/php/conf.d/redis.ini \
    && echo extension=mongodb.so > /usr/local/etc/php/conf.d/mongodb.ini \
    && echo extension=memcached.so > /usr/local/etc/php/conf.d/memcached.ini \
    && echo extension=vips.so > /usr/local/etc/php/conf.d/vips.ini \
    && echo extension=xmlrpc.so > /usr/local/etc/php/conf.d/xmlrpc.ini \
{%- else %}
    # Install extensions for PHP 7.x
    && pecl install apcu \
    && printf "no --disable-memcached-sasl\n" | pecl install memcached \
    && echo extension=memcached.so > /usr/local/etc/php/conf.d/memcached.ini \
    # Install vips (only works with PHP >= 7.0)
    && printf "\n" | pecl install vips \
    && echo extension=vips.so > /usr/local/etc/php/conf.d/vips.ini \
    && pecl install redis \
    && pecl install mongodb \
    && pecl install imagick \
    && pecl install amqp \
    && echo extension=apcu.so > /usr/local/etc/php/conf.d/apcu.ini \
    && echo extension=redis.so > /usr/local/etc/php/conf.d/redis.ini \
    && echo extension=imagick.so > /usr/local/etc/php/conf.d/imagick.ini \
    && echo extension=mongodb.so > /usr/local/etc/php/conf.d/mongodb.ini \
    && echo extension=amqp.so > /usr/local/etc/php/conf.d/amqp.ini \
{%- endif %}
    # Uninstall dev and header packages
    && apt-get purge -y -f --force-yes \
        libc-client-dev \
        libkrb5-dev \
        libbz2-dev \
        libicu-dev \
        libldap2-dev \
        libldb-dev \
        libmcrypt-dev \
        libxml2-dev \
        libxslt1-dev \
        zlib1g-dev \
        libpng-dev \
        libwebp-dev \
        libpq-dev \
        libzip-dev \
        libvips-dev \
        librabbitmq-dev \
        libmagick++-dev \
        libgmp-dev \
    && rm -f /usr/local/etc/php-fpm.d/zz-docker.conf \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer2 \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer1 --1 \
    && ln -sf /usr/local/bin/composer2 /usr/local/bin/composer \
    {{ services.php() }}
{%- endmacro %}

{% macro officialAlpine(role='', version='') -%}
    # Install php environment
    && apk-install \
        imagemagick \
        graphicsmagick \
        ghostscript \
        jpegoptim \
        pngcrush \
        optipng \
        pngquant \
        vips \
        rabbitmq-c \
        c-client \
        # Libraries
        libldap \
        icu-libs \
        libintl \
        libpq \
        libxslt \
        libgomp \
        libzip \
        libmemcached \
        yaml \
        # Build dependencies
        linux-headers \
        autoconf \
        g++ \
        make \
        libtool \
        pcre-dev \
        gettext-dev \
        freetype-dev \
        gmp-dev \
        libjpeg-turbo-dev \
        libpng-dev \
        vips-dev \
        krb5-dev \
        openssl-dev \
        imap-dev \
        imagemagick-dev \
        rabbitmq-c-dev \
        openldap-dev \
        icu-dev \
        postgresql-dev \
        libxml2-dev \
        ldb-dev \
        pcre-dev \
        libxslt-dev \
        libzip-dev \
        libmemcached-dev \
        yaml-dev \
    # Install guetzli
    && wget https://github.com/google/guetzli/archive/master.zip \
    && unzip master.zip \
    && make -C guetzli-master \
    && cp guetzli-master/bin/Release/guetzli /usr/local/bin/ \
    && rm -rf master.zip guetzli-master \
    # https://github.com/docker-library/php/issues/240
    && apk add gnu-libiconv --update-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing/ --allow-untrusted \
    # Install new version of ICU
    && curl -sS -o /tmp/icu.tar.gz -L https://github.com/unicode-org/icu/releases/download/release-73-1/icu4c-73_1-src.tgz \
    && tar -zxf /tmp/icu.tar.gz -C /tmp && cd /tmp/icu/source && ./configure --prefix=/usr/local && make && make install && cd / && rm -rf /tmp/icu* \
    # Install extensions
{%- if version == '7.4' or version == '8.0' or version == '8.1' or version == '8.2' or version == '8.3' %}
    && PKG_CONFIG_PATH=/usr/local docker-php-ext-configure intl \
    && docker-php-ext-configure gd --with-jpeg --with-freetype --with-webp \
    && git clone --branch master --depth 1 https://github.com/Imagick/imagick.git /usr/src/php/ext/imagick \
    && git clone --branch latest --depth 1 https://github.com/php-amqp/php-amqp.git /usr/src/php/ext/amqp \
    && cd /usr/src/php/ext/amqp && git submodule update --init \
{%- else %}
    && docker-php-ext-configure intl --with-icu-dir=/usr/local \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-webp-dir=/usr/include/ \
{%- endif %}
    && docker-php-ext-configure ldap \
    && PHP_OPENSSL=yes docker-php-ext-configure imap --with-kerberos --with-imap-ssl \
    && docker-php-ext-install \
        bcmath \
        bz2 \
        calendar \
        exif \
        ftp \
        gmp \
{%- if version == '7.4' or version == '8.0' or version == '8.1' or version == '8.2' or version == '8.3' %}
        ffi \
        imagick \
        amqp \
{%- endif %}
        intl \
        gettext \
        ldap \
        mysqli \
        imap \
{%- if version != '7.4' and version != '8.0' and version != '8.1' and version != '8.2' and version != '8.3' %}
        hash \
{%- endif %}
        pcntl \
        pdo_mysql \
        pdo_pgsql \
        pgsql \
        soap \
        sockets \
{%- if version != '8.1' and version != '8.2' and version != '8.3' %}
        tokenizer \
{%- endif %}
        sysvmsg \
        sysvsem \
        sysvshm \
        shmop \
{%- if version != '8.0' and version != '8.1' and version != '8.2' and version != '8.3' %}
        xmlrpc \
{%- endif %}
        xsl \
        zip \
        gd \
        gettext \
        opcache \
    # Install extensions for PHP 7.x
    {%- if version == '7.3' or version == '7.4' or version == '8.0' or version == '8.1' or version == '8.2' or version == '8.3' %}
    # Memcached for 7.3 can currently only be built from master
    && MEMCACHED="`mktemp -d`" \
    && curl -skL https://github.com/php-memcached-dev/php-memcached/archive/master.tar.gz | tar zxf - --strip-components 1 -C $MEMCACHED \
    && docker-php-ext-configure $MEMCACHED \
    && docker-php-ext-install $MEMCACHED \
    && rm -rf $MEMCACHED \
    {%- else %}
    && printf "no --disable-memcached-sasl\n" | pecl install memcached-3.0.4 \
    && docker-php-ext-enable memcached \
    {%- endif %}
{%- if version == '8.0' or version == '8.1' or version == '8.2' or version == '8.3' %}
    && pecl install apcu \
    && pecl install excimer \
    && pecl install opentelemetry \
    && pecl install protobuf \
    && pecl install vips \
    && pecl install yaml \
    && pecl install redis \
    && pecl install mongodb \
    && pecl install xmlrpc-1.0.0RC3 \
    && docker-php-ext-enable \
        apcu \
        excimer \
        opentelemetry \
        protobuf \
        vips \
        yaml \
        redis \
        xmlrpc \
        imagick \
        mongodb \
{%- else %}
    # Install vips (only works with PHP >= 7.0)
    && pecl install apcu \
    && printf "\n" | pecl install vips \
    && pecl install redis \
    && pecl install mongodb \
    {%- if version != '7.4' %}
    && pecl install imagick \
    && pecl install amqp \
    {%- endif %}
    && pecl install yaml \
    && docker-php-ext-enable \
        apcu \
        redis \
        mongodb \
        amqp \
        vips \
        yaml \
{%- endif %}
    # Uninstall dev and header packages
    && apk del -f --purge \
        autoconf \
        linux-headers \
        g++ \
        make \
        libtool \
        pcre-dev \
        gettext-dev \
        freetype-dev \
        gmp-dev \
        libjpeg-turbo-dev \
        libpng-dev \
        vips-dev \
        krb5-dev \
        openssl-dev \
        imap-dev \
        rabbitmq-c-dev \
        imagemagick-dev \
        openldap-dev \
        icu-dev \
        postgresql-dev \
        libxml2-dev \
        ldb-dev \
        pcre-dev \
        libxslt-dev \
        libzip-dev \
        libmemcached-dev \
        yaml-dev \
    && rm -f /usr/local/etc/php-fpm.d/zz-docker.conf \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer2 \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer1 --1 \
    && ln -sf /usr/local/bin/composer2 /usr/local/bin/composer \
    {{ services.php() }}
{%- endmacro %}

{% macro officialDevelopment(role='', version='') -%}
    # Install development environment
    && BLACKFIRE_INSTALL_METHOD=raw bash -c "$(curl -L https://installer.blackfire.io/installer.sh)" \
    && blackfire php:install \
    && echo 'deb https://packages.tideways.com/apt-packages-main any-version main' | tee /etc/apt/sources.list.d/tideways.list \
    && wget -qO - https://packages.tideways.com/key.gpg | apt-key add - \
    && apt-install tideways-php tideways-daemon \
    {%- if version|int == 5 %}
    && pecl install xdebug-2.5.5 \
    {%- elif version == '8.0' or version == '8.1' or version == '8.2' or version == '8.3' %}
    && pecl install xdebug \
    {%- elif version == '7.0' or version == '7.1' %}
    && pecl install xdebug-2.8.1 \
    {%- elif version == '7.4' %}
    && pecl install xdebug-3.1.6 \
    {%- else %}
    && pecl install xdebug \
    {%- endif %}
    && docker-php-ext-enable xdebug \
    {{ services.phpDev() }}
{%- endmacro %}

{% macro officialDevelopmentAlpine(role='', version='') -%}
    # Install development environment
    && bash -c "$(curl -L https://installer.blackfire.io/installer.sh)" \
    && blackfire php:install \
    && apk-install \
        linux-headers \
        make \
        autoconf \
        g++ \
    {%- if version|int == 5 %}
    && pecl install xdebug-2.5.5 \
    {%- elif version == '7.0' or version == '7.1' %}
    && pecl install xdebug-2.8.1 \
    {%- elif version == '7.4' %}
    && pecl install xdebug-3.1.6 \
    {%- else %}
    && pecl install xdebug \
    {%- endif %}
    && apk del -f --purge \
        autoconf \
        linux-headers \
        g++ \
        make \
    && docker-php-ext-enable xdebug \
    {{ services.phpDev() }}
{%- endmacro %}
