# Use the node:16.20.2-buster image as the base image
FROM node:16.20.2-buster

# Set environment variables to avoid interactive prompts during the installation
ENV DEBIAN_FRONTEND=noninteractive

# Install GCC 8, G++ 8, Python 2.7, and Python 3.6
RUN apt-get update && apt-get install -y \
    gcc-8 \
    g++-8 \
    python \
    python3 \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Set GCC and G++ to use version 8
RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 100 \
    && update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 100

# Install Node.js and necessary build tools
# RUN apt-get install -y \ gcc-8 g++-8 python2.7 python3.6

# Verify installations
RUN gcc --version && \
    python --version && \
    python3 --version && \
    node --version && \
    npm --version

# Clean up
RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Set up the user and group
ARG USER_NAME=hostuser
ARG USER_UID
ARG USER_GID

RUN if getent group $USER_GID; then groupdel -f $(getent group $USER_GID | cut -d: -f1); fi && \
    if getent passwd $USER_UID; then userdel -f $(getent passwd $USER_UID | cut -d: -f1); fi && \
    groupadd --gid $USER_GID hostuser && \
    useradd --uid $USER_UID --gid $USER_GID -m hostuser

# RUN groupadd --gid $USER_GID $USER_NAME && \
#     useradd --uid $USER_UID --gid $USER_GID -m $USER_NAME

# Switch to the created user
USER $USER_NAME

# Print current directory and list files (debugging)
RUN pwd && ls -la

RUN mkdir -vp /home/$USER_NAME/linux

# Set working directory
WORKDIR /home/$USER_NAME/linux

# Copy source code with correct ownership
COPY --chown=$USER_NAME . .

# RUN npm install

RUN pwd && ls -la

# RUN ls -lrt node_modules/.bin

# Verify the Node.js and npm version
RUN node -v && npm -v && which npm
# RUN npm typescript -v && which npm
USER root
RUN npm install -g pkg --unsafe-perm
USER $USER_NAME
RUN which pkg && pkg --version

CMD ["npm", "run", "build"]
