ARG DISTRO_VERSION

# Stage 1 - bundle base image + runtime interface client
# Grab a fresh copy of the image and install Python
FROM public.ecr.aws/docker/library/centos:${DISTRO_VERSION} AS python-centos-builder

ARG RUNTIME_VERSION

# Install apt dependencies
RUN yum install -y \
  gcc \
  gcc-c++ \
  tar \
  gzip \
  make \
  autoconf \
  automake \
  freetype-devel \
  yum-utils \
  findutils \
  openssl-devel \
  wget \
  libffi-devel \
  sqlite-devel

RUN RUNTIME_LATEST_VERSION=${RUNTIME_VERSION}.$(curl -s https://www.python.org/ftp/python/ | \
                                                grep -oE "href=\"$(echo ${RUNTIME_VERSION} | sed "s/\\./\\\./g")\.[0-9]+" | \
                                                cut -d. -f3 | \
                                                sort -rn | \
                                                while read -r patch; do \
                                                  $(wget -c https://www.python.org/ftp/python/${RUNTIME_VERSION}.$patch/Python-${RUNTIME_VERSION}.$patch.tgz -O Python-${RUNTIME_VERSION}.$patch.tgz); \
                                                  [ $? -eq 0 ] && echo $patch && break; \
                                                done) \
  && tar -xzf Python-${RUNTIME_LATEST_VERSION}.tgz \
  && cd Python-${RUNTIME_LATEST_VERSION} \
  && ./configure --prefix=/usr/local --enable-shared \
  && make \
  && make install \
  && ln -s /usr/local/bin/python${RUNTIME_VERSION} /usr/local/bin/python${RUNTIME_LATEST_VERSION}

# Stage 2 - clean python build dependencies
FROM public.ecr.aws/docker/library/centos:${DISTRO_VERSION} AS python-centos
RUN yum install -y \
  libffi-devel

# Copy the compiled python to /usr/local
COPY --from=python-centos-builder /usr/local /usr/local
ENV LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

# Stage 3 - build function and dependencies
FROM python-centos-builder AS build-image
ARG RUNTIME_VERSION
ARG ARCHITECTURE

# Install aws-lambda-cpp build dependencies
RUN yum install -y \
  tar \
  gzip \
  make \
  autoconf \
  automake \
  libtool \
  libcurl-devel \
  gcc-c++ \
  wget

# Install a modern CMake
RUN wget --quiet -O cmake-install https://github.com/Kitware/CMake/releases/download/v3.20.0/cmake-3.20.0-linux-${ARCHITECTURE}.sh && \
    sh cmake-install --skip-license --prefix=/usr --exclude-subdirectory;

ENV PATH=/usr/local/bin:$PATH
ENV LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH


# Include global args in this stage of the build
ARG RIC_BUILD_DIR="/home/build/"
# Create function directory
RUN mkdir -p ${RIC_BUILD_DIR}
# Copy function code and Runtime Interface Client .tgz
WORKDIR ${RIC_BUILD_DIR}
COPY . .
RUN make init build && \
    mv ./dist/awslambdaric-*.tar.gz ./dist/awslambdaric-test.tar.gz

# Include global args in this stage of the build
ARG FUNCTION_DIR="/home/app/"
# Create function directory
RUN mkdir -p ${FUNCTION_DIR}
# Copy function code
COPY tests/integration/test-handlers/echo/* ${FUNCTION_DIR}
# Copy Runtime Interface Client .tgz
RUN cp ./dist/awslambdaric-test.tar.gz ${FUNCTION_DIR}/awslambdaric-test.tar.gz

# Install the function's dependencies
WORKDIR ${FUNCTION_DIR}
ARG ENABLE_LTO=OFF
ENV ENABLE_LTO ${ENABLE_LTO}
RUN python${RUNTIME_VERSION} -m pip install \
          awslambdaric-test.tar.gz \
          --verbose \
          --target ${FUNCTION_DIR} && \
    rm awslambdaric-test.tar.gz


# Stage 4 - final runtime interface client image
# Grab a fresh copy of the Python image
FROM python-centos

# Include global arg in this stage of the build
ARG FUNCTION_DIR="/home/app/"
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}
# Copy in the built dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}

ENTRYPOINT [ "/usr/local/bin/python3", "-m", "awslambdaric" ]
CMD [ "app.handler" ]