#Build this image with: docker build --file Dockerfile_MasterServer -t lmpms:latest .
#Delete the image with the command: docker image rm lmpms:latest

#Create a container with: docker run -d -p 8700:8700/udp -p 8701:8701/tcp --restart=unless-stopped --name lmpms lmpms:latest
#Create a container with with specific ports: docker run -d -p 8700:8700 -p 8701:8701 -e PORT=8700 -e HTTP_PORT=8701 --name lmpms lmpms:latest
#Attach to a container with: docker exec -it lmpms /bin/ash
#When inside a container, you can dettach with: CONTROL+P+Q
#Check logs with: docker logs -f lmpms

#Stop a container with: docker stop lmpms
#Start a container with: docker start lmpms
#Remove a container with: docker container rm lmpms

ARG OS_BASE=alpine
ARG OS_VERSION=3.20

FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:6.0-${OS_BASE}${OS_VERSION} AS base

COPY .nuget                                 /LunaMultiplayer/.nuget
COPY LunaMultiPlayer.sln                    /LunaMultiplayer/LunaMultiPlayer.sln
COPY Lidgren.Core/Lidgren.Core.csproj       /LunaMultiplayer/Lidgren.Core/Lidgren.Core.csproj
COPY uhttpsharp/uhttpsharp.csproj           /LunaMultiplayer/uhttpsharp/uhttpsharp.csproj
COPY LmpCommonTest/LmpCommonTest.csproj     /LunaMultiplayer/LmpCommonTest/LmpCommonTest.csproj
COPY LmpUpdater/LmpUpdater.csproj           /LunaMultiplayer/LmpUpdater/LmpUpdater.csproj
COPY LmpMasterServer/LmpMasterServer.csproj /LunaMultiplayer/LmpMasterServer/LmpMasterServer.csproj
COPY MasterServer/MasterServer.csproj       /LunaMultiplayer/MasterServer/MasterServer.csproj
COPY LmpUpdater/packages.config             /LunaMultiplayer/LmpUpdater/packages.config
COPY uhttpsharp/packages.config             /LunaMultiplayer/uhttpsharp/packages.config

ARG OS_BASE
ARG OS_VERSION
ARG TARGETARCH
ARG TARGETVARIANT
RUN export TARGET=$(echo ${TARGETARCH}${TARGETVARIANT} | sed -e 's/amd64/x64/' -e 's/armv8/arm64/' -e 's/armv7/arm/'); \
    cd /LunaMultiplayer && \
    dotnet restore MasterServer -r ${OS_BASE}.${OS_VERSION}-${TARGET}

FROM --platform=$BUILDPLATFORM base AS builder
COPY . /LunaMultiplayer
WORKDIR /LunaMultiplayer/MasterServer
ARG OS_BASE
ARG OS_VERSION
ARG TARGETARCH
ARG TARGETVARIANT
RUN export TARGET=$(echo ${TARGETARCH}${TARGETVARIANT} | sed -e 's/amd64/x64/' -e 's/armv8/arm64/' -e 's/armv7/arm/'); \
    dotnet publish -c Release -r ${OS_BASE}.${OS_VERSION}-${TARGET} --self-contained true -p:PublishTrimmed=true -o Publish

FROM ${OS_BASE}:${OS_VERSION}
# icu-data-full is needed to cross-reference server country codes with a list of allowed CCs
RUN apk update && apk add icu-libs icu-data-full libstdc++ libgcc
COPY --from=builder /LunaMultiplayer/MasterServer/Publish/ /LmpMasterServer/
EXPOSE 8700/udp 8701/tcp
STOPSIGNAL SIGINT
WORKDIR /LmpMasterServer
CMD [ "./MasterServer" ]
