# Use the specified base image
FROM ubuntu:jammy

# Add a new user with the same user ID and group ID from the host machine
ARG USER
ARG USER_ID
ARG GROUP
ARG GROUP_ID
RUN groupadd -g $GROUP_ID $GROUP && \
    useradd -r -u $USER_ID -g $GROUP $USER

# Install necessary software
RUN apt-get update && \
    apt-get install -y \
    llvm-dev \
    fish \
    vim \
    tmux \
    sudo \
    git \
    default-jdk \
    clang \
    lld \
    cmake \
    ninja-build \
    git \
    python3-pip \
    fuse3 \
    curl \
    autoconf \
    libtool

# Install Mull
RUN curl -L -o Mull.deb 'https://github.com/mull-project/mull/releases/download/0.21.1/Mull-14-0.21.1-LLVM-14.0-ubuntu-22.04.deb' \
    && apt install ./Mull.deb \
    && rm Mull.deb

# Install rclone
RUN curl -L -o rclone.deb 'https://downloads.rclone.org/v1.64.0/rclone-v1.64.0-linux-amd64.deb' \
    && apt install ./rclone.deb \
    && rm rclone.deb

# Install protobuf
ARG PROTOBUF_VERSION=3.20.3
RUN git clone -b v${PROTOBUF_VERSION} --recursive https://github.com/protocolbuffers/protobuf.git \
    && cd protobuf && ./autogen.sh \
    && ./configure --enable-static=no \
    && make -j${NPROC} install && ldconfig \
    && cd python && python3 setup.py install --cpp_implementation \
    && cd ../.. && rm -rf protobuf

# Give the current user sudo rights
ARG USER
RUN echo "$USER ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/$USER && \
    chmod 0440 /etc/sudoers.d/$USER
    
# Switch to the given user
ARG USER
USER $USER
