# The Dockerfile to build centos with a sudo user
FROM fedora:39

# Install the required packages
RUN dnf install -y sudo systemd

# Create a new user
RUN useradd -m -s /bin/bash user && \
    usermod -aG wheel user && \
    echo "user ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

# Set the working directory
WORKDIR /home/user
COPY run.sh /home/user/run.sh

# remove the last `zsh` in the script to prevent the endless runtime
RUN echo "" >> ./run.sh
RUN tac run.sh | awk '/^zsh$/ && !found { found = 1; next } 1' | tac > run.sh.tmp && mv run.sh.tmp run.sh

# remove the line `systemctl --user start syncthing.service`
RUN tac run.sh | awk '/^systemctl --user start syncthing.service$/ && !found { found = 1; next } 1' | tac > run.sh.tmp && mv run.sh.tmp run.sh

# remove the line `docker run -d -p 9001:9001 --name portainer_agent --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker/volumes:/var/lib/docker/volumes portainer/agent:latest`
RUN tac run.sh | awk '/^docker run -d -p 9001:9001 --name portainer_agent --restart=always -v \/var\/run\/docker.sock:\/var\/run\/docker.sock -v \/var\/lib\/docker\/volumes:\/var\/lib\/docker\/volumes portainer\/agent:latest$/ && !found { found = 1; next } 1' | tac > run.sh.tmp && mv run.sh.tmp run.sh

# Set the user
USER user

# Set the entrypoint
CMD bash run.sh
