# 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="10"

# Create the base debian image to be used for both building and testing NSCLI.
FROM ${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 = "20.04" ]; then \
        wget http://http.us.debian.org/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 ; \
    fi


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

# Install build requirements
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-distutils \
        python3-pip \
        python3-setuptools \
        python3-venv && \
    apt-get clean && rm -rf "/var/lib/apt/lists"/* && \
    python3 -m pip --no-cache-dir install -U pip && \
    python3 -m pip --no-cache-dir install -U setuptools wheel

# 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
WORKDIR /dpkg-build

# Produce the new artifacts
RUN dch --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://pages.github.rackspace.com/nsi/nscli/Changelog.html" && \
    dpkg-buildpackage -us -uc -b && \
    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
