# OpenSim with Herbal3d region modules
#   docker build -t opensim-herbal3d .

# Built with dotnet8
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build

LABEL Description="Docker container running OpenSimulator with Herbal3d extensions"

# In the target image, these are directories built and used
# While parameters, these usually don't change
ARG OPENSIMHOME=/home/opensim
ARG OPENSIMDIR=/home/opensim/opensim
ARG OPENSIMCONFIG=/home/opensim/opensim/bin/config
ARG VERSIONDIR=/home/opensim/VERSION
ARG OS_ACCOUNT=opensim

# Arguments for fetching the proper version of OpenSimulator
ARG OS_GIT_REPO=git://opensimulator.org/git/opensim
ARG OS_GIT_BRANCH=master
ARG OS_BUILDTARGET=Release
ARG OS_SLN=OpenSim.sln

# Arguments about the build environment passed in so they can be embedded in the container
ARG BUILD_DATE=20240101.0000
ARG BUILD_DAY=20240101
ARG OS_DOCKER_VERSION=1.0.0
ARG OS_DOCKER_GIT_BRANCH=main
ARG OS_DOCKER_GIT_COMMIT=62fda19d0038a274bf3fd922b023b149b3a40a90
ARG OS_DOCKER_GIT_COMMIT_SHORT=62fda19d

# Each of the different packages can have their branch over-ridden with a --build-arg
ARG COMMON_ENTITIES_BRANCH=dotnet6
ARG COMMON_UTIL_BRANCH=main
ARG TRANSPORT_BRANCH=master
ARG LODEN_BRANCH=dotnet6
ARG OSAUTH_BRANCH=dotnet6
ARG RAGU_BRANCH=dotnet6

# Include required packages ('coreutils git vim' included for debugging)
RUN apt-get update \
    && apt-get upgrade -y \
    && apt-get install -y \
        coreutils procps git vim \
        screen \
        ccrypt \
        libgdiplus \
        libc6-dev \
        mariadb-client

# The simulator is run under the user name 'opensim'
RUN adduser --disabled-password --gecos 'OpenSimulator user' $OS_ACCOUNT

USER $OS_ACCOUNT:$OS_ACCOUNT

# Version information that goes in the image
RUN mkdir -p $VERSIONDIR \
    && cd $VERSIONDIR \
    && echo $BUILD_DATE > "BUILD_DATE" \
    && echo $BUILD_DAY > "BUILD_DAY" \
    && echo $OS_DOCKER_VERSION > "OS_DOCKER_VERSION" \
    && echo $OS_DOCKER_GIT_BRANCH > "OS_DOCKER_GIT_BRANCH" \
    && echo $OS_DOCKER_GIT_COMMIT > "OS_DOCKER_GIT_COMMIT" \
    && echo $OS_DOCKER_GIT_COMMIT_SHORT > "OS_DOCKER_GIT_COMMIT_SHORT" \
    && echo ${OS_DOCKER_VERSION}-${BUILD_DAY}-${OS_DOCKER_GIT_COMMIT_SHORT} > "OS_DOCKER_IMAGE_VERSION"

# Fetch the latest version of the OpenSimulator sources.
RUN cd $OPENSIMHOME \
    && git clone --depth=1 -b $OS_GIT_BRANCH --single-branch $OS_GIT_REPO opensim

RUN cd $OPENSIMDIR/addon-modules/ \
    && git clone --depth=1 -b $COMMON_ENTITIES_BRANCH --single-branch https://github.com/Herbal3d/HerbalCommonEntitiesCS.git

RUN cd $OPENSIMDIR/addon-modules/ \
    && git clone --depth=1 -b $COMMON_UTIL_BRANCH --single-branch https://github.com/Herbal3d/HerbalCommonUtilCS.git

RUN cd $OPENSIMDIR/addon-modules/ \
    && git clone --depth=1 -b $TRANSPORT_BRANCH --single-branch https://github.com/Herbal3d/HerbalTransportCS.git

RUN cd $OPENSIMDIR/addon-modules/ \
    && git clone --depth=1 -b $LODEN_BRANCH --single-branch https://github.com/Herbal3d/Loden.git

RUN cd $OPENSIMDIR/addon-modules/ \
    && git clone --depth=1 -b $OSAUTH_BRANCH --single-branch https://github.com/Herbal3d/OSAuthModule.git

RUN cd $OPENSIMDIR/addon-modules/ \
    && git clone --depth=1 -b $RAGU_BRANCH --single-branch https://github.com/Herbal3d/RaguOS.git

# The Herbal3d images have a special version building tool
RUN cd $BASEDIR \
    && git clone --depth=1 -b main --single-branch https://github.com/Herbal3d/BuildVersion.git \
    && cd BuildVersion \
    && dotnet build --configuration Debug BuildVersion.sln
ENV BUILDVERSION=$BASEDIR/BuildVersion/bin/Debug/net6.0/BuildVersion
RUN cd $OPENSIMDIR/addon-modules/HerbalCommonEntitiesCS/CommonEntities \
    && chmod +x updateVersion.sh \
    && ./updateVersion.sh $BUILDVERSION
