FROM --platform=linux/amd64 ubuntu:jammy

ARG DST_USER_ID
ARG DST_GROUP_ID
ARG LOCAL_BUILD=0

ENV PIPELINE_ID=00000000-0000-0000-0000-000000000000
ENV SCANHUB_URL="http://localhost:3000/api/v1"

ENV ALLOW_GITHUB_SSH=True

VOLUME ["/input"]
VOLUME ["/report"]
VOLUME ["/status"]

COPY ./apt-requirements.txt /tmp/apt.txt

RUN apt-get update -y && \
    DEBIAN_FRONTEND=noninteractive TZ="Europe/UTC" \
    apt-get install -y $(cat /tmp/apt.txt) && \
    setcap cap_net_raw,cap_net_admin=eip /bin/tcpdump

# Install Chrome browser for Karma
# RUN cd /tmp && wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && apt-get update -y && apt-get install -y ./google-chrome-stable_current_amd64.deb && rm -f google-chrome-stable_current_amd64.deb

RUN if [ ${LOCAL_BUILD} -eq 1 ]; then groupadd nandy; else groupadd -g "${DST_GROUP_ID}" nandy; fi
RUN useradd -d /home/nandy -m -s /bin/bash -u "${DST_USER_ID}" -g "${DST_GROUP_ID}" nandy;
USER nandy

ENV HOME /home/nandy
ENV WORKDIR $HOME/processing
ENV PROCESSING_VENV $HOME/processing/venv

COPY --chown=nandy:nandy ./ssh-config /home/nandy/.ssh
RUN chmod 400 /home/nandy/.ssh/github_rsa

ENV NVM_DIR="$HOME/.nvm"
RUN echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm' >> $HOME/.profile

ENV NVM_DIR $HOME/.nvm
ENV NODE_VERSION 22.18.0

RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.40.0/install.sh | env -u NODE_VERSION bash \
    && . $NVM_DIR/nvm.sh \
    && nvm install $NODE_VERSION \
    && nvm alias default $NODE_VERSION \
    && nvm use default \
    && npm install --global yarn@1.22.22

# Fix git issue with big resps
RUN git config --global http.postBuffer 52428800
RUN git config --global core.compression 0
RUN git config --global http.version HTTP/1.1
RUN git config --global submodule.recurse true
RUN git config --global submodule.fetchJobs 8
RUN git config --global fetch.recurseSubmodules on-demand

SHELL ["/bin/bash", "-l", "-c"]

RUN mkdir $WORKDIR $HOME/extracted_package
WORKDIR $WORKDIR

COPY --chown=nandy ./src/requirements.txt $WORKDIR

RUN python3 -m venv $PROCESSING_VENV # && source $PROCESSING_VENV/bin/activate && pip install -r $WORKDIR/requirements.txt

COPY --chown=nandy:nandy ./runtime_check $WORKDIR/runtime_check
RUN source $PROCESSING_VENV/bin/activate && pip install $WORKDIR/runtime_check/


COPY --chown=nandy:nandy ./scanfeeder /usr/local/bin/scanfeeder
COPY --chown=nandy:nandy ./src/vsix_package_processing.py $WORKDIR
COPY --chown=nandy:nandy ./meta $HOME/meta
COPY --chown=nandy:nandy ./run.sh /usr/local/bin/run.sh
RUN chmod +x /usr/local/bin/run.sh
COPY --chown=nandy ./pipe_create.sh /usr/local/bin/pipe_create.sh
RUN chmod +x /usr/local/bin/pipe_create.sh
COPY --chown=nandy ./pipe_listener.sh /usr/local/bin/pipe_listener.sh
RUN chmod +x /usr/local/bin/pipe_listener.sh
COPY --chown=nandy ./pipe_remove.sh /usr/local/bin/pipe_remove.sh
RUN chmod +x /usr/local/bin/pipe_remove.sh

RUN bash -c 'source $PROCESSING_VENV/bin/activate && pip install -r $WORKDIR/requirements.txt'

RUN mkdir $WORKDIR/config

COPY entrypoint.sh /usr/local/bin/entrypoint.sh

# Release version argument
ARG RELEASE_VERSION
LABEL org.opencontainers.image.version="${RELEASE_VERSION}"
ENV PLUGIN_VERSION="${RELEASE_VERSION}"

# Build date argument
ARG BUILD_DATE
LABEL org.opencontainers.image.created="${BUILD_DATE}"
ENV PLUGIN_BUILD_DATE="${BUILD_DATE}"

USER root
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
