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

FROM mono:5 as built
# FROM mono:4.6 as built

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

ENV OPENSIMDIR=/home/opensim/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 \
        mysql-client \
        sqlite \
    && rm -rf /var/lib/apt/lists/*

# The simulator is run under the user name 'opensim'
# (From https://stackoverflow.com/questions/27701930/add-user-to-docker-container)
RUN adduser --disabled-password --gecos 'OpenSimulator user' opensim

USER opensim:opensim

# Scripts that keep OpenSimulator running, make crash reports, clean up log files, etc
COPY --chown=opensim:opensim scripts/* /home/opensim/
COPY --chown=opensim:opensim vimrc /home/opensim/.vimrc

# Fetch and build the latest version of the OpenSimulator sources.
# This includes a lot of steps so we can delete the sources and git files
#    to make the resulting layer small.
RUN cd /home/opensim \
    && git clone git://opensimulator.org/git/opensim \
    && rm -rf $OPENSIMDIR/.git \
    && cd $OPENSIMDIR/addon-modules/ \
    && git clone https://github.com/Herbal3d/HerbalCommonEntitiesCS.git \
    && git clone https://github.com/Herbal3d/HerbalTransportCS.git \
    && git clone https://github.com/Herbal3d/Loden.git \
    && git clone https://github.com/Herbal3d/OSAuthModule.git \
    && git clone https://github.com/Herbal3d/RaguOS.git \
    && mkdir -p $OPENSIMDIR/bin/config \
    && cp $OPENSIMDIR/addon-modules/Loden/config/Loden.ini $OPENSIMDIR/bin/config \
    && cp $OPENSIMDIR/addon-modules/RaguOS/config/RaguOS.ini $OPENSIMDIR/bin/config \
    && cp $OPENSIMDIR/addon-modules/OSAuthModule/config/OSAuth.ini $OPENSIMDIR/bin/config \
    && cd $OPENSIMDIR \
    && ./runprebuild.sh \
    && nuget restore OpenSim.sln \
    && cp packages/*/lib/net45/* bin \
    && msbuild \
    && cd $OPENSIMDIR \
    && rm -rf $OPENSIMDIR/OpenSim \
    && rm -rf $OPENSIMDIR/addon-modules

# Use the default, included configuration parameters
RUN cp $OPENSIMDIR/bin/OpenSim.ini.example $OPENSIMDIR/bin/OpenSim.ini
# OpenSim.ini does an include of "config-include/Standalone.ini".
#     so copy an empty version to nullfy the default configuration.
COPY --chown=opensim:opensim config-include/ $OPENSIMDIR/bin/config-include/
COPY --chown=opensim:opensim config/ $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

