# Xibo Development Container
# This is the dev container and doesn't contain CRON or composer it maps the PWD straight through
# Stage 1
# Build the CMS container
FROM debian:bullseye-slim
LABEL org.opencontainers.image.authors="support@xibosignage.com"

RUN apt update && \
    apt install -y software-properties-common lsb-release ca-certificates curl && \
    rm -rf /var/lib/apt/lists/* && \
    ln -fs /usr/share/zoneinfo/Etc/UTC /etc/localtime

# Add sury.org PHP Repository
RUN curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg && \
    sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'

RUN LC_ALL=C.UTF-8 DEBIAN_FRONTEND=noninteractive apt update && apt upgrade -y && apt install -y \
    tar \
    bash \
    curl \
    apache2 \
    libapache2-mod-xsendfile \
    netcat \
    iputils-ping \
    gnupg \
    php8.4 \
    libapache2-mod-php8.4 \
    php8.4-gd \
    php8.4-dom \
    php8.4-pdo \
    php8.4-zip \
    php8.4-mysql \
    php8.4-gettext \
    php8.4-soap \
    php8.4-iconv \
    php8.4-curl \
    php8.4-ctype \
    php8.4-fileinfo \
    php8.4-xml \
    php8.4-simplexml \
    php8.4-mbstring \
    php8.4-memcached \
    php8.4-phar \
    php8.4-opcache \
    php8.4-mongodb \
    php8.4-gnupg \
    tzdata \
    msmtp \
    openssl \
    cron \
    default-mysql-client \
    && dpkg-reconfigure --frontend noninteractive tzdata \
    && rm -rf /var/lib/apt/lists/*

RUN update-alternatives --set php /usr/bin/php8.4

# Enable Apache module
RUN a2enmod rewrite \
    && a2enmod headers \
    && a2enmod proxy \
    && a2enmod proxy_http \
    && a2enmod proxy_wstunnel

# Add all necessary config files in one layer
ADD docker/ /

# Adjust file permissions as appropriate
RUN chmod +x /entrypoint.sh /usr/local/bin/httpd-foreground /usr/local/bin/wait-for-command.sh \
    /etc/periodic/15min/cms-db-backup && \
    chmod 777 /tmp && \
    chown -R www-data:www-data /var/www/.gnupg

# Update the PHP.ini file
RUN sed -i "s/error_reporting = .*$/error_reporting = E_ERROR | E_WARNING | E_PARSE/" /etc/php/8.4/apache2/php.ini && \
    sed -i "s/session.gc_probability = .*$/session.gc_probability = 1/" /etc/php/8.4/apache2/php.ini && \
    sed -i "s/session.gc_divisor = .*$/session.gc_divisor = 100/" /etc/php/8.4/apache2/php.ini && \
    sed -i "s/allow_url_fopen = .*$/allow_url_fopen = Off/" /etc/php/8.4/apache2/php.ini && \
    sed -i "s/expose_php = .*$/expose_php = Off/" /etc/php/8.4/apache2/php.ini && \
    sed -i "s/error_reporting = .*$/error_reporting = E_ERROR | E_WARNING | E_PARSE/" /etc/php/8.4/cli/php.ini && \
    sed -i "s/session.gc_probability = .*$/session.gc_probability = 1/" /etc/php/8.4/cli/php.ini && \
    sed -i "s/session.gc_divisor = .*$/session.gc_divisor = 100/" /etc/php/8.4/cli/php.ini && \
    sed -i "s/allow_url_fopen = .*$/allow_url_fopen = Off/" /etc/php/8.4/cli/php.ini && \
    sed -i "s/expose_php = .*$/expose_php = Off/" /etc/php/8.4/cli/php.ini

# Capture the git commit for this build if we provide one
ARG GIT_COMMIT=dev

# Set some environment variables
ENV CMS_DEV_MODE=true \
    INSTALL_TYPE=dev \
    XMR_HOST=xmr \
    MYSQL_HOST=db \
    MYSQL_PORT=3306 \
    MYSQL_USER=root \
    MYSQL_PASSWORD=root \
    MYSQL_DATABASE=cms \
    MYSQL_BACKUP_ENABLED=false \
    MYSQL_ATTR_SSL_CA=none \
    MYSQL_ATTR_SSL_VERIFY_SERVER_CERT=true \
    CMS_SERVER_NAME=localhost \
    CMS_ALIAS=none \
    CMS_PHP_SESSION_GC_MAXLIFETIME=1440 \
    CMS_PHP_POST_MAX_SIZE=2G \
    CMS_PHP_UPLOAD_MAX_FILESIZE=2G \
    CMS_PHP_MAX_EXECUTION_TIME=300 \
    CMS_PHP_MEMORY_LIMIT=256M \
    CMS_PHP_CLI_MAX_EXECUTION_TIME=0 \
    CMS_PHP_CLI_MEMORY_LIMIT=256M \
    CMS_PHP_COOKIE_SECURE=Off \
    CMS_PHP_COOKIE_HTTP_ONLY=On \
    CMS_PHP_COOKIE_SAMESITE=Lax \
    CMS_APACHE_START_SERVERS=2 \
    CMS_APACHE_MIN_SPARE_SERVERS=5 \
    CMS_APACHE_MAX_SPARE_SERVERS=10 \
    CMS_APACHE_MAX_REQUEST_WORKERS=60 \
    CMS_APACHE_MAX_CONNECTIONS_PER_CHILD=300 \
    CMS_APACHE_TIMEOUT=30 \
    CMS_APACHE_OPTIONS_INDEXES=false \
    CMS_QUICK_CHART_URL=http://quickchart:3400 \
    CMS_APACHE_SERVER_TOKENS=OS \
    CMS_APACHE_LOG_REQUEST_TIME=true \
    CMS_USE_MEMCACHED=true \
    MEMCACHED_HOST=memcached \
    MEMCACHED_PORT=11211 \
    CMS_USAGE_REPORT=true \
    XTR_ENABLED=false \
    GIT_COMMIT=$GIT_COMMIT \
    GNUPGHOME=/var/www/.gnupg

# Expose port 80
EXPOSE 80

# Map the source files into /var/www/cms
# Create library and cache, because they might not exist
# Create /var/www/backup so that we have somewhere for entrypoint to log errors.
RUN mkdir -p /var/www/cms && \
    mkdir -p /var/www/cms/library/temp && \
    mkdir -p /var/www/cms/cache && \
    mkdir -p /var/www/backup

# Run entry
CMD ["/entrypoint.sh"]
