FROM registry.access.redhat.com/ubi8/nodejs-16@sha256:382f3359921567a2e2648f814fd25ff3ba7da8bbf41172ff94aaa9d5bc6214ac
ENV NODE_ENV=production
WORKDIR /apm

# Copy Node.js profiler
COPY eg-node-monitor.zip ./

RUN if [ -x "$(command -v unzip)" ]; then \
    echo "unzip found";\
  elif [ -x "$(command -v apt-get)" ]; then \
    echo "unzip not found";\
    apt-get update && apt-get install -y unzip && apt-get clean; \
  else \
    echo "unzip not found and can't install it";\
  fi

# Extract the zip
RUN mkdir -p btm \
  && unzip eg-node-monitor.zip -d ./btm -x "build/*" \
  && mv btm/scripts/container_copy.sh ./copy.sh \
  && chmod -R 700 ./btm \
  && rm -vrf eg-node-monitor.zip

RUN gccVersion=$(gcc --version) || echo "gcc not found" && pythonVersion=$(python3 --version) || pythonVersion=$(python --version) || echo "Python not found" && echo "gcc: $gccVersion and python: $pythonVersion"
WORKDIR /apm/btm

RUN node -v && [ -z "$pythonVersion" ] && [ -z "$gccVersion" ] \
  && if [ -x "$(command -v gcc)" ]; then \
    echo "Python and GCC are found."; \
    npm rebuild \
    && node scripts/node_module_availability_checker.js --for-container-env \
    || npm install --production || echo "npm installed failed."; \
  # For Alpine
  elif [ -x "$(command -v apk)" ]; then \
    echo "apk found"; \
    apk add --no-cache --virtual .gyp python3 make g++ \
    && npm rebuild \
    && node scripts/node_module_availability_checker.js --for-container-env \
    || npm install --production || echo "npm installed failed"; \
    apk del .gyp; \
  # For Debian/Ubuntu
  elif [ -x "$(command -v apt-get)" ]; then \
    echo "apt-get found"; \
    apt-get install -y g++ make python3 \
    && npm rebuild \
    && node scripts/node_module_availability_checker.js --for-container-env \
    || npm install --production || echo "npm installed failed"; \
    apt-get remove --purge -y g++ make python3 && apt-get -y autoremove; \
  # For RHEL/CentOS/Amazon Linux
  elif [ -x "$(command -v yum)" ]; then \
    echo "yum found"; \
    yum install -y gcc-c++ make python3 \
    && npm rebuild \
    && node scripts/node_module_availability_checker.js --for-container-env \
    || npm install --production || echo "npm installed failed"; \
    yum remove -y gcc-c++ make python3; \
  else \
    echo "Nothing is matched"; \
  fi

# optional: Will throw error if any node_modules is not installed properly
RUN node scripts/node_module_availability_checker.js --for-container-env

WORKDIR /apm

#Run as non-root user
#USER 1001