FROM php:8.5-apache

RUN apt update && \
    apt upgrade -y && \
    apt install -y \
    libzip-dev \
    unzip \
    libonig-dev \
    libxml2-dev \
    libpng-dev \
    libjpeg-dev \
    libicu-dev \
    libpq-dev \
    libsqlite3-dev \
    sqlite3 \
    curl \
    git \
    cron \
    nano


RUN docker-php-ext-configure gd --with-jpeg
RUN docker-php-ext-configure zip

RUN docker-php-ext-install \
    zip \
    mbstring \
    exif \
    pcntl \
    bcmath \
    xml \
    intl \
    pdo_mysql \
    pdo_pgsql \
    pdo_sqlite

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
    php composer-setup.php --install-dir=/usr/local/bin --filename=composer && \
    php -r "unlink('composer-setup.php');"

RUN curl -sS https://get.symfony.com/cli/installer | bash && \
    mv /root/.symfony5/bin/symfony /usr/local/bin/symfony

ARG CACHEBURST=1
ARG BRANCH=master

WORKDIR /var/www/html
RUN git clone -b ${BRANCH} --single-branch https://gitea.rkprather.com/ryan/sermon-notes.git ./
RUN git config --global --add safe.directory /var/www/html

RUN mv 000-default.conf /etc/apache2/sites-available/

RUN echo "20 1 * * 6 root cd /var/www/html && /usr/local/bin/php bin/console app:get-audio > /proc/1/fd/1 2>&1" > /etc/cron.d/get-audio
RUN chmod 644 /etc/cron.d/get-audio
RUN crontab /etc/cron.d/get-audio

RUN COMPOSER_ALLOW_SUPERUSER=1 composer install --no-scripts --no-dev --optimize-autoloader
RUN mkdir /data

RUN mkdir -p /var/www/html/var/cache/prod
RUN mkdir -p /var/www/html/var/log

RUN chown -R 33:33 /var/www/html /data
RUN find /var/www/html -type d -exec chmod 755 '{}' \;
RUN find /var/www/html -type f -exec chmod 644 '{}' \;
RUN chmod 755 /data
RUN a2enmod rewrite setenvif headers

COPY prod.env /var/www/html/.env
RUN /usr/local/bin/php bin/console importmap:install
RUN /usr/local/bin/php bin/console asset-map:compile
RUN rm /var/www/html/.env
RUN chmod +x /var/www/html/bin/entrypoint.sh

EXPOSE 80

ENTRYPOINT ["/var/www/html/bin/entrypoint.sh"]
CMD ["apache2-foreground"]
