FROM php:8.3.37-apache

# Install PHP dependencies required by this legacy application
RUN apt-get update && apt-get install -y --no-install-recommends --fix-missing \
    ca-certificates \
    curl \
    git \
    imagemagick \
    libfreetype6-dev \
    libicu-dev \
    libjpeg-dev \
    libmagickwand-dev \
    libmcrypt-dev \
    libonig-dev \
    libpng-dev \
    libpq-dev \
    libxml2-dev \
    libzip-dev \
    unzip \
    zip \
    && rm -rf /var/lib/apt/lists/*

RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j"$(nproc)" \
        mysqli \
        pdo_mysql \
        mbstring \
        gd \
        zip \
        exif \
        intl \
        soap \
        opcache \
    && printf "\n" | pecl install imagick \
    && printf "\n" | pecl install mcrypt \
    && docker-php-ext-enable imagick mcrypt \
    && rm -rf /tmp/pear

# Enable Apache rewrite support
RUN a2enmod rewrite headers expires

# Disable the legacy source-tree protection in the local development container.
RUN mkdir -p /etc/metadmin && touch /etc/metadmin/tofile

# Runtime settings required by the legacy application.
COPY .docker/legacy.ini /usr/local/etc/php/conf.d/legacy.ini
RUN sed -i 's/\r$//' /usr/local/etc/php/conf.d/legacy.ini \
    && chmod 644 /usr/local/etc/php/conf.d/legacy.ini

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

WORKDIR /var/www/html
COPY . /var/www/html
RUN rm -rf /var/www/html/.docker

# Install PHP dependencies declared by the project
RUN composer install --no-interaction --no-progress --prefer-dist --no-scripts || true

# Harden ownership/permissions: source code owned by root:root (read-only for www-data),
# while only runtime upload/cache/log paths are writable by www-data.
COPY .docker/vendor.htaccess /etc/apache2/vendor.htaccess
RUN sed -i 's/\r$//' /etc/apache2/vendor.htaccess \
    && chmod 644 /etc/apache2/vendor.htaccess \
    && mkdir -p \
        /var/www/html/cache \
        /var/www/html/arquivos/apptable \
        /var/www/html/destino \
        /var/www/html/images/upload \
        /var/www/html/images/thumb \
        /var/www/html/upload \
        /var/www/html/tmp \
        /var/www/html/vendor/mpdf/mpdf/tmp/mpdf \
    && touch /var/www/html/metadminlog.log \
    && cp /etc/apache2/vendor.htaccess /var/www/html/vendor/.htaccess \
    && cp /etc/apache2/vendor.htaccess /var/www/html/vendor/mpdf/mpdf/tmp/.htaccess \
    && chown -R root:root /var/www/html \
    && find /var/www/html -type d -exec chmod 755 {} + \
    && find /var/www/html -type f -exec chmod 644 {} + \
    && if [ ! -e /var/www/html/.htaccess ] && [ ! -L /var/www/html/.htaccess ]; then \
        ln -s /var/www/html/arquivos/.htaccess /var/www/html/.htaccess; \
    fi \
    && chown -h root:root /var/www/html/.htaccess \
    && chown -R www-data:www-data \
        /var/www/html/cache \
        /var/www/html/arquivos \
        /var/www/html/destino \
        /var/www/html/images/upload \
        /var/www/html/images/thumb \
        /var/www/html/upload \
        /var/www/html/tmp \
        /var/www/html/vendor/mpdf/mpdf/tmp \
        /var/www/html/metadminlog.log \
    && chmod -R u+rwX,go+rX,go-w \
        /var/www/html/cache \
        /var/www/html/arquivos \
        /var/www/html/destino \
        /var/www/html/images/upload \
        /var/www/html/images/thumb \
        /var/www/html/upload \
        /var/www/html/tmp \
        /var/www/html/vendor/mpdf/mpdf/tmp \
    && chown root:root /var/www/html/vendor/.htaccess /var/www/html/vendor/mpdf/mpdf/tmp/.htaccess \
    && chmod 644 /var/www/html/vendor/.htaccess /var/www/html/vendor/mpdf/mpdf/tmp/.htaccess \
    && chmod 644 /var/www/html/metadminlog.log

# Validation & entrypoint scripts executed on every container start and restart
COPY .docker/validate-container.sh /usr/local/bin/validate-container.sh
COPY .docker/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN sed -i 's/\r$//' /usr/local/bin/validate-container.sh /usr/local/bin/docker-entrypoint.sh \
    && chmod 755 /usr/local/bin/validate-container.sh /usr/local/bin/docker-entrypoint.sh \
    && head -n 1 /usr/local/bin/validate-container.sh | grep -qx '#!/bin/bash' \
    && head -n 1 /usr/local/bin/docker-entrypoint.sh | grep -qx '#!/bin/bash' \
    && bash -n /usr/local/bin/validate-container.sh \
    && bash -n /usr/local/bin/docker-entrypoint.sh \
    && htpasswd -bcB /etc/apache2/.docs_htpasswd docs agrichem_docs_2026 >/dev/null 2>&1 \
    && chown root:www-data /etc/apache2/.docs_htpasswd \
    && chmod 640 /etc/apache2/.docs_htpasswd

EXPOSE 80

ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["apache2-foreground"]
