FROM nginxinc/nginx-unprivileged:stable-alpine

ENV PUBLIC_DIR /usr/share/nginx/html
ENV TACTICAL_DIR /opt/tactical
ENV TACTICAL_READY_FILE ${TACTICAL_DIR}/tmp/tactical.ready
USER root

RUN deluser --remove-home nginx \
  && addgroup -S nginx -g 1000 \
  && adduser -S -G nginx -u 1000 nginx

RUN apk add --no-cache bash wget tar grep

SHELL ["/bin/bash", "-e", "-o", "pipefail", "-c"]

COPY api/tacticalrmm/tacticalrmm/settings.py /tmp/settings.py

RUN WEB_VERSION=$(grep -o 'WEB_VERSION.*' /tmp/settings.py | cut -d'"' -f 2) \
  && webtar="trmm-web-v${WEB_VERSION}.tar.gz" \
  && wget -q "https://github.com/amidaware/tacticalrmm-web/releases/download/v${WEB_VERSION}/${webtar}" -O /tmp/${webtar} \
  && tar -xzf /tmp/${webtar} -C ${PUBLIC_DIR} \
  && rm -f /tmp/${webtar}

RUN mv ${PUBLIC_DIR}/dist/* ${PUBLIC_DIR}
RUN rm -rf ${PUBLIC_DIR}/dist
RUN chown -R nginx:nginx /etc/nginx && chown -R nginx:nginx ${PUBLIC_DIR}

COPY docker/containers/tactical-frontend/entrypoint.sh /docker-entrypoint.d/
RUN chmod +x /docker-entrypoint.d/entrypoint.sh

USER nginx

EXPOSE 8080
