ARG DEBIAN_RELEASE=bookworm
FROM debian:${DEBIAN_RELEASE}-slim AS builder

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update \
	&& apt-get dist-upgrade -y \
	&& apt-get -y install --no-install-recommends \
	sudo \
	apache2 \
    libapache2-mod-php \
    python3 && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Log to stdout / stderr
RUN ln -sf /proc/self/fd/1 /var/log/apache2/access.log && \
ln -sf /proc/self/fd/1 /var/log/apache2/error.log

RUN a2enmod rewrite 

FROM builder
    # Remove pre-existing files in webroot
    RUN rm /var/www/html/*
    
    # Use same paths as on RevPi in order to make pictory_configcheck work
    RUN mkdir /var/www/revpi
    COPY --chown=www-data:www-data ./src/pictory /var/www/html/pictory
    COPY ./usr/bin/pictory_configcheck /usr/bin/

    # Link pictory folder to webserver root
    RUN ln -s /var/www/revpi/pictory /var/www/html/pictory
    
    # Copy session helper to webroot
    COPY docker/session-helper.php /var/www/html/index.php
    # Make exit link work
    COPY docker/redirect.html /var/www/html/index.html
    
    # Start webserver
    EXPOSE 80
    CMD ["apachectl", "-D", "FOREGROUND"]

