FROM php:8.2-apache

# System dependencies for the PHP extensions phpBB needs at runtime.
# The base php:8.2-apache image already ships: json, mbstring, curl, PDO,
# pdo_sqlite and sqlite3 (phpBB's sqlite3 DB driver uses the SQLite3 class).
RUN apt-get update && apt-get install -y --no-install-recommends \
        libpng-dev libfreetype6-dev libjpeg62-turbo-dev libicu-dev libzip-dev \
        unzip \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install gd intl zip \
    && a2enmod rewrite \
    && rm -rf /var/lib/apt/lists/*

# Composer
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

# phpBB source (build context = the phpBB/ directory of a checkout)
COPY . /var/www/html/
WORKDIR /var/www/html

# Install PHP dependencies (production, no dev tools)
RUN composer install --no-dev --no-scripts --optimize-autoloader --no-interaction

# Apache: enable .htaccess (AllowOverride All) for the front-controller rewrite
COPY apache-site.conf /etc/apache2/sites-available/000-phpbb.conf
RUN a2dissite 000-default.conf || true \
    && a2ensite 000-phpbb.conf

# Persistent writable data directory for the SQLite database
RUN mkdir -p /var/www/phpbb_data && chown -R www-data:www-data /var/www/phpbb_data

# phpBB writable directories (created here; re-chowned after the installer below
# because the installer itself creates cache/production/ as root)
RUN mkdir -p /var/www/html/cache /var/www/html/store /var/www/html/files /var/www/html/images/avatars/upload

# CLI installer configuration (SQLite backend + admin user)
COPY install-config.yml /tmp/install-config.yml

# Run the CLI installer: creates config.php, the SQLite schema, and the admin account
RUN php install/phpbbcli.php install /tmp/install-config.yml

# phpBB refuses to serve the board while the install/ directory is present
RUN rm -rf /var/www/html/install

# Make every runtime-writable path (and config.php) owned by www-data so that
# Apache can write the SQLite DB, caches, file store and avatar uploads.
RUN chown www-data:www-data /var/www/html/config.php 2>/dev/null || true \
    && chown -R www-data:www-data \
        /var/www/phpbb_data \
        /var/www/html/cache \
        /var/www/html/store \
        /var/www/html/files \
        /var/www/html/images/avatars/upload \
        /var/www/html/assets

EXPOSE 80
