FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src

COPY ["RobotNet.WebApp/RobotNet.WebApp.csproj", "RobotNet.WebApp/"]
COPY ["RobotNet.WebApp/libman.json", "RobotNet.WebApp/"]
COPY ["RobotNet.MapShares/RobotNet.MapShares.csproj", "RobotNet.MapShares/"]
COPY ["RobotNet.RobotShares/RobotNet.RobotShares.csproj", "RobotNet.RobotShares/"]
COPY ["RobotNet.Script.Shares/RobotNet.Script.Shares.csproj", "RobotNet.Script.Shares/"]
COPY ["RobotNet.Script/RobotNet.Script.csproj", "RobotNet.Script/"]
COPY ["RobotNet.Script.Expressions/RobotNet.Script.Expressions.csproj", "RobotNet.Script.Expressions/"]
COPY ["RobotNet.Shares/RobotNet.Shares.csproj", "RobotNet.Shares/"]
COPY ["RobotNet.Clients/RobotNet.Clients.csproj", "RobotNet.Clients/"]

RUN dotnet restore "RobotNet.WebApp/RobotNet.WebApp.csproj"

WORKDIR /src/RobotNet.WebApp
RUN dotnet tool install -g Microsoft.Web.LibraryManager.Cli
ENV PATH="${PATH}:/root/.dotnet/tools"
RUN libman restore

WORKDIR /src
COPY RobotNet.WebApp/ RobotNet.WebApp/
COPY RobotNet.MapShares/ RobotNet.MapShares/
COPY RobotNet.RobotShares/ RobotNet.RobotShares/
COPY RobotNet.Script.Shares/ RobotNet.Script.Shares/
COPY RobotNet.Script/ RobotNet.Script/
COPY RobotNet.Script.Expressions/ RobotNet.Script.Expressions/
COPY RobotNet.Shares/ RobotNet.Shares/
COPY RobotNet.Clients/ RobotNet.Clients/

RUN rm -rf ./RobotNet.WebApp/bin
RUN rm -rf ./RobotNet.WebApp/obj
RUN rm -rf ./RobotNet.MapShares/bin
RUN rm -rf ./RobotNet.MapShares/obj
RUN rm -rf ./RobotNet.RobotShares/bin
RUN rm -rf ./RobotNet.RobotShares/obj
RUN rm -rf ./RobotNet.cript.Shares/bin
RUN rm -rf ./RobotNet.cript.Shares/obj
RUN rm -rf ./RobotNet.Script/bin
RUN rm -rf ./RobotNet.Script/obj
RUN rm -rf ./RobotNet.Script.Expressions/bin
RUN rm -rf ./RobotNet.Script.Expressions/obj
RUN rm -rf ./RobotNet.Shares/bin
RUN rm -rf ./RobotNet.Shares/obj
RUN rm -rf ./RobotNet.Clients/bin
RUN rm -rf ./RobotNet.Clients/obj

WORKDIR "/src/RobotNet.WebApp"
RUN dotnet build "RobotNet.WebApp.csproj" -c Release -o /app/build

FROM build AS publish
WORKDIR /src/RobotNet.WebApp
RUN dotnet publish "RobotNet.WebApp.csproj" -c Release -o /app/publish --no-restore

FROM nginx:alpine AS final
WORKDIR /usr/share/nginx/html

COPY --from=publish /app/publish/wwwroot .
COPY RobotNet.WebApp/nginx.conf /etc/nginx/nginx.conf

ENTRYPOINT ["nginx", "-g", "daemon off;"]