FROM node:20.11.0-bullseye

ARG USERNAME=node
ARG NPM_GLOBAL=/usr/local/share/npm-global

# Add NPM global to PATH and disable update notifier
ENV PATH=${NPM_GLOBAL}/bin:${PATH}
ENV NPM_CONFIG_UPDATE_NOTIFIER=false

# Configure global npm install location and install CLI tools
RUN \
  if ! getent group npm > /dev/null 2>&1; then groupadd -r npm; fi \
  && usermod -a -G npm ${USERNAME} \
  && umask 0002 \
  && mkdir -p ${NPM_GLOBAL} \
  && touch /usr/local/etc/npmrc \
  && chown ${USERNAME}:npm ${NPM_GLOBAL} /usr/local/etc/npmrc \
  && chmod g+s ${NPM_GLOBAL} \
  && npm config -g set prefix ${NPM_GLOBAL} \
  && su ${USERNAME} -c "npm config -g set prefix ${NPM_GLOBAL}" \
  && su ${USERNAME} -c "umask 0002 && npm install -g http-server" \
  && npm cache clean --force > /dev/null 2>&1

# Install system packages
RUN apt-get update && DEBIAN_FRONTEND=noninteractive \
    apt-get -y install --no-install-recommends jq bash netcat curl \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Create script directory
RUN mkdir -p /cosmos-script

# Set app working directory
WORKDIR /app

# Copy app source code
COPY . .

# Install app dependencies
RUN npm install --legacy-peer-deps

EXPOSE 7000
