# SuiteCRM 7.15.1 - PHP 8.1 + Apache
FROM php:8.1-apache

LABEL maintainer="Kevin Gessner"
LABEL description="SuiteCRM 7.15.1 containerized with PHP 8.1 and Apache"

# Install system dependencies and PHP extensions
RUN set -eux; \
    apt-get update && apt-get install -y --no-install-recommends \
        # Core
        libzip-dev \
        libpng-dev \
        libjpeg-dev \
        libfreetype6-dev \
        libonig-dev \
        libxml2-dev \
        libldap2-dev \
        libc-client-dev \
        libkrb5-dev \
        libcurl4-openssl-dev \
        libicu-dev \
        # Utils
        unzip \
        wget \
        curl \
        cron \
        msmtp \
        # Cleanup
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-configure imap --with-kerberos --with-imap-ssl \
    && docker-php-ext-install -j$(nproc) \
        pdo \
        pdo_mysql \
        mysqli \
        gd \
        mbstring \
        zip \
        xml \
        curl \
        ldap \
        imap \
        intl \
        calendar \
        opcache \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Configure PHP for SuiteCRM
RUN { \
        echo 'memory_limit = 512M'; \
        echo 'upload_max_filesize = 64M'; \
        echo 'post_max_size = 64M'; \
        echo 'max_execution_time = 600'; \
        echo 'max_input_time = 600'; \
        echo 'display_errors = Off'; \
        echo 'log_errors = On'; \
        echo 'date.timezone = Europe/Berlin'; \
    } > /usr/local/etc/php/conf.d/suitecrm.ini

# Configure OPcache
RUN { \
        echo 'opcache.memory_consumption=256'; \
        echo 'opcache.interned_strings_buffer=16'; \
        echo 'opcache.max_accelerated_files=20000'; \
        echo 'opcache.revalidate_freq=2'; \
        echo 'opcache.fast_shutdown=1'; \
    } > /usr/local/etc/php/conf.d/opcache-recommended.ini

# SuiteCRM version
ENV SUITECRM_VERSION=7.15.1
ENV SUITECRM_SHA256=468b811addd21dfb29d411ee6e815dbdf7099f912347e88cd3e8d010d829db7a

# Download and extract SuiteCRM
RUN set -eux; \
    wget -q "https://github.com/salesagility/SuiteCRM/releases/download/v${SUITECRM_VERSION}/SuiteCRM-${SUITECRM_VERSION}.zip" \
        -O /tmp/suitecrm.zip; \
    echo "${SUITECRM_SHA256}  /tmp/suitecrm.zip" | sha256sum -c -; \
    unzip -q /tmp/suitecrm.zip -d /var/www/html/; \
    rm /tmp/suitecrm.zip; \
    chown -R www-data:www-data /var/www/html

# Apache configuration
RUN a2enmod rewrite expires headers

COPY apache-suitecrm.conf /etc/apache2/sites-available/000-default.conf
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

WORKDIR /var/www/html

VOLUME ["/var/www/html/upload", "/var/www/html/custom", "/var/www/html/config_override.php"]

EXPOSE 80

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