# Build command template
# podman build --build-arg groupid=<groupid> --build-arg userid=<userid> --build-arg groupname=<groupname> --no-cache -f path/to/Dockerfiles/murfey-instrument-server -t murfey-instrument-server:<version> path/to/python-murfey

# Set up the base image to build with
FROM docker.io/library/python:3.12-slim-bookworm AS base

# Install Vim in base image
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y --no-install-recommends \
        rsync \
        vim \
        && \
    apt-get autoremove && \
    rm -rf /var/lib/apt/lists/*


# Build Murfey in a branch image
FROM base as build
COPY ./ /python-murfey/
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y --no-install-recommends \
        build-essential \
        busybox \
        net-tools \
        libpq-dev \
        && \
    busybox --install && \
    python -m venv /venv && \
    /venv/bin/python -m pip install --upgrade \
        pip \
        build \
        importlib-metadata && \
    /venv/bin/python -m pip install /python-murfey[sxt]


# Transfer completed Murfey build to base image
FROM base

# Define external build arguments
ARG groupid
ARG groupname
ARG userid

# Copy completed Murfey build across and set user and group permissions
COPY --from=build /venv/ /venv/
RUN groupadd -r -g "${groupid}" "${groupname}" && \
    useradd -r -M "${groupname}" -u "${userid}" -g "${groupname}" && \
    chown -R "${userid}":"${groupid}" /venv && \
    chmod -R a+x /venv
ENV PATH=/venv/bin:$PATH
USER "${userid}":"${groupid}"
