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

#Create a container with: docker run -td -p 8800:8800 -p 8900:8900 --name lmpsrv lmpsrv:latest
# the -t (tty) flag is needed so docker can send SIGINT for proper shutdown.
#Attach to a container with: docker exec -it lmpsrv /bin/ash
#When inside a container, you can dettach with: CONTROL+P+Q
#Check logs with: docker logs -f lmpsrv

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

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 LmpCommon/LmpCommon.shproj       /LunaMultiplayer/LmpCommon/LmpCommon.shproj
COPY Lidgren/Lidgren.shproj           /LunaMultiplayer/Lidgren/Lidgren.shproj
COPY LmpGlobal/LmpGlobal.shproj       /LunaMultiplayer/LmpGlobal/LmpGlobal.shproj
COPY Lidgren.Core/Lidgren.Core.csproj /LunaMultiplayer/Lidgren.Core/Lidgren.Core.csproj
COPY Lidgren.Net/Lidgren.Net.csproj   /LunaMultiplayer/Lidgren.Net/Lidgren.Net.csproj
COPY uhttpsharp/uhttpsharp.csproj     /LunaMultiplayer/uhttpsharp/uhttpsharp.csproj
COPY LmpUpdater/LmpUpdater.csproj     /LunaMultiplayer/LmpUpdater/LmpUpdater.csproj
COPY Server/Server.csproj             /LunaMultiplayer/Server/Server.csproj

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/Server && \
    dotnet restore -r ${OS_BASE}.${OS_VERSION}-${TARGET}

COPY . /LunaMultiplayer

FROM base AS debug
WORKDIR /LunaMultiplayer/Server
ENV DOTNET_PerfMapEnabled=1
ENV COMPlus_PerfMapEnabled=1
CMD [ "/bin/ash" ]

FROM --platform=$BUILDPLATFORM base AS builder
WORKDIR /LunaMultiplayer/Server
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} -o Publish

FROM ${OS_BASE}:${OS_VERSION}
RUN apk add icu-libs libstdc++ libgcc
COPY --from=builder /LunaMultiplayer/Server/Publish/ /LMPServer/
EXPOSE 8800/udp 8900/tcp
VOLUME "/LMPServer/Config" "/LMPServer/Plugins" "/LMPServer/Universe" "/LMPServer/logs"
STOPSIGNAL SIGINT
WORKDIR /LMPServer
CMD ./Server
