FROM ruby:3.3

# Install the runtime interface client for Ruby
RUN gem install aws_lambda_ric

# Add the runtime interface client to the PATH
ENV PATH="/usr/local/bundle/bin:/opt/bin:${PATH}"

# Create a directory for the Lambda function
ENV LAMBDA_TASK_ROOT=/var/task
RUN mkdir -p ${LAMBDA_TASK_ROOT}
WORKDIR ${LAMBDA_TASK_ROOT}

ENV ARTIFACT_DIR=/opt
ENV FUNCTION_DIR=${LAMBDA_TASK_ROOT}

# Install dependencies
RUN apt update -y && apt install -y postgresql-common unzip vorbis-tools pip sox python-is-python3
# Copy custom libraries
COPY ./assets/bin/ffmpeg ${ARTIFACT_DIR}/bin/
RUN chmod a+x /opt/bin/ffmpeg

RUN git clone http://www.pogo.org.uk/~mark/bpm-tools.git && cd bpm-tools && make && cp bpm /opt/bin/

# Copy just enough of the shared gem to satisfy bundle install when it runs.
# This way we can make code changes easily in `shared/lib/*`, and not create
# full docker rebuilds for speed
RUN mkdir -p ${LAMBDA_TASK_ROOT}/shared
COPY shared/shared.gemspec ${LAMBDA_TASK_ROOT}/shared/

COPY lambdas ${LAMBDA_TASK_ROOT}/lambdas

RUN ls -laR ${LAMBDA_TASK_ROOT}

WORKDIR ${LAMBDA_TASK_ROOT}/lambdas/unzipper
# Copy Gemfile and Gemfile.lock
RUN ls -la
RUN gem install bundler && \
    bundle config set --local path "/vendor/bundle" && \
    bundle install

# Copy application code
COPY shared ${LAMBDA_TASK_ROOT}/shared
#RUN bundle config set --local deployment 'true' && bundle install

WORKDIR ${LAMBDA_TASK_ROOT}/lambdas/unzipper

# Set runtime interface client as default command for the container runtime
ENTRYPOINT [ "aws_lambda_ric" ]

# Set the Lambda handler
CMD ["app.lambda_handler"]
