# Define global args
ARG RUNTIME_VERSION
ARG DISTRO_VERSION

# Grab a fresh copy of the image and install ruby and build the runtime interface client gem
FROM ruby:${RUNTIME_VERSION}-alpine${DISTRO_VERSION} AS build-image

RUN gem install bundler
RUN apk add --no-cache git

ARG RIC_BUILD_DIR="/build"
# Create directory to build the Runtime Interface Client gem
RUN mkdir -p ${RIC_BUILD_DIR}

WORKDIR ${RIC_BUILD_DIR}

COPY . .
RUN rake build


# Grab a fresh copy of the Ruby image
FROM ruby:${RUNTIME_VERSION}-alpine${DISTRO_VERSION}

# Copy the Runtime Interface Client gem and install it
ARG RIC_BUILD_DIR="/build"
COPY --from=build-image ${RIC_BUILD_DIR}/pkg/aws_lambda_ric*.gem aws_lambda_ric.gem
RUN gem install aws_lambda_ric.gem

ARG FUNCTION_DIR="/function"

RUN mkdir -p ${FUNCTION_DIR}
# Copy function code
COPY test/integration/test-handlers/echo/* ${FUNCTION_DIR}
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}

ENTRYPOINT ["aws_lambda_ric"]
CMD ["app.App::Handler.process"]