########################################
# build webui                          #
########################################
FROM node:14 AS builder

# copy files
COPY       webui/       /pycs/webui_dev/
WORKDIR    /pycs/webui_dev

# install packages
RUN npx browserslist@latest --update-db
RUN npm install

# build webui
RUN npm run build

########################################
# build pycs image                     #
########################################
FROM python:3.9 AS pycs

# get the user and group. if not defined, fallback to root
ARG UID=root
ARG GID=root

# copy backend files
COPY pycs/              /pycs/pycs/
COPY migrations/        /pycs/migrations/
COPY app.py             /pycs/
COPY requirements.txt   /pycs/

WORKDIR    /pycs/

RUN mkdir projects db
RUN chown ${UID}:${GID} projects db

# copy UI from builder
COPY --from=builder /pycs/webui_dev/dist /pycs/webui

# install required libraries for opencv
RUN apt-get update \
 && apt-get install -y libgl1-mesa-glx ffmpeg libsm6 libxext6

# install dependencies
RUN python -m pip install --upgrade pip
RUN pip install numpy
RUN pip install -r /pycs/requirements.txt

# configure start
USER      ${UID}:${GID}
EXPOSE    5000

#ENTRYPOINT ["python", "app.py"]
