# Dockerfile

ARG AWS_ACCOUNT=086679231553

FROM --platform=linux/amd64 ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python312 AS base

WORKDIR /var/app

USER root

ENV PATH="${PATH}:/opt/libreoffice7.6/program/"
ENV DEBIAN_FRONTEND=noninteractive

# Update and install required packages. Fix CVEs
RUN apt-get update && apt-get install -y \
    curl gnupg \
    libxml2 libxml2-dev \
    libexpat1 \
    libc6 \
    libicu72 \
 && apt-mark hold libxml2 libexpat1 libc6 libicu72 \
 && apt-get clean && rm -rf /var/lib/apt/lists/*

# Install Node.js
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
    apt-get install -y nodejs && \
    node --version && npm --version

RUN curl -fsSL https://packagecloud.io/orchardit/libreoffice/gpgkey | gpg --dearmor > /etc/apt/keyrings/orchardit_libreoffice-archive-keyring.gpg \
    && curl -fsSL https://packagecloud.io/orchardit/libreoffice/gpgkey | gpg --dearmor > /etc/apt/trusted.gpg.d/orchardit_libreoffice.gpg
RUN curl -s https://packagecloud.io/install/repositories/orchardit/libreoffice/script.deb.sh?any=true | bash -x 
# there is no minimal installiation anymore,
RUN apt-get update && apt-get -y install libobasis7.6-base libobasis7.6-calc libobasis7.6-core libobasis7.6-draw libobasis7.6-en-us \
    libobasis7.6-extension-beanshell-script-provider libobasis7.6-extension-javascript-script-provider \
    libobasis7.6-extension-mediawiki-publisher libobasis7.6-extension-nlpsolver libobasis7.6-extension-pdf-import \
    libobasis7.6-extension-report-builder libobasis7.6-firebird libobasis7.6-graphicfilter libobasis7.6-images \
    libobasis7.6-impress libobasis7.6-librelogo libobasis7.6-libreofficekit-data libobasis7.6-math libobasis7.6-ogltrans \
    libobasis7.6-onlineupdate libobasis7.6-ooofonts libobasis7.6-ooolinguistic libobasis7.6-postgresql-sdbc \
    libobasis7.6-python-script-provider libobasis7.6-pyuno libobasis7.6-writer libobasis7.6-xsltfilter \
    libreoffice7.6-base libreoffice7.6-calc libreoffice7.6-dict-en libreoffice7.6-draw libreoffice7.6-en-us \
    libreoffice7.6-impress libreoffice7.6-math libreoffice7.6-ure libreoffice7.6-writer libreoffice7.6 default-jre \
        && soffice --version \
        && java --version \
        && apt-get -y install fontconfig

COPY fonts/*.deb /tmp/fonts/

RUN dpkg -i /tmp/fonts/fonts-asap_1.0_all.deb \
    && dpkg -i /tmp/fonts/fonts-averiasanslibre_1.0_all.deb \
    && fc-match Asap \
    && fc-match AveriaSansLibre \
    && rm -rf /tmp/fonts/

# Create and configure virtualenv and uwsgi
ENV VIRTUAL_ENV /var/venv
RUN python3.12 -m venv ${VIRTUAL_ENV}
ENV PATH "$VIRTUAL_ENV/bin:$PATH"
ENV PYTHONPATH="${PYTHONPATH}:/var/app/src"

RUN python -m pip install --upgrade pip setuptools

# Add application and install requirements
COPY requirements.txt requirements-test.txt ./
RUN pip install -r requirements.txt
COPY . /var/app
RUN chmod +x scripts/set-build-info-in-template.sh \
    && ./scripts/set-build-info-in-template.sh \
    && grep 'Build:' /var/app/src/frontend/templates/home.html

#######################################
### Build image for live deployment ###
#######################################
FROM base AS deploy

ENV PATH="${PATH}:/opt/libreoffice7.6/program/"

# Remove tests, scripts, etc.
RUN rm -rf /var/app/tests /var/app/scripts/ /var/app/.flake8 /var/app/.isort.cfg /var/app/.pylintrc

USER worker
ENTRYPOINT ["./run-server.sh"]

EXPOSE      8000

#################################
### Build image for unit-lint ###
#################################
FROM        base AS unit-lint

ENV PATH="${PATH}:/opt/libreoffice7.6/program/"

# Install dev/test requirements
RUN         pip install -I -r requirements-test.txt

# Install Node.js dependencies for Vitest
COPY package*.json ./
RUN npm install

USER root
ENTRYPOINT  ["scripts/unit-lint.sh"]


########################################
### Build image for test-integration ###
########################################
FROM        base AS test-integration

ENV PATH="${PATH}:/opt/libreoffice7.6/program/"

# Install test requirements
RUN         pip install -I -r requirements-test.txt

USER root
ENTRYPOINT  [ "scripts/test-integration.sh" ]
