# We accept build-args to specify which flavor of debian to use for this build.
# This makes it easy to produce NSCLI artifacts for multiple OS versions with one Dockerfile.
# This idea come from https://dh-virtualenv.readthedocs.io/en/1.1/examples.html
ARG DIST_ID="debian"
ARG DIST_VERSION="11"

# Create the base debian image to be used for both building and testing NSCLI.
FROM docker.io/${DIST_ID}:${DIST_VERSION} AS build-base

# Passing build args into the scope below
ARG DIST_ID
ARG DIST_VERSION

# Environment variable housekeeping
ENV DEBIAN_FRONTEND="noninteractive"
ENV DEBEMAIL="nsi@rackspace.com"
ENV DEBFULLNAME="GTS NetSec Automation Engineering"
ENV PS1='[\u@\h \W]\$ '
ENV LC_ALL="C.UTF-8"

# Install Rackspace CA certs
# Prefer the Rackspace mirror for package downloads
COPY ["docker/ca_certs/*.crt", "/usr/local/share/ca-certificates/"]
RUN apt-get -q update && apt-get -yq install apt-transport-https ca-certificates wget && \
    update-ca-certificates && \
    mv /etc/apt/sources.list /etc/apt/sources.list.old && \
    case "$DIST_ID" in \
       "ubuntu") COMPONENTS="main multiverse restricted universe" \
       ;; \
       "debian") COMPONENTS="main contrib non-free" \
       ;; \
    esac && \
    CODENAME="$(grep -m 1 CODENAME /etc/os-release | cut -d '=' -f 2)" && \
    printf 'deb https://mirror.rackspace.com/%s/ %s %s\n' ${DIST_ID} ${CODENAME} "${COMPONENTS}" > /etc/apt/sources.list && \
    cat /etc/apt/sources.list.old >> /etc/apt/sources.list && \
    apt-get update && \
    if [ $DIST_ID = "ubuntu" ] && [ $DIST_VERSION = "22.04" ]; then \
        wget https://archive.debian.org/debian-archive/debian/pool/main/d/dh-virtualenv/dh-virtualenv_1.1-1_all.deb && \
        apt-get -yq install ./dh-virtualenv_1.1-1_all.deb || exit 1 ; \
        apt-get -yq install --no-install-recommends python3-distutils || exit 1; \
    fi


# Switch to a new image for building the NSCLI artifact.
FROM build-base AS pkg-builder

# Install build requirements
# System packages (apt-get): Build tools and libraries needed by the build system itself
# Python packages (pip): Upgraded to latest versions for modern packaging features
#   -U (upgrade): Get latest versions rather than Ubuntu's potentially outdated packages
#   -I (ignore-installed): Install without uninstalling apt-managed packages (avoids conflicts and error where
# pip can't uninstall packages installed by apt)
#   --break-system-packages: Required for Python 3.12+ to install packages outside venv (safe in throwaway container 
#   where breaking system python isn't a concern)
# dh-virtualenv creates the .deb with an embedded Python virtualenv with bundled dependencies, 
#   making it self-contained and avoiding system python conflicts
RUN apt-get -yq install --no-install-recommends \
        build-essential \
        debhelper \
        devscripts \
        dh-virtualenv \
        equivs \
        gcc \
        libffi-dev \
        libgmp3-dev \
        lsb-release \
        python3-dev \
        python3-pip \
        # python3-setuptools \ # TODO try build again w/ these commented out (installed by pip below)
        python3-venv && \
    apt-get clean && rm -rf "/var/lib/apt/lists"/* && \
    python3 -m pip --no-cache-dir install --break-system-packages -U -I pip && \
    python3 -m pip --no-cache-dir install --break-system-packages -U -I setuptools wheel toml

# These build-args are metadata for the final debian artifact we produce.
# VERSION = nscli version. Usually this is the only one we care about, but in rare circumstances, the other variables
# might need to be incremented in the event of a patch/post release.
ARG VERSION
ARG DEB_REVISION=1
ARG REVISION=1

# Copy NSCLI source into the container
ADD . /dpkg-build
# python3-distutils is deprecated in Ubuntu 24.04 so copy in the compatible debian/control file
RUN if [ "$DIST_VERSION" = "24.04" ]; then \
        cp /dpkg-build/debian/control.ubuntu24 /dpkg-build/debian/control; \
    fi
WORKDIR /dpkg-build

# Generate constraints file for pip install
RUN python3 ./scripts/generate_constraints.py > ./constraints.txt

# Produce the new artifacts
RUN debchange --create --distribution "unstable" --package "nscli" \
    --newversion "${VERSION}-${DEB_REVISION}$(lsb_release -is | tr '[:upper:]' '[:lower:]')${REVISION}~$(lsb_release -rs)" \
    "Release notes are published at https://gh-pages.pvceng.rax.io/RSS-Engineering/nsi-nscli/Changelog.html" && \
    dpkg-buildpackage --unsigned-source --unsigned-changes --build=binary && \
    mkdir -p /dpkg && \
    cp -pl /nscli[-_]* /dpkg

# Start a new image to test the installation and operation of the artifact we just produced.
FROM build-base AS tester

COPY --from=pkg-builder /dpkg /dpkg
RUN apt-get -y install ./dpkg/nscli*.deb
RUN nscli -v
