FROM ubuntu:latest

# Update OS and install deps
RUN apt update && apt clean && apt upgrade -y
RUN apt install -y curl apt-utils sudo curl python3 python-is-python3 git wget nano openssh-server apt-transport-https zip

# Get JDK Ready
RUN wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo tee /usr/share/keyrings/adoptium.asc
RUN 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" | sudo tee /etc/apt/sources.list.d/adoptium.list
RUN cat /etc/apt/sources.list.d/adoptium.list
RUN sudo apt-get update

RUN apt update

# Install memory safe JDK
RUN DEBIAN_FRONTEND=noninteractive apt install temurin-21-jdk -y

# Download fabric server
# This needs to be changed to the version you wish to support.
RUN mkdir -vp /home/mc 
RUN mkdir -vp /home/mc/minecraft

#Auto Accept EULA for MC
COPY eula /home/mc/minecraft/eula.txt
COPY server.properties /home/mc/minecraft/server.properties
# Add a user
RUN useradd mc
# Let user use Bash
RUN usermod --shell /bin/bash mc
# Provide users ownership
RUN chown -R mc:mc /home/mc

#COPY mod manager installer
COPY mod-manager-installer.sh /var/mod-manager-installer.sh
RUN chmod +x /var/mod-manager-installer.sh

COPY connection_manager.js /usr/bin/connections
RUN chmod +x /usr/bin/connections
RUN ln -s /usr/bin/connections /usr/bin/con

ENV PATH="/usr/bin:$PATH"


RUN apt-get install -y ca-certificates curl gnupg
RUN sudo mkdir -p /etc/apt/keyrings
RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
ENV NODE_MAJOR=18
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
RUN apt-get update
RUN apt-get install nodejs -y
RUN npm install -g holesail

COPY pm2 /var/tools/pm2
COPY scripts /var/tools/scripts

COPY get_latest_fabric_version.js /var/tools/scripts

RUN cd /var/tools/scripts && npm i axios

RUN data=$(node /var/tools/scripts/get_latest_fabric_version.js 1.21.1) && \
    echo "Download URL: $data" && \
    wget -O /home/mc/minecraft/server.jar "$data"


RUN chmod +x -R /var/tools

# Install PM2
RUN npm install pm2 -g


# Copy over start.sh to allow for backround run
COPY start.sh /start.sh
RUN chmod +x /start.sh

# Set root pass
RUN echo 'root:noshallpass' | chpasswd

ENTRYPOINT ["/bin/bash", "/start.sh"]