FROM python:3.10

RUN mkdir /app

# Install NodeJS
ENV NODE_MAJOR=20
RUN apt-get update
RUN apt-get install -y ca-certificates curl gnupg
RUN mkdir -p /etc/apt/keyrings
RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_${NODE_MAJOR}.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
RUN apt-get update
RUN apt-get install -y nodejs
RUN rm -rf /var/lib/apt/lists/*

RUN mkdir /temp
RUN mkdir /temp/libpostal-build
WORKDIR /temp

RUN git clone https://github.com/openvenues/libpostal
WORKDIR /temp/libpostal
RUN ./bootstrap.sh
RUN ./configure --datadir=/temp/libpostal-build
RUN make -j4
RUN make install
RUN ldconfig

WORKDIR /app

COPY . .

RUN pip install -r requirements/base.txt -r requirements/production.txt
RUN npm install
RUN npm run build

# Placeholder values so the static files collect
RUN DATABASE_URL=postgres://x/x \
    SECRET_KEY=x \
    ALLOWED_HOSTS=x \
    SITE_URL=x \
    OIDC_RP_REALM_URL=x \
    OIDC_RP_CLIENT_ID=x \
    OIDC_RP_CLIENT_SECRET=x \
    CLAMD_TCP_SOCKET=1 \
    CLAMD_TCP_ADDR=x \
    DEFAULT_COUNTRY=x \
    DEFAULT_CONTRACTEE_NAME=x \
    DEFAULT_CONTRACTEE_STREET=x \
    DEFAULT_CONTRACTEE_ZIP=x \
    DEFAULT_CONTRACTEE_DISTRICT=x \
    DEFAULT_CONTRACTEE_ICO_NUMBER=x \
    DEFAULT_STAFF_GROUPS=x \
    NASTENKA_API_URL=x \
    NASTENKA_API_TOKEN=x \
    CHOBOTNICE_API_URL=x \
    python manage.py collectstatic --noinput --settings=registry.settings.production

RUN bash -c "adduser --disabled-login --quiet --gecos app app &&  \
             chmod -R o+r /app/ && \
             chmod o+x /app/run.sh"
USER app

ENV DJANGO_SETTINGS_MODULE "registry.settings.production"

EXPOSE 8000

CMD ["bash", "run.sh"]