# Use the latest Ubuntu image as base
FROM ubuntu:latest

# Set environment variables
ENV PATH="/usr/bin:$PATH"
ENV NODE_MAJOR=18

# Update OS and install essential dependencies
RUN apt-get update -y && apt-get upgrade -y && apt-get install -y \
    curl \
    apt-utils \
    sudo \
    python3 \
    python-is-python3 \
    git \
    wget \
    nano \
    openssh-server \
    apt-transport-https \
    zip \
    ca-certificates \
    gnupg

# Add Adoptium GPG key and repository for JDK
RUN wget -qO- https://packages.adoptium.net/artifactory/api/gpg/key/public | tee /usr/share/keyrings/adoptium.asc && \
    echo "deb [signed-by=/usr/share/keyrings/adoptium.asc] https://packages.adoptium.net/artifactory/deb $(awk -F= '/^VERSION_CODENAME/{print $2}' /etc/os-release) main" | tee /etc/apt/sources.list.d/adoptium.list && \
    apt-get update -y

# Install memory-safe JDK
RUN apt-get install -y temurin-21-jdk

# Create directories for the Minecraft server
RUN mkdir -p /home/mc/minecraft

# Add and configure a new user for the server
RUN useradd -m -s /bin/bash mc && chown -R mc:mc /home/mc

# Add NodeSource repository and install Node.js
RUN mkdir -p /etc/apt/keyrings && \
    curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && \
    echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list && \
    apt-get update -y && apt-get install -y nodejs

# Install Holesail globally
RUN npm install -g holesail

# Download the Velocity server JAR
RUN wget -O /home/mc/minecraft/server.jar https://api.papermc.io/v2/projects/velocity/versions/3.4.0-SNAPSHOT/builds/454/downloads/velocity-3.4.0-SNAPSHOT-454.jar

# Set up tools, configurations, and permissions
COPY pm2 /var/tools/pm2
RUN chmod +x -R /var/tools && npm install -g pm2

COPY velocity.toml /home/mc/minecraft
COPY forwarding.secret /home/mc/minecraft

# Add and prepare the startup script
COPY start.sh /start.sh
RUN chmod +x /start.sh

# Set the root password
RUN echo 'root:noshallpass' | chpasswd

# Set the default command to start the server
ENTRYPOINT ["/bin/bash", "/start.sh"]