FROM openjdk:8-jre
EXPOSE 25565

ARG MINECRAFT=1.7.10
ARG FORGE=10.13.4.1641

ENV \
    ForgeUrl1=http://files.minecraftforge.net/maven/net/minecraftforge/forge/${MINECRAFT}-${FORGE}/forge-${MINECRAFT}-${FORGE}-installer.jar \
    ForgeUrl2=http://files.minecraftforge.net/maven/net/minecraftforge/forge/${MINECRAFT}-${FORGE}-${MINECRAFT}/forge-${MINECRAFT}-${FORGE}-${MINECRAFT}-installer.jar \
    InstallerJar=forge-${MINECRAFT}-${FORGE}-installer.jar \
    MINECRAFT=$MINECRAFT \
    FORGE=$FORGE

ENV \
    INSTDIR=/usr/share/minecraft \
    RUNDIR=/minecraft

RUN useradd -U -d /minecraft -m forge

WORKDIR $INSTDIR

# Since the URL schema changed, we need to try both after another.
# Prepare the server files and remove the installer.
# The EULA needs to be present, so we also accept it at this point.
# The remaining jar beginning with forge-* is the launcher (multiple names
# possible), so we will symlink it to forge.jar for convenience.
RUN ( wget -O ${InstallerJar} ${ForgeUrl1} || wget -O ${InstallerJar} ${ForgeUrl2} ) && \
    echo "eula=true" > eula.txt && \
    java -jar ./${InstallerJar} --installServer && rm -f ${InstallerJar} && \
    ln -s "$(find -maxdepth 1 -name 'forge-*.jar')" forge.jar || exit 2

COPY entrypoint.sh /usr/bin

WORKDIR $RUNDIR

VOLUME ["/minecraft"]

ENTRYPOINT ["/usr/bin/entrypoint.sh"]