FROM ubuntu:noble

# Set Environment Variables
ENV DEBIAN_FRONTEND=noninteractive

# Mise à jour et installation de paquets de base
RUN apt-get update && \
    apt-get install -y \
    curl \
    wget \
    vim \
    git \
    ca-certificates \
    sudo \
    bash \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Installation des dépendances et du dépôt PHP Sury
RUN set -eux; \
    apt-get update; \
    apt-get upgrade -y; \
    apt-get install -y --no-install-recommends \
        curl libmemcached-dev libz-dev libpq-dev \
        libjpeg-dev libpng-dev libfreetype6-dev libssl-dev \
        libwebp-dev libxpm-dev libmcrypt-dev libonig-dev \
        libicu-dev libcurl4-openssl-dev openssh-client \
        git wget unzip zip libzip-dev ca-certificates \
        dnsutils iputils-ping vim lsb-release apt-transport-https \
        gnupg2 software-properties-common; \
    LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/php; \
    apt-get update;

# Installation de PHP 8.4, Nginx et extensions essentielles
RUN set -eux; \
    apt-get update; \
    apt-get install -y --no-install-recommends \
        php7.4-cli \
        php7.4-fpm \
        php7.4-common \
        php7.4-mysql \
        php7.4-pgsql \
        php7.4-sqlite3 \
        php7.4-zip \
        php7.4-gd \
        php7.4-mbstring \
        php7.4-curl \
        php7.4-xml \
        php7.4-bcmath \
        php7.4-intl \
        php7.4-redis \
        php7.4-memcached \
        php7.4-imagick \
        php7.4-opcache \
        php7.4-xdebug \
        php8.4-cli \
        php8.4-fpm \
        php8.4-common \
        php8.4-mysql \
        php8.4-pgsql \
        php8.4-sqlite3 \
        php8.4-zip \
        php8.4-gd \
        php8.4-mbstring \
        php8.4-curl \
        php8.4-xml \
        php8.4-bcmath \
        php8.4-intl \
        php8.4-redis \
        php8.4-memcached \
        php8.4-imagick \
        php8.4-opcache \
        php8.4-xdebug \
        nginx \
        supervisor \
        ghostscript \
        librsvg2-bin \
        wkhtmltopdf \
        clamav \
        clamav-daemon; \
    rm -rf /var/lib/apt/lists/*

# ClamAV : téléchargement des signatures antivirus (en build pour avoir une base utilisable au démarrage)
RUN (freshclam 2>/dev/null || true)

# Installation de xhprof pour PHP 8.4
# Note: PECL xhprof ne supporte pas encore PHP 8.4, on utilise le fork longxinH/xhprof
# Note: Si le build échoue avec des erreurs DNS, vérifiez votre connexion réseau ou utilisez --network=host
RUN set -eux; \
    apt-get update; \
    apt-get install -y --no-install-recommends \
        php8.4-dev \
        build-essential \
        pkg-config \
        autoconf \
        gcc \
        make \
        graphviz; \
    cd /tmp && \
    git clone https://github.com/longxinH/xhprof.git xhprof-source && \
    cd xhprof-source/extension && \
    phpize8.4 && \
    ./configure --with-php-config=php-config8.4 && \
    make && \
    make install && \
    echo "extension=xhprof.so" > /etc/php/8.4/mods-available/xhprof.ini && \
    echo "xhprof.output_dir=/tmp/xhprof" >> /etc/php/8.4/mods-available/xhprof.ini && \
    (phpenmod8.4 xhprof 2>/dev/null || ln -sf /etc/php/8.4/mods-available/xhprof.ini /etc/php/8.4/cli/conf.d/20-xhprof.ini && ln -sf /etc/php/8.4/mods-available/xhprof.ini /etc/php/8.4/fpm/conf.d/20-xhprof.ini) && \
    mkdir -p /tmp/xhprof && \
    chmod 777 /tmp/xhprof && \
    cd / && \
    rm -rf /tmp/xhprof-source && \
    apt-get purge -y --auto-remove php8.4-dev build-essential pkg-config autoconf gcc make; \
    rm -rf /var/lib/apt/lists/*

# Installation de Composer
RUN curl -sS https://getcomposer.org/installer | php8.4 -- --install-dir=/usr/local/bin --filename=composer

# Crée un utilisateur non-root
RUN useradd -ms /bin/bash dev && \
    echo "dev ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

# Permet d'utiliser bash au lieu de sh par défaut dans docker desktop
RUN ln -sf /bin/bash /bin/sh

# Set working directory
WORKDIR /var/www/html

COPY certs /usr/local/share/ca-certificates/
RUN update-ca-certificates

# Copier la configuration supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Copier le script d'entrée
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Créer les répertoires de logs et le socket ClamAV (clamd ne tourne pas sous systemd)
RUN mkdir -p /var/log/supervisor /var/run/clamav && chown clamav:clamav /var/run/clamav

# Exposer le port 80
EXPOSE 80

# Utiliser le script d'entrée
ENTRYPOINT ["/entrypoint.sh"]
