###
### Build Variables
###
FROM localhost/debian:latest

# deploy options
# -p (port) and -v (volume) both go host:container
LABEL config_default="\
-p 19132:19132/udp \
-p 19133:19133/udp \
-v /srv/vol/mcbe/data:/vol/data"

# Build variables
# uid that the files owner user should have
ARG FILESUID=5000

###
### General Setup
###

# install packages we want
RUN apt update -y && apt install -y libcurl4 zip

# create minecraft server user with file owner UID
RUN addgroup --gid $FILESUID mcadmin && \
    adduser mcadmin --ingroup mcadmin --uid $FILESUID --disabled-password --gecos "Minecraft Server Admin" --shell /usr/sbin/nologin && \
    rm /home/mcadmin/.bashrc

# Copy custom scripts
COPY assets/bin/ /usr/local/bin/

###
### Minecraft
###
WORKDIR /home/mcadmin

# download Minecraft Bedrock dedicated server
RUN url=$(wget -q https://www.minecraft.net/en-us/download/server/bedrock/ -O - | grep -Eo 'https://[^ ]+bin-linux/bedrock-server-[^ ]+\.zip' | head -n 1) && \
    wget $url && \
    echo "Unzipping ..." && \
    unzip -q $(basename $url) && \
    rm $(basename $url) && \
    chown -R mcadmin:mcadmin ./

# copy minecraft config
COPY --chown=mcadmin:mcadmin assets/minecraft/ /home/mcadmin/

# copy units to systemd
COPY assets/systemd/ /etc/systemd/system/

# enable systemd units
RUN systemctl enable mcbe.service mcbe-backup.timer