# Arguments
ARG centos_stream_version=9
ARG extra_repository=crb
ARG mapnik_version=4.0.0

# Mapnik Builder
FROM quay.io/centos/centos:stream${centos_stream_version} as mapnik-builder

## Arguments
ARG centos_stream_version
ARG extra_repository
ARG mapnik_version

## Install mapnik-builder dependencies
RUN --mount=id=centos:stream${centos_stream_version}-/var/cache/dnf,target=/var/cache/dnf,type=cache,sharing=locked \
    echo "install_weak_deps=False" >> /etc/dnf/dnf.conf && \
    echo "keepcache=True" >> /etc/dnf/dnf.conf && \
    dnf --assumeyes install "dnf-command(config-manager)" && \
    dnf config-manager --save --setopt=${extra_repository}.enabled=1 && \
    dnf --assumeyes install epel-release && \
    dnf --assumeyes upgrade && \
    dnf --assumeyes install \
        boost-devel \
        cairo-devel \
        cmake \
        freetype-devel \
        gcc \
        gcc-c++ \
        gdal-devel \
        git \
        harfbuzz-devel \
        libicu-devel \
        libjpeg-devel \
        libpng-devel \
        libtiff-devel \
        libwebp-devel \
        libxml2-devel \
        make \
        postgresql-devel \
        proj-devel \
        sqlite-devel \
        zlib-devel

## Download, Build & Install `Mapnik`
WORKDIR /tmp/mapnik_src
RUN --mount=id=centos:stream${centos_stream_version}-mapnik-src:${mapnik_version},target=/tmp/mapnik_src,type=cache \
    if [ ! -f CMakeLists.txt ]; then \
        git clone --branch v${mapnik_version} --depth 1 --jobs 8 --recurse-submodules https://github.com/mapnik/mapnik.git /tmp/mapnik_src; \
    fi
WORKDIR /tmp/mapnik_build
RUN --mount=id=centos:stream${centos_stream_version}-mapnik-src:${mapnik_version},target=/tmp/mapnik_src,type=cache \
    --mount=id=centos:stream${centos_stream_version}-mapnik-build:${mapnik_version},target=/tmp/mapnik_build,type=cache \
    export CMAKE_BUILD_PARALLEL_LEVEL=$(nproc) && \
    export DESTDIR=/tmp/mapnik && \
    cmake -B . -S /tmp/mapnik_src \
        -DBUILD_BENCHMARK:BOOL=OFF \
        -DBUILD_DEMO_CPP:BOOL=OFF \
        -DBUILD_DEMO_VIEWER:BOOL=OFF \
        -DBUILD_TESTING:BOOL=OFF \
        -DBUILD_UTILITY_GEOMETRY_TO_WKB:BOOL=OFF \
        -DBUILD_UTILITY_MAPNIK_INDEX:BOOL=OFF \
        -DBUILD_UTILITY_MAPNIK_RENDER:BOOL=OFF \
        -DBUILD_UTILITY_OGRINDEX:BOOL=OFF \
        -DBUILD_UTILITY_PGSQL2SQLITE:BOOL=OFF \
        -DBUILD_UTILITY_SHAPEINDEX:BOOL=OFF \
        -DBUILD_UTILITY_SVG2PNG:BOOL=OFF \
        -DCMAKE_BUILD_TYPE:STRING=Release \
        -DCMAKE_INSTALL_PREFIX:PATH=/usr && \
    cmake --build . && \
    cmake --install . --strip

# Builder
FROM quay.io/centos/centos:stream${centos_stream_version} as builder

## Arguments
ARG centos_stream_version
ARG extra_repository

## Install builder dependencies
RUN --mount=id=centos:stream${centos_stream_version}-/var/cache/dnf,target=/var/cache/dnf,type=cache,sharing=locked \
    echo "install_weak_deps=False" >> /etc/dnf/dnf.conf && \
    echo "keepcache=True" >> /etc/dnf/dnf.conf && \
    dnf --assumeyes install "dnf-command(config-manager)" && \
    dnf config-manager --save --setopt=${extra_repository}.enabled=1 && \
    dnf --assumeyes install epel-release && \
    dnf --assumeyes upgrade && \
    dnf --assumeyes install \
        boost-devel \
        cairo-devel \
        cmake \
        freetype-devel \
        gcc \
        gcc-c++ \
        gdal \
        glib2-devel \
        harfbuzz-devel \
        httpd-devel \
        iniparser-devel \
        libcurl-devel \
        libicu-devel \
        libjpeg-devel \
        libmemcached-devel \
        libpng-devel \
        librados2-devel \
        libtiff-devel \
        libwebp-devel \
        libxml2-devel \
        make \
        pkg-config \
        procps \
        proj-devel

