ARG FROM_IMAGE

FROM $FROM_IMAGE

ARG H_GID
ARG H_UID
ARG PIP_REQ_FILE

RUN apt update && \
	DEBIAN_FRONTEND=noninteractive TZ=Europe/Berlin apt-get install -y tzdata \
	&& cp /usr/share/zoneinfo/Europe/Berlin /etc/localtime \
	&& echo "Europe/Berlin" > /etc/timezone 

RUN apt install -y bash \
	build-essential \
	git \
	curl \
	ca-certificates \
	python3-dev \
	python3-pip

RUN ln -s /usr/bin/python3 /usr/local/bin/python \
	&& addgroup --gid $H_GID user \
	&& adduser user --uid $H_UID --ingroup user --gecos "" --home /home/user/ --disabled-password

RUN apt-get update \
	&& apt-get install -y \
		bash nano vim mc \
		curl wget \
		htop screen

# if cpu container (install cpu only versions; for gpu, gpu versions)
RUN if [ ! -d /usr/local/cuda/ ]; then \
		python3 -m pip install --no-cache-dir --break-system-packages \
			torch --index-url https://download.pytorch.org/whl/cpu; \
		python3 -m pip install --no-cache-dir --break-system-packages \
			faiss-cpu; \
	else \
		python3 -m pip install --no-cache-dir --break-system-packages \
			faiss-gpu-cu12; \
	fi;

RUN mkdir -p /ums-agenten/persist/

ENV NLTK_DATA=/ums-agenten/persist/nltk/
ENV HF_HOME=/ums-agenten/persist/hf_home/

COPY ./docker/$PIP_REQ_FILE /ums-agenten/requirements.txt
RUN pip3 install --break-system-packages --no-cache-dir -r /ums-agenten/requirements.txt \
	&& python -m pip freeze > /ums-agenten/requirements.txt

COPY ./docker/init.py /ums-agenten/project/
COPY ./docker/setup.py /ums-agenten/project/
RUN pip3 install --break-system-packages -e /ums-agenten/project/

WORKDIR /ums-agenten/project/src/
RUN chown -R user:user /ums-agenten
USER user

CMD ["/bin/bash"]