# OpenSim for a container
#   docker build -t opensim-standalone .

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

LABEL Version="0.1"
LABEL Description="Docker container running OpenSimulator"

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 \
    && ./runprebuild.sh \
    && msbuild \
    && cd $OPENSIMDIR \
    && rm -rf OpenSim

# 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 over any extra configuration files we're including
RUN mkdir -p $OPENSIMDIR/bin/config
COPY --chown=opensim:opensim config/ $OPENSIMDIR/bin/config/
# Use the default, included configuration parameters
RUN cp $OPENSIMDIR/bin/OpenSim.ini.example $OPENSIMDIR/bin/OpenSim.ini

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

