# OpenSimulator in a container: the tools, a release of the core and its
# modules, from the packages of the kit, on an Ubuntu LTS (which has the .NET
# runtime and libgdiplus in its own repositories). No database server: the
# database is another container, or the host's (see packaging/container/README.md).
# Built by packaging/container/build, which gives it the packages (pkgs/).
ARG BASE=docker.io/library/ubuntu:26.04

FROM ${BASE}

ARG OPENSIM_SYSTEM_USER=opensim
ARG OPENSIM_SYSTEM_UID=8002
ARG OPENSIM_SYSTEM_GROUP=opensim
ARG OPENSIM_SYSTEM_GID=8002

ARG OPENSIM_ADMIN_USER=opensim_admin
ARG OPENSIM_ADMIN_UID=1000
ARG OPENSIM_ADMIN_GROUP=opensim_admin
ARG OPENSIM_ADMIN_GID=1000

ARG OPENSIM_VAR_ROOT=/var/lib/opensim

ENV DEBIAN_FRONTEND=noninteractive

# A mirror that does not answer once is asked again
RUN echo 'Acquire::Retries "5";' >/etc/apt/apt.conf.d/80retries

# The account of the instances, with a fixed ID: a folder of the host shared
# with the container can be given to it (chown 8002:8002)
RUN grep -q "^${OPENSIM_SYSTEM_GROUP}:" /etc/group || groupadd --system -g ${OPENSIM_SYSTEM_GID} ${OPENSIM_SYSTEM_GROUP}
RUN grep -q "^${OPENSIM_SYSTEM_USER}:" /etc/passwd || useradd --system -u ${OPENSIM_SYSTEM_UID} -g ${OPENSIM_SYSTEM_GID} \
    --home-dir ${OPENSIM_VAR_ROOT} --create-home --shell /bin/bash \
    ${OPENSIM_SYSTEM_USER}

RUN grep -q "^${OPENSIM_ADMIN_GROUP}:" /etc/group || groupadd --system -g ${OPENSIM_ADMIN_GID} ${OPENSIM_ADMIN_GROUP}
RUN grep -q "^${OPENSIM_ADMIN_USER}:" /etc/passwd || useradd -u ${OPENSIM_ADMIN_UID} -g ${OPENSIM_ADMIN_GID} \
    -G ${OPENSIM_SYSTEM_GID},sudo \
    --create-home --shell /bin/bash ${OPENSIM_ADMIN_USER}

# The packages depend on "a database server, or PostgreSQL, or SQLite" and on
# "a MySQL client": what is installed first satisfies them, so no server is added
# (the database is another container, or another machine).
# The Magiiic repository gives the packages of the family.

# Install ca-certificates curl gpg openssl before adding sources
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
    ca-certificates curl gpg openssl

COPY magiiic.list /etc/apt/sources.list.d/magiiic.list

#dotnet-runtime-10.0
RUN curl -fsSL https://apt.magiiic.com/magiiic-packaging.asc \
    | gpg --dearmor -o /usr/share/keyrings/magiiic-packaging.gpg \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
        opensim-cli \
    && opensim install-dotnet \
    && /usr/share/opensim-cli/quick-install.sh
    # \
    # && mkdir -p /etc/opensim ${OPENSIM_VAR_ROOT} /var/log/opensim \
    # && chown opensim:opensim /etc/opensim ${OPENSIM_VAR_ROOT} /var/log/opensim
    # && /usr/share/opensim-cli/quick-install.sh

RUN rm -rf /pkgs /var/lib/apt/lists/*

# Volumes are only relevant if there is no need to access them from host
# VOLUME ["/etc/opensim", "${OPENSIM_VAR_ROOT}", "/var/log/opensim"]

USER root
WORKDIR ${OPENSIM_VAR_ROOT}

RUN echo "${OPENSIM_ADMIN_USER} ALL = (ALL) NOPASSWD: ALL" >> /etc/sudoers.d/opensim_admin
# RUN echo "${OPENSIM_SYSTEM_USER} ALL = (ALL) NOPASSWD: ALL" >> /etc/sudoers.d/opensim_admin

# USER ${OPENSIM_ADMIN_USER}
# WORKDIR /home/${OPENSIM_ADMIN_USER}

USER ${OPENSIM_SYSTEM_USER}
WORKDIR ${OPENSIM_VAR_ROOT}

STOPSIGNAL SIGTERM

# ENTRYPOINT ["/usr/local/bin/opensim-container"]
