FROM php:8.1-apache

# Install system dependencies
RUN apt-get update && apt-get install -y \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libonig-dev \
    && rm -rf /var/lib/apt/lists/*

# Install PHP extensions needed by videoDB
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) \
        gd \
        mysqli \
        exif \
        mbstring

# Enable Apache mod_rewrite for .htaccess
RUN a2enmod rewrite

# Allow .htaccess overrides in document root
RUN sed -i '/<Directory \/var\/www\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf

# Set working directory to document root
WORKDIR /var/www/html

# Copy application (including vendor which is committed to the repo)
COPY . .

# Create cache directories and set permissions
RUN mkdir -p \
        cache/smarty \
        cache/imdb \
        cache/img \
        cache/thumbs \
        cache/javascript \
        cache/local \
    && chown -R www-data:www-data \
        cache \
        images

# Copy entrypoint and db-init scripts
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
COPY init-db.php /usr/local/bin/init-db.php
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

EXPOSE 80

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["apache2-foreground"]
