FROM alpine:3.18 as build

ARG FFMPEG_VERSION=4.3.2
ARG PREFIX=/opt/ffmpeg
ARG LD_LIBRARY_PATH=/opt/ffmpeg/lib
ARG MAKEFLAGS="-j4"

RUN apk add --no-cache --update \
    build-base \
    coreutils \
    fdk-aac-dev \
    freetype-dev \
    gcc \
    lame-dev \
    libgomp \
    libogg-dev \
    libass \
    x264-libs \
    libass-dev \
    libvpx-dev \
    libvorbis-dev \
    libwebp-dev \
    libtheora-dev \
    opus-dev \
    openssl \
    openssl-dev \
    pkgconf \
    pkgconfig \
    rtmpdump-dev \
    wget \
    x264-dev \
    x265-dev \
    yasm

RUN echo http://dl-cdn.alpinelinux.org/alpine/edge/community >> /etc/apk/repositories \
    && echo http://dl-cdn.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories \
    && apk add --no-cache --update rav1e-dev

WORKDIR /tmp/
RUN wget --progress=dot:giga http://ffmpeg.org/releases/ffmpeg-${FFMPEG_VERSION}.tar.gz \
    && tar zxf ffmpeg-${FFMPEG_VERSION}.tar.gz && rm ffmpeg-${FFMPEG_VERSION}.tar.gz

# Compile ffmpeg.
WORKDIR /tmp/ffmpeg-${FFMPEG_VERSION}
RUN ./configure \
    --enable-version3 \
    --enable-gpl \
    --enable-nonfree \
    --enable-small \
    --enable-libmp3lame \
    --enable-libx264 \
    --enable-libx265 \
    --enable-libvpx \
    --enable-libtheora \
    --enable-libvorbis \
    --enable-libopus \
    --enable-libfdk-aac \
    --enable-libass \
    --enable-libwebp \
    --enable-librtmp \
    --enable-librav1e \
    --enable-postproc \
    --enable-avresample \
    --enable-libfreetype \
    --enable-filter=scale \
    --enable-filter=decimate \
    --enable-openssl \
    --disable-debug \
    --disable-doc \
    --disable-ffplay \
    --extra-cflags="-I${PREFIX}/include" \
    --extra-ldflags="-L${PREFIX}/lib" \
    --extra-libs="-lpthread -lm" \
    --prefix="${PREFIX}" \
    && make \
    && make install \
    && make distclean

# Cleanup.
RUN rm -rf /var/cache/apk/* /tmp/*

FROM php:7.4-fpm-alpine
ENV PATH=/opt/ffmpeg/bin:$PATH
ENV PYTHONUNBUFFERED=1

RUN apk add --no-cache supervisor beanstalkd zip libzip python3 libtheora rtmpdump x264-dev x265-dev \
    libpng libjpeg imagemagick imagemagick-libs imagemagick-dev libass librtmp libvpx libwebp lame pcre \
    libogg libvorbis exiftool libjpeg-turbo-utils freetype zlib x264-libs x265 libzip opus bash libgomp \
    && apk add --no-cache --virtual .build-deps zlib-dev libpng-dev jpeg-dev freetype-dev \
    libzip-dev curl-dev autoconf gcc libc-dev make \
    && python3 -m ensurepip \
    && pip3 install ffmpeg \
    && mkdir -p /var/www/ \
    && mkdir -p /scripts \
    && docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ \
    && docker-php-ext-install gd \
    && docker-php-ext-configure zip \
    && docker-php-ext-install zip \
    && docker-php-ext-install curl \
    && printf "\n" | pecl install imagick \
    && printf "\n" | pecl install apcu \
    && docker-php-ext-enable imagick \
    && apk del --no-cache .build-deps \
    && rm -rf /var/cache/apk/*

COPY --from=build /opt/ffmpeg /opt/ffmpeg
COPY --from=build /usr/lib/libssl.so* /usr/lib/
COPY --from=build /usr/lib/libcrypto.so* /usr/lib/
COPY --from=build /usr/lib/libfdk-aac.so* /usr/lib/
COPY --from=build /usr/lib/librav1e.so* /usr/lib/
COPY --from=build /usr/lib/libx264.so* /usr/lib/
COPY --from=build /usr/lib/libvpx.so* /usr/lib/
COPY --from=build /usr/lib/libx265.so* /usr/lib/

COPY docker/local/server/config/fpm-pool.conf /etc/php7/php-fpm.d/www.conf
COPY docker/local/server/config/php.ini /usr/local/etc/php/
COPY docker/local/server/config/crontab /etc/crontab

COPY docker/local/server/config/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY docker/local/server/config/supervisord/supervisord_php-fpm.conf /etc/supervisor/conf.d/supervisord_php-fpm.conf
COPY docker/local/server/config/supervisord/supervisord_beanstalkd.conf /etc/supervisor/conf.d/supervisord_beanstalkd.conf
COPY docker/local/server/config/supervisord/supervisord_ec2init.conf /etc/supervisor/conf.d/supervisord_ec2init.conf
COPY docker/local/server/config/supervisord/supervisord_cron.conf /etc/supervisor/conf.d/supervisord_cron.conf

# Import UID and GID from docker compose
ARG UID
ARG GID

ENV UID=${UID}
ENV GID=${GID}

# Create custom user with the same GID and UID of the system running the docker or else php will have permission problems
RUN addgroup -g ${GID} -S php
RUN adduser -G php -S -D -s /bin/sh -u ${UID} php

# Copying all the files over
COPY --chown=php:php / /var/www/webpagetest

# Copying config files over
COPY docker/local/server/config/connectivity.ini /var/www/webpagetest/www/settings/connectivity.ini
COPY docker/local/server/config/mobile_devices.ini /var/www/webpagetest/www/settings/mobile_devices.ini
COPY docker/local/server/config/video.ini /var/www/webpagetest/www/settings/video.ini

COPY docker/local/server/scripts/ /scripts/

RUN chmod 755 /scripts/* \
    && chown -R php.php /run \
    && mkdir -p /var/www/webpagetest/www/results \
    && mkdir -p /var/www/webpagetest/www/tmp \
    && chown -R php.php /var/www/webpagetest/www/results \
    && mkdir -p /var/www/webpagetest/www/work/video \
    && chown -R php.php /var/www/webpagetest/www/work/video \
    && chown -R php.php /var/www/webpagetest/www/settings/ \
    && crontab /etc/crontab

# Might hang at gcc just delete current docker files and restart
# INSTALLS XDEBUG
RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS \
    && pecl install xdebug-3.0.0 \
    && docker-php-ext-enable xdebug \
    && apk del -f .build-deps

# Might need to uncomment this if vendor/autoload.php has a problem loading
# RUN curl -s https://getcomposer.org/installer | php
# RUN mv composer.phar /usr/local/bin/composer
# RUN composer install --working-dir=/var/www/webpagetest/

USER php
WORKDIR /var/www/webpagetest/www
EXPOSE 9000
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
