FROM php:8.1-fpm-bullseye

ENV COMPOSER_MEMORY_LIMIT=-1
ARG DEBIAN_FRONTEND=noninteractive
WORKDIR /var/www/

# Get Composer binary
COPY --from=composer:2.4.4 /usr/bin/composer /usr/bin/composer

# Install package dependencies
RUN apt-get update \
  && apt-get upgrade -y \
#  && apt-get install -y --no-install-recommends apt-utils \
  && apt-get install -y --no-install-recommends \
## Standard
      locales \
      locales-all \
      git \
      gosu \
      zip \
      unzip \
      libzip-dev \
      libcurl4-openssl-dev \
## Image Optimization
      optipng \
      pngquant \
      jpegoptim \
      gifsicle \
## Image Processing
      libjpeg62-turbo-dev \
      libpng-dev \
      libmagickwand-dev \
# Required for GD
      libxpm4 \
      libxpm-dev \
      libwebp6 \
      libwebp-dev \
## Video Processing
      ffmpeg \
## Database
#      libpq-dev \
#      libsqlite3-dev \
      mariadb-client \
# Locales Update
  && sed -i '/en_US/s/^#//g' /etc/locale.gen \
  && locale-gen \
  && update-locale \
# Install PHP extensions
  && docker-php-source extract \
#PHP Imagemagick extensions
  && pecl install imagick \
  && docker-php-ext-enable imagick \
# PHP GD extensions
  && docker-php-ext-configure gd \
      --with-freetype \
      --with-jpeg \
      --with-webp \
      --with-xpm \
  && docker-php-ext-install -j$(nproc) gd \
#PHP Redis extensions
  && pecl install redis \
  && docker-php-ext-enable redis \
#PHP Database extensions
  && docker-php-ext-install pdo_mysql \
#pdo_pgsql pdo_sqlite \
#PHP extensions (dependencies)
  && docker-php-ext-configure intl \
  && docker-php-ext-install -j$(nproc) intl bcmath zip pcntl exif curl \
#Cleanup
  && docker-php-source delete \
  && apt-get autoremove --purge -y \
  && apt-get clean \
  && rm -rf /var/cache/apt \
  && rm -rf /var/lib/apt/lists/

# Use the default production configuration
COPY contrib/docker/php.production.ini "$PHP_INI_DIR/php.ini"

COPY . /var/www/
# for detail why storage is copied this way, pls refer to https://github.com/pixelfed/pixelfed/pull/2137#discussion_r434468862
RUN cp -r storage storage.skel \
  && composer install --prefer-dist --no-interaction --no-ansi --optimize-autoloader \
  && rm -rf html && ln -s public html \
  && chown -R www-data:www-data /var/www

RUN php artisan horizon:publish

VOLUME /var/www/storage /var/www/bootstrap

CMD ["/var/www/contrib/docker/start.fpm.sh"]