FROM    086679231553.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python311 AS base

# 1. Prepare system environment

USER root

RUN apt update
RUN apt upgrade -y

RUN apt install -y which curl git tar zip openssl
RUN apt install -y default-jre build-essential supervisor
# pigz for split_file.sh
RUN apt install -y pigz awscli
RUN apt install -y gnupg
RUN apt install -y gosu expect

# Explicitly set locale and related env vars to use en_US.UTF-8
RUN apt install -y locales
RUN localedef -c -f UTF-8 -i en_US en_US.UTF-8
ENV LC_ALL=en_US.UTF-8
ENV LANG=en_US.UTF-8

# Cleanup
RUN rm -rf /var/lib/apt/lists/*

# 1.1. install ITMSTransporter (optionally)
ARG WITH_ITMS=false
COPY itms /itms
RUN if [ "$WITH_ITMS" = "true" ] ; then \
      cd /itms && make download_from_apple install clean ; \
    else  \
      echo 'Skip ITMS installation' ; \
    fi

# Prepares EFS mount
RUN mkdir /mnt/efs && chown -R worker:worker /mnt/efs

# 2. Prepare python environment

USER worker
WORKDIR /var/app
ENV PATH="/home/worker/.local/bin:${PATH}"

RUN python -m pip install --upgrade --user pip
RUN python -m pip install --user pipenv


COPY --chown=worker Pipfile.off /var/app/Pipfile
COPY --chown=worker Pipfile.off.lock /var/app/Pipfile.lock

# to make pipenv install venv in /var/app/.venv
ENV PIPENV_VENV_IN_PROJECT=1
ENV PATH="/var/app/.venv/bin:${PATH}"

# IS_DEV indicates image for development environment
ARG IS_DEV=false
RUN if [ "$IS_DEV" = "true" ] ; then \
      pipenv sync --dev ; \
    else \
      pipenv sync ; \
    fi

# 3. install the application

COPY feed_ingestion /var/app/feed_ingestion/
COPY Makefile /var/app/
COPY entrypoint.sh /var/app/
COPY setup.py /var/app/
COPY MANIFEST.in /var/app/

RUN if [ "$IS_DEV" = "true" ] ; then \
      python3 -m pip install -e . ; \
    else \
      python3 -m pip install . ; \
    fi

# save version of the image in the container
# it is used in the application to report version
ARG version=unknown
ENV APP_VERSION=${version}

# Prevent irrelevant warnings from cluttering Kibana
# efedorov: Maybe it is not actual after upgrade python
ENV PYTHONWARNINGS="ignore::UserWarning:cffi.cparser.py"

ENTRYPOINT ["pipenv", "run"]

FROM base AS dev

COPY .flake8 /var/app/.flake8
COPY tests /var/app/tests/
COPY integration_tests /var/app/integration_tests/
COPY dev.py /var/app/
COPY pytest.ini /var/app/

CMD ["python", "dev.py"]

FROM base AS production

COPY conf /var/app/conf/
USER root
ENTRYPOINT ["/var/app/entrypoint.sh"]
EXPOSE 9001