## Copy files from builder(s)
### Mapnik
COPY --from=mapnik-builder /tmp/mapnik /

## Build, Test & Install `mod_tile`
COPY . /tmp/mod_tile_src
WORKDIR /tmp/mod_tile_build
RUN export CMAKE_BUILD_PARALLEL_LEVEL=$(nproc) && \
    cmake -B . -S /tmp/mod_tile_src \
        -DCMAKE_BUILD_TYPE:STRING=Release \
        -DCMAKE_INSTALL_LOCALSTATEDIR:PATH=/var \
        -DCMAKE_INSTALL_PREFIX:PATH=/usr \
        -DCMAKE_INSTALL_RUNSTATEDIR:PATH=/run \
        -DCMAKE_INSTALL_SYSCONFDIR:PATH=/etc \
        -DENABLE_TESTS:BOOL=ON && \
    cmake --build .
RUN export CTEST_PARALLEL_LEVEL=$(nproc) && \
    ctest --output-on-failure
RUN export DESTDIR=/tmp/mod_tile && \
    cmake --install . --strip

# Runner
FROM quay.io/centos/centos:stream${centos_stream_version} as runner

## Arguments
ARG centos_stream_version
ARG extra_repository

## Install runner dependencies
RUN --mount=id=centos:stream${centos_stream_version}-/var/cache/dnf,target=/var/cache/dnf,type=cache,sharing=locked \
    echo "install_weak_deps=False" >> /etc/dnf/dnf.conf && \
    echo "keepcache=True" >> /etc/dnf/dnf.conf && \
    dnf --assumeyes install "dnf-command(config-manager)" && \
    dnf config-manager --save --setopt=${extra_repository}.enabled=1 && \
    dnf --assumeyes install epel-release && \
    dnf --assumeyes upgrade && \
    dnf --assumeyes install \
        boost-regex \
        cairo \
        gdal \
        harfbuzz \
        httpd \
        iniparser \
        libicu \
        libmemcached \
        librados2 \
        libtiff \
        libwebp \
        proj

## Copy files from builder(s)
### Mapnik
COPY --from=mapnik-builder /tmp/mapnik /
### mod_tile
COPY --from=builder /tmp/mod_tile /
COPY --chown=apache:apache --from=builder \
    /tmp/mod_tile_src/utils/example-map \
    /usr/share/renderd/example-map
COPY --from=builder \
    /tmp/mod_tile_src/etc/apache2/renderd-example-map.conf \
    /etc/httpd/conf.d/renderd-example-map.conf

## Add configuration
RUN printf '\n[example-map]\nMAXZOOM=20\nMINZOOM=0\nURI=/tiles/renderd-example\nXML=/usr/share/renderd/example-map/mapnik.xml\n' >> /etc/renderd.conf
RUN printf '\n[example-map-jpg]\nMAXZOOM=20\nMINZOOM=0\nTYPE=jpg image/jpeg jpeg\nURI=/tiles/renderd-example-jpg\nXML=/usr/share/renderd/example-map/mapnik.xml\n' >> /etc/renderd.conf
RUN printf '\n[example-map-png256]\nMAXZOOM=20\nMINZOOM=0\nTYPE=png image/png png256\nURI=/tiles/renderd-example-png256\nXML=/usr/share/renderd/example-map/mapnik.xml\n' >> /etc/renderd.conf
RUN printf '\n[example-map-png32]\nMAXZOOM=20\nMINZOOM=0\nTYPE=png image/png png32\nURI=/tiles/renderd-example-png32\nXML=/usr/share/renderd/example-map/mapnik.xml\n' >> /etc/renderd.conf
RUN printf '\n[example-map-webp]\nMAXZOOM=20\nMINZOOM=0\nTYPE=webp image/webp webp\nURI=/tiles/renderd-example-webp\nXML=/usr/share/renderd/example-map/mapnik.xml\n' >> /etc/renderd.conf

## Start services
CMD httpd -e debug -k start; \
    G_MESSAGES_DEBUG=${G_MESSAGES_DEBUG:-info} renderd --foreground
