ARG DOTNET_VERSION=8.0
FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION} AS build

ARG OPENSIM_VERSION=0.9.3.0

# Cache Bust to avoid caching skipping the latest commit
ADD https://api.github.com/repos/opensim/opensim/git/refs/heads/${OPENSIM_VERSION} version.json
RUN git clone --depth 1 --branch "${OPENSIM_VERSION}" https://github.com/opensim/opensim.git /app
WORKDIR /app
RUN rm -rf .git && \
	./runprebuild.sh && \
	./compile.sh && \
	find . -name "*.pdb" -delete && \
	rm -r -- *prebuild* compile.sh ThirdParty TESTING.txt Prebuild OpenSim* Makefile BUILDING.md && \
	rm bin/config-include/storage/SQLiteStandalone.ini
COPY defaults  bin/defaults

FROM mcr.microsoft.com/dotnet/runtime:${DOTNET_VERSION} AS prepare

ARG OPENSIM_VERSION=0.9.3.0

LABEL \
	org.opencontainers.image.title="OpenSimulator (unofficial)" \
	org.opencontainers.image.authors="code@soupbowl.io" \
	org.opencontainers.image.source="https://github.com/soup-bowl/opensimulator-docker" \
	org.opencontainers.image.licenses="MIT" \
	org.opencontainers.image.version="${OPENSIM_VERSION}"

# hadolint ignore=DL3008
RUN apt-get update && \
	apt-get install --no-install-recommends -y libc6-dev libgdiplus libsqlite3-dev uuid-runtime && \
	apt-get clean && \
	rm -rf /var/lib/apt/lists/*

COPY --from=build /app /opt/opensim

EXPOSE 9000

WORKDIR /opt/opensim/bin

COPY docker-entrypoint.sh /usr/local/bin/

FROM prepare AS final-screen

RUN apt-get update && \
	apt-get install --no-install-recommends -y screen && \
	apt-get clean && \
	rm -rf /var/lib/apt/lists/*

ENTRYPOINT ["docker-entrypoint.sh"]
CMD [ "screen", "-S", "OpenSim", "-D", "-m", "dotnet", "OpenSim.dll" ]

FROM prepare AS final

ENTRYPOINT ["docker-entrypoint.sh"]
CMD [ "dotnet", "OpenSim.dll", "-console=basic" ]
