# ============================
# WissKI Distillery Dockerfile
# ============================

# This file is part of the WissKI Distillery and sets up an image
# to be used for individual WissKIs.


ARG PHP_VERSION=8.3-apache-trixie
FROM docker.io/library/php:$PHP_VERSION

# Setup in /var/www
WORKDIR /var/www

# install and enable the various required php extensions and dropbear ssh server
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
        curl \
        default-mysql-client \
        git \
        imagemagick \
        libavif-dev \
        libcurl4-openssl-dev \
        libfreetype6-dev \
        libicu-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libssh2-1-dev \
        libwebp-dev \
        libxml2-dev \
        libxpm-dev \
        libxslt-dev \
        libzip-dev \
        openssh-client \
        unzip \
        vim \
        zip \
        iipimage-server \
    && \
    docker-php-source extract && \
    mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" && \
    pear config-set php_ini "$PHP_INI_DIR/php.ini" && \
    docker-php-ext-configure gd \
        --enable-gd \
        --with-avif \
        --with-webp \
        --with-jpeg \
        --with-xpm \
        --with-freetype \
        --enable-gd-jis-conv \
    && \
    docker-php-ext-install \
        curl \
        gd \
        intl \
        mysqli \
        pdo_mysql \
        soap \
        xml \
        xmlwriter \
        xsl \
        zip \
    && \
    pecl install uploadprogress-2.0.2 && \
    pecl install xmlrpc-1.0.0RC3 && \
    pecl install ssh2-1.4.1 && \
    pecl install apcu-5.1.24 && \
    pecl install xdebug-3.4.3 && \
    docker-php-ext-enable \
        apcu \
        curl \
        gd \
        intl \
        mysqli \
        mysqli \
        pdo_mysql \
        soap \
        ssh2 \
        uploadprogress \
        xml \
        xmlrpc \
        xmlwriter \
        xsl \
        xdebug \
        zip \
    && \
    docker-php-source delete

# Install nodejs and hard-coded development dependencies
RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash - && \
    apt-get install -y nodejs && \
    npm install -g corepack cspell

# enable the apache rewrite mod
RUN a2enmod rewrite headers && \
    touch /var/log/xdebug.log && \
    chown www-data:www-data /var/log/xdebug.log && \
    chmod 777 /var/log/xdebug.log

# Install composer.
ARG COMPOSER_VERSION=2.8.9
RUN curl -sS https://getcomposer.org/installer | php -- --version=$COMPOSER_VERSION && \
    mv composer.phar /usr/local/bin/composer

# Add it to the path
ENV PATH "/usr/local/bin:/var/www/html/vendor/bin:$PATH"

# Configure PHP
ADD php.ini.d/zzz-custom.ini /usr/local/etc/php/conf.d/zzz_custom.ini

# first remove the default configuration
RUN rm /etc/apache2/sites-available/*.conf && \
    rm /etc/apache2/sites-enabled/*.conf

# Then add all the configs
ADD apache.d/conf/ports.conf /etc/apache2/ports.conf
ADD apache.d/sites-available/wisski.conf /etc/apache2/sites-available/wisski.conf

# this file is technically only needed for iipserv is enabled
# but we add it in either case, as it's not enabled.
ADD apache.d/mods-available/iipsrv.conf /etc/apache2/mods-available/iipsrv.conf

# enable it
RUN a2ensite wisski

# Create the location where WissKI will be mounted into.
RUN mkdir -p /var/www/html/web/modules/custom/wisski/ && \
    chown -R www-data:www-data /var/www/

# Allow www-data to use bash
RUN chsh -s /bin/bash www-data

ADD lifecycle /lifecycle/