FROM debian:bookworm-slim AS assets

RUN apt-get update && \
    apt-get install -y --no-install-recommends curl ca-certificates && \
    rm -rf /var/lib/apt/lists/* && \
    find /var/log -type f -name '*.log*' -delete

RUN mkdir -p /var/www/html/cgit-css && \
    curl -sSL -o /var/www/html/favicon.ico https://git-scm.com/favicon.ico && \
    curl -sSL -o /var/www/html/cgit-css/highlight.min.js https://raw.githubusercontent.com/highlightjs/cdn-release/refs/heads/11-stable/build/highlight.min.js && \
    curl -sSL -o /var/www/html/cgit-css/github.min.css https://raw.githubusercontent.com/highlightjs/cdn-release/refs/heads/11-stable/build/styles/github.min.css && \
    curl -sSL -o /var/www/html/cgit-css/github-dark.min.css https://raw.githubusercontent.com/highlightjs/cdn-release/refs/heads/11-stable/build/styles/github-dark.min.css && \
    sed -i '1s/^/@media screen and (prefers-color-scheme: dark) {/' /var/www/html/cgit-css/github-dark.min.css && echo '}' >> /var/www/html/cgit-css/github-dark.min.css && \
    echo 'User-agent: *\nDisallow: /' >> /var/www/html/robots.txt

FROM debian:bookworm-slim

RUN apt-get update && \
    apt-get install -y --no-install-recommends apache2 cgit python3-markdown && \
    rm -rf /var/lib/apt/lists/* && \
    find /var/log -type f -name '*.log*' -delete

COPY --from=assets /var/www/html /var/www/html
COPY theme.css /var/www/html/cgit-css/
COPY head-include.html /var/www/html/cgit-css/
COPY md2html.py /usr/lib/cgit/filters/
RUN cp /usr/share/cgit/cgit.css /usr/share/cgit/cgit.png /var/www/html/cgit-css/

RUN echo 'Listen ${APACHE_PORT}' > /etc/apache2/ports.conf && \
    echo 'ServerName ${APACHE_SERVER_NAME}' >> /etc/apache2/apache2.conf && \
    sed -e '/^User /d' -e '/^Group /d' -e 's;^PidFile .*;PidFile ${APACHE_RUN_DIR}/apache.pid;gi' -e 's;^ErrorLog .*;ErrorLog /dev/stderr;gi' \
        -e 's;Require all granted;Require all denied;gi' -e 's;"^\\\\.ht";"^\\.";g' -i /etc/apache2/apache2.conf && \
    rm -f /etc/apache2/sites-enabled/* /etc/apache2/mods-enabled/* /etc/apache2/conf-enabled/* && \
    ln -s ../mods-available/mpm_event.load /etc/apache2/mods-enabled/mpm_event.load && \
    ln -s ../mods-available/mpm_event.conf /etc/apache2/mods-enabled/mpm_event.conf && \
    ln -s ../mods-available/mime.load /etc/apache2/mods-enabled/mime.load && \
    ln -s ../mods-available/alias.load /etc/apache2/mods-enabled/alias.load && \
    ln -s ../mods-available/filter.load /etc/apache2/mods-enabled/filter.load && \
    ln -s ../mods-available/deflate.load /etc/apache2/mods-enabled/deflate.load && \
    ln -s ../mods-available/deflate.conf /etc/apache2/mods-enabled/deflate.conf && \
    ln -s ../mods-available/headers.load /etc/apache2/mods-enabled/headers.load && \
    ln -s ../mods-available/authn_core.load /etc/apache2/mods-enabled/authn_core.load && \
    ln -s ../mods-available/authz_core.load /etc/apache2/mods-enabled/authz_core.load && \
    ln -s ../mods-available/authn_file.load /etc/apache2/mods-enabled/authn_file.load && \
    ln -s ../mods-available/authz_user.load /etc/apache2/mods-enabled/authz_user.load && \
    ln -s ../mods-available/auth_basic.load /etc/apache2/mods-enabled/auth_basic.load && \
    ln -s ../mods-available/cgid.load /etc/apache2/mods-enabled/cgid.load && \
    echo 'TypesConfig /etc/mime.types\nDeflateAlterETag NoChange' > /etc/apache2/mods-enabled/mime.conf && \
    echo 'ServerTokens Major\nServerSignature Off\nTraceEnable Off' > /etc/apache2/conf-enabled/security.conf && \
    echo 'CustomLog /dev/stdout combined' > /etc/apache2/conf-enabled/access-log.conf

COPY cgitrc /etc/
COPY apache.conf /etc/apache2/sites-enabled/

ENV APACHE_PORT=8080
ENV APACHE_RUN_DIR=/dev/shm
ENV APACHE_SERVER_NAME=cgit

ENTRYPOINT ["/sbin/apache2", "-D", "FOREGROUND"]