ARG BASE_REGISTRY=registry1.dso.mil
ARG BASE_IMAGE=redhat/ubi/ubi10
ARG BASE_TAG=10.1
ARG ELASTIC_VERSION=9.4.0
ARG ELASTIC_REGISTRY=docker.elastic.co
ARG ELASTIC_IMAGE=elastic-maps-service/elastic-maps-server

ARG NODEJS_VERSION=24

# EMS Server source image
FROM ${ELASTIC_REGISTRY}/${ELASTIC_IMAGE}:${ELASTIC_VERSION} AS source

# Main layer
FROM ${BASE_REGISTRY}/${BASE_IMAGE}:${BASE_TAG}

EXPOSE 8080

ARG NODEJS_VERSION

# Install node software and user
# RHEL 10 dropped module streams: the default stream (22) uses 'nodejs',
# alternate streams (24+) use 'nodejs${V}' with suffixed binaries.
RUN set -eu && \
    if [ "${NODEJS_VERSION}" = "22" ]; then \
      PKG_NODE=nodejs; \
      PKG_I18N=nodejs-full-i18n; PKG_DOCS=nodejs-docs; \
    else \
      PKG_NODE="nodejs${NODEJS_VERSION}"; \
      PKG_I18N="nodejs${NODEJS_VERSION}-full-i18n"; PKG_DOCS="nodejs${NODEJS_VERSION}-docs"; \
    fi && \
    dnf install -y "${PKG_NODE}" && \
    rpm --verify "${PKG_NODE}" && \
    # Test
    node --version && \
    # Cleanup
    dnf -y remove "${PKG_DOCS}" "${PKG_I18N}" && \
    dnf -y clean all --enablerepo='*' && \
    if [ "${NODEJS_VERSION}" != "22" ]; then \
      ln -sf "/usr/bin/node-${NODEJS_VERSION}" /usr/bin/node && \
      ln -sf "/usr/bin/npm-${NODEJS_VERSION}" /usr/bin/npm && \
      ln -sf "/usr/bin/npx-${NODEJS_VERSION}" /usr/bin/npx; \
    fi && \
    # Add user
    groupadd --gid 1000 node && \
    useradd --uid 1000 --gid node --shell /bin/bash --create-home node

# Copy the different components from EMS Server
COPY --from=source --chown=node:node /usr/src/app /usr/src/app

# Copy the license
COPY LICENSE /licenses/elastic-maps-server

# Advertise the platform to the app
ENV EMS_PLATFORM="ironbank"

# Create EMS log folder
RUN mkdir -p /var/log/elastic-maps-server \
    && chown -R node:node /var/log/elastic-maps-server

WORKDIR /usr/src/app/server

# Run a server status check against the /status endpoint)
HEALTHCHECK --interval=30s --start-period=15s --timeout=5s \
    CMD node --no-warnings "/usr/src/app/server/app/healthcheck.js"

USER 1000

ENTRYPOINT [  ]

CMD ["node", "app/index.js"]
