FROM php:8.1-apache

ENV COMPOSER_MEMORY_LIMIT -1
ENV APACHE_DOCUMENT_ROOT /var/www/html/contao/public
ENV APACHE_SERVER_NAME localhost
ENV PATH="$PATH"
WORKDIR /var/www/html

RUN apt-get update

RUN apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libicu-dev \
    && docker-php-ext-install -j$(nproc) iconv \
    && docker-php-ext-configure gd --with-jpeg=/usr/include/ --with-freetype=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install -j$(nproc) intl \
    && docker-php-ext-install -j$(nproc) pdo_mysql

RUN apt-get install -y git zip

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug \
    && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.client_host = host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo 'xdebug.discover_client_host=1' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo 'xdebug.log=/var/www/var/log/xdebug.log' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo 'xdebug.log_level=0' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

COPY .docker/contao/install_composer.sh /install_composer.sh

RUN chmod +x /install_composer.sh \
	&& cd / \
    && /install_composer.sh \
    && rm /install_composer.sh \
    && mv /composer.phar /usr/local/bin/composer

RUN a2enmod rewrite && a2enmod ssl && a2enmod socache_shmcb
#RUN a2enmod rewrite && a2enmod ssl
#COPY ./.docker/apache/httpd.conf /etc/apache2/sites-available/httpd.conf
#COPY ./.docker/apache/httpd.conf /etc/apache2/conf-available/httpd.conf

COPY ./cert/mycert.crt /etc/ssl/certs/mycert.crt
COPY ./cert/mycert.key /etc/ssl/private/mycert.key

RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
#RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/httpd.conf
#RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/httpd.conf
#RUN sed -i '/SSLCertificateFile.*snakeoil\.pem/c\SSLCertificateFile \/etc\/ssl\/certs\/mycert.crt' /etc/apache2/sites-available/*.conf
#RUN sed -i '/SSLCertificateKeyFile.*snakeoil\.key/cSSLCertificateKeyFile /etc/ssl/private/mycert.key\' /etc/apache2/sites-available/*.conf
#RUN sed -i '/SSLCertificateFile.*snakeoil\.pem/c\SSLCertificateFile \/etc\/ssl\/certs\/mycert.crt' /etc/apache2/sites-enabled/*.conf
#RUN sed -i '/SSLCertificateKeyFile.*snakeoil\.key/cSSLCertificateKeyFile /etc/ssl/private/mycert.key\' /etc/apache2/sites-enabled/*.conf

RUN sed -i '/SSLCertificateFile.*snakeoil\.pem/c\SSLCertificateFile /etc/ssl/certs/mycert.crt' /etc/apache2/sites-available/default-ssl.conf
RUN sed -i '/SSLCertificateKeyFile.*snakeoil\.key/cSSLCertificateKeyFile /etc/ssl/private/mycert.key\' /etc/apache2/sites-available/default-ssl.conf

RUN a2ensite default-ssl
RUN apt-get update && apt-get upgrade -y
#RUN a2ensite httpd

RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

COPY app/contao/composer.json /var/www/html/contao/composer.json

COPY .docker/contao/init.sh /init.sh

RUN chown -R www-data:www-data /usr/local/bin/composer
RUN chown -R www-data:www-data /var/www/html/

RUN chmod 755 /init.sh
RUN chmod +x /init.sh


RUN rm -rf /var/www/html/contao/var/cache/*
EXPOSE 80
EXPOSE 443

#USER www-data

ENTRYPOINT ["/init.sh"]
CMD ["apache2-foreground"]