FROM php:8.2-apache

LABEL Name="srm"
LABEL Version="v9.1.10"

ENV ADD_TESTING_DATA=true
ENV DEFAULT_CONNECTION=existing
ENV DEFAULT_CONNECTION_HOST=srm_db
ENV DEFAULT_CONNECTION_USER=srm_admin_user 
ENV DEFAULT_CONNECTION_PORT=3306

# 1. Install development packages and clean up apt cache.
RUN apt-get update && apt-get install -y \
    curl \
    nano \
    cron \
    git \
    g++ \
    libonig-dev \
    libbz2-dev \
    libfreetype6-dev \
    libicu-dev \
    libjpeg-dev \
    libmcrypt-dev \
    libreadline-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    libzip-dev \
    unzip \
    zip \
    && rm -rf /var/lib/apt/lists/*

# 2. Apache configs + document root.
RUN echo "ServerName srm.local" >> /etc/apache2/apache2.conf

ENV APACHE_DOCUMENT_ROOT=/var/www/html/dashboard/public

RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf \
    && sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf


# 3. Enable Apache modules.
RUN a2enmod rewrite headers

# 4. Start with base PHP config, then add extensions.
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

# 5. Install PHP dependencies.
RUN docker-php-ext-install \
    gd\
    bcmath \
    bz2 \
    calendar \
    iconv \
    intl \
    mbstring \
    opcache \
    pdo_mysql \
    zip

# 6. Copy all files inside container.
COPY . .

# 8. Set up crontab for srmuser.

COPY crontab /etc/cron.d/crontab
RUN crontab -u www-data /etc/cron.d/crontab \
    && touch /var/log/cron.log \ 
    && chmod 0644 /var/log/cron.log

# 9. Composer.
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
RUN chown www-data:www-data -R /var/www/html\
    && composer install --no-dev --no-scripts --prefer-dist


# 10. Add entrypoint script.
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh\
    && rm -rf notes.md install.md /var/www/html/Dockerfile-singlestage /var/www/html/Dockerfile-multistage crontab docker-entrypoint.sh docker-compose-*.yml

ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]