# syntax=docker/dockerfile:1.3
FROM python:3.10-slim-bullseye AS sistema_base
USER root
RUN useradd --create-home --uid 1000 --shell /bin/bash briq
# installo le dipendenze che servono anche a runtime
RUN apt-get update\
    && apt-get install -y libpq-dev nginx curl bash\
    gettext mosquitto-clients libjpeg-dev postgresql-client-13 \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf /var/cache/apt/archives
RUN mkdir -p /django /django_static /var/lib/nginx /vue /logs /home/briq/.cache/pypoetry && \ 
    touch /nginx.conf /maybe_ssl.conf /full_chain.pem /poetry_location && \
    chown -R briq:briq /var/lib/nginx /django /vue /django_static /nginx.conf /logs /maybe_ssl.conf /full_chain.pem /poetry_location /home/briq/.cache/pypoetry
SHELL ["/bin/bash", "-c"]

USER briq
RUN curl -sSL https://install.python-poetry.org | python3 -\
    && echo "/home/briq/.local/bin/poetry" > /poetry_location

USER root
RUN ln -s "$(cat /poetry_location)" /usr/bin/ && rm /poetry_location


FROM sistema_base as sistema_build
USER root
RUN apt-get update\
    && apt-get -y install npm --no-install-recommends\
    && npm install -g javascript-obfuscator\
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf /var/cache/apt/archives

FROM sistema_build as sistema_build_dipendenze_python
USER root
RUN apt-get update\
    && apt-get -y install gcc build-essential git libpython3-dev ccache 

COPY --chown=briq:briq /django/pyproject.toml /django/poetry.lock /django/
USER briq
RUN --mount=target=/home/briq/.cache/pypoetry/cache,type=cache,uid=1000,gid=1000 cd /django && poetry install --no-dev
USER root
RUN bash -c "set -e; find / -iname '__pycache__' -prune -exec rm -rf '{}' \; \
    && find / -iname '*.pyc' -delete"


FROM sistema_base AS sistema_base_con_python
USER briq:briq
COPY --from=sistema_build_dipendenze_python /home/briq/.cache/pypoetry/ /home/briq/.cache/pypoetry/


FROM sistema_build_dipendenze_python as sistema_build_python
USER briq:briq
WORKDIR /django
RUN --mount=target=/home/briq/.cache/pypoetry/cache,type=cache,uid=1000,gid=1000 poetry install
USER root
COPY --chown=briq:briq django /django/
USER briq:briq
RUN --mount=target=/ccache,type=cache,uid=1000,gid=1000 env CCACHE_DIR=/ccache bash compila_apps.sh
USER root


FROM sistema_build AS sistema_build_js
USER root
COPY --chown=briq:briq vue/package.json vue/package-lock.json /vue/
USER briq:briq
WORKDIR /vue
RUN npm install
COPY /vue/ /vue/
#COPY docker/babel.config.js /vue/babel.config.js
RUN npm run build


FROM sistema_base_con_python AS sistema_deploy
USER briq:briq
COPY --from=sistema_build_python /django_static/ /django_static/
RUN true #vedi https://github.com/moby/moby/issues/37965
COPY --from=sistema_build_python /django /django/
RUN true
COPY --from=sistema_build_python /django/pyproject.toml /django/
RUN true
COPY --from=sistema_build_js /vue/dist /vue/dist
WORKDIR /
ARG GIT_COMMIT_SHA=N/A
ARG GIT_COMMIT_REF=N/A
ARG GIT_CLEAN=true
ARG GIT_COMMIT_DATE=ContinuousIntegration
USER root
RUN echo "{\"shash\": \"${GIT_COMMIT_SHA}\", \"lhash\": \"${GIT_COMMIT_SHA}\", \"commsdate\": \"${GIT_COMMIT_DATE}\", \"refnames\": \"${GIT_COMMIT_REF}\", \"clean\":\"${GIT_CLEAN}\"}" > /git_info
COPY --chown=briq:briq docker/test_and_build/run_in_docker.sh docker/test_and_build/startstop_utils.sh docker/test_and_build/nginx.conf.tpl /
RUN cp -f /git_info /vue/dist/ver.json
RUN chown briq:briq /vue/dist/ver.json
USER briq:briq
CMD /run_in_docker.sh
