FROM php:8.1-apache

RUN apt-get update && apt-get install -y \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libzip-dev \
    unzip \
    tar \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install mysqli pdo pdo_mysql gd zip \
    && a2enmod rewrite headers \
    && rm -rf /var/lib/apt/lists/*

COPY . /var/www/html/

RUN if [ -f /var/www/html/app.tar.gz ]; then \
      tar -xzf /var/www/html/app.tar.gz -C /var/www/html && \
      rm -f /var/www/html/app.tar.gz; \
    fi

# Corrige caminhos antigos do Scriptcase para rodar no container
RUN find /var/www/html -type f -name "*.php" -exec sed -i \
    -e 's#https://producao.aplicativopro.com/producao/prod#/_lib/prod#g' \
    -e 's#/producao.aplicativopro.com/producao/prod#/_lib/prod#g' \
    -e 's#/producao/prod#/_lib/prod#g' \
    -e 's#https://producao.aplicativopro.com/producao/tmp#/_lib/tmp#g' \
    -e 's#/producao.aplicativopro.com/producao/tmp#/_lib/tmp#g' \
    -e 's#/producao/tmp#/_lib/tmp#g' \
    {} \;

RUN mkdir -p /var/www/html/_lib/tmp \
    && chown -R www-data:www-data /var/www/html \
    && find /var/www/html -type d -exec chmod 755 {} \; \
    && find /var/www/html -type f -exec chmod 644 {} \; \
    && chmod -R 775 /var/www/html/_lib/tmp

EXPOSE 80