RUN cd $OPENSIMDIR/addon-modules/HerbalCommonEntitiesCS/CommonEntitiesConv \
    && chmod +x updateVersion.sh \
    && ./updateVersion.sh $BUILDVERSION
RUN cd $OPENSIMDIR/addon-modules/HerbalTransportCS \
    && chmod +x updateVersion.sh \
    && ./updateVersion.sh $BUILDVERSION
RUN cd $OPENSIMDIR/addon-modules/Loden \
    && chmod +x updateVersion.sh \
    && ./updateVersion.sh $BUILDVERSION
RUN cd $OPENSIMDIR/addon-modules/RaguOS \
    && chmod +x updateVersion.sh \
    && ./updateVersion.sh $BUILDVERSION

# Extract version information from the fetched sources
RUN cd $OPENSIMDIR \
    && echo $OS_GIT_REPO > "$VERSIONDIR/OS_GIT_REPO" \
    && echo $OS_GIT_BRANCH > "$VERSIONDIR/OS_GIT_BRANCH" \
    && echo $OS_BUILDTARGET > "$VERSIONDIR/OS_BUILD_TARGET" \
    && git describe --tags --long --always > "$VERSIONDIR/OS_GIT_DESCRIBE" \
    && git rev-parse HEAD > "$VERSIONDIR/OS_GIT_COMMIT" \
    && git rev-parse --short HEAD > "$VERSIONDIR/OS_GIT_COMMIT_SHORT" \
    && grep "const string VersionNumber" ./OpenSim/Framework/VersionInfo.cs | \
        sed -e "s/^.*VersionNumber = \"\(.*\)\".*$/\1/" > "$VERSIONDIR/OS_VERSION"

# Use the proper drawing library
RUN cp $OPENSIMDIR/bin/System.Drawing.Common.dll.linux $OPENSIMDIR/bin/System.Drawing.Common.dll

# Build OpenSimulator
RUN cd $OPENSIMDIR \
    && chmod +x runprebuild.sh \
    && ./runprebuild.sh \
    && dotnet build --configuration $OS_BUILDTARGET $OS_SLN

# None of the configuration files in the built image are used
# Configuration files are all specified by invocation parameter.

# =======================================================================
# Create the binary only container with the dotnet8 runtime
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS release

# In the target image, these are directories built and used
ARG OPENSIMHOME=/home/opensim
ARG OPENSIMDIR=/home/opensim/opensim
ARG VERSIONDIR=/home/opensim/VERSION
ARG OS_BUILDTARGET=Release
ARG OS_ACCOUNT=opensim

# Include required packages ('coreutils,git,vim' included for debugging)
RUN apt-get update \
    && apt-get install -y \
        coreutils procps git vim \
        screen \
        ccrypt \
        cron \
        libgdiplus \
        libc6-dev \
        mariadb-client \
    && rm -rf /var/lib/{apt,dpkg,cache,log}/

# The simulator is run under the user name 'opensim'
RUN adduser --disabled-password --gecos 'OpenSimulator user' $OS_ACCOUNT

USER $OS_ACCOUNT:$OS_ACCOUNT

# Scripts that keep OpenSimulator running, make crash reports, clean up log files, etc
COPY --chown=opensim:opensim temp-run-scripts/*.sh $OPENSIMHOME
COPY --chown=opensim:opensim temp-run-scripts/crontab $OPENSIMHOME
COPY --chown=opensim:opensim temp-run-scripts/vimrc $OPENSIMHOME/.vimrc

# Copy the binary files
COPY --from=build --chown=opensim:opensim $OPENSIMDIR/bin $OPENSIMDIR/bin
COPY --from=build --chown=opensim:opensim $VERSIONDIR $VERSIONDIR

# The NuGet DLLs are not copied into the bin directory.
# TODO: Figure out why NuGet packages are not copied.
COPY --from=build --chown=opensim:opensim $OPENSIMDIR/.nuget/packages/fleck/1.2.0/lib/netstandard2.0/Fleck.dll $OPENSIMDIR/bin
COPY --from=build --chown=opensim:opensim $OPENSIMDIR/.nuget/packages/nlog/4.7.15/lib/netstandard2.0/NLog.dll $OPENSIMDIR/bin

# The .ini files in config-include in the image are not used and are emptied here to eliminate any confusion
RUN cd $OPENSIMDIR/bin/config-include \
    && $OPENSIMHOME/nullOutConfigInclude.sh

# Create the directory that is mounted that includes all the configuration
RUN mkdir -p $OPENSIMDIR/bin/config

WORKDIR $OPENSIMDIR/bin
ENTRYPOINT [ "/home/opensim/bootOpenSim.sh" ]

# No ports are exposed by default since the simulator and regions can be anywhere.
# The simulator defaults to port 9000.
# EXPOSE 9000/tcp
# EXPOSE 9000/udp

# Directories that can be overridden with a mounted volume for configuration.
# The default configuration is for a simple standalone region.
# All the normal configuration has been nulled out (OpenSim.ini is the default
#    and all the included files in bin/config-include are empty).
#    A non-standalone configuration replaces bin/config with files that do
#    the configuration of the whole system (include 'architecture' ini file and
#    the necessary other INI files).
VOLUME $OPENSIMDIR/bin/config


