ARG DEVEL_DATE
ARG DEVEL_VERSION=true
ARG PYTHON_WHEEL_VERSION
ARG SFCGAL_REGISTRY

FROM ${SFCGAL_REGISTRY}:debian-latest as sfcgal_build

FROM python:${PYTHON_WHEEL_VERSION}-slim-bookworm AS container

ARG DEVEL_DATE
ARG DEVEL_VERSION
ARG PYTHON_WHEEL_VERSION

# copy sfcgal build
COPY --from=sfcgal_build /SFCGAL/build/ /SFCGAL/build/
RUN echo "/SFCGAL/build/src" > /etc/ld.so.conf.d/sfcgal.conf && \
    ldconfig

# install dependencies
RUN apt update && apt install --yes --no-install-recommends gcc libboost-serialization1.74.0 libgmpxx4ldbl libstdc++-12-dev patchelf

# copy files
COPY setup.py README.md ci/increment_version.py ./
COPY ./pysfcgal ./pysfcgal

# Increase the dev version number to upload the wheel to gitlab package registry
RUN if [ "$DEVEL_VERSION" = "true" ]; then python3 increment_version.py ${DEVEL_DATE}; fi

# generate the wheel
RUN python3 -m venv venv && \
    . venv/bin/activate
RUN python3 -m pip install -U pip build auditwheel twine
RUN CFLAGS=-I/SFCGAL/build/include LDFLAGS=-L/SFCGAL/build/src python3 -m build
RUN auditwheel repair dist/*.whl -w /dist_linux_${PYTHON_WHEEL_VERSION}/ --plat manylinux_2_35_x86_64

# check the generated wheel with twine
RUN twine check --strict /dist_linux_${PYTHON_WHEEL_VERSION}/*
