##########################################################################
#
#   Data Transport Network System 
#
#   This image contains a complete data transport system, including the
#   news server and application code. 
#
#   Note when installing inn news server:
#
#   A wrinkle when installing the inn package is that it runs makedbz 
#   to setup up the history databases, but it also checks to see if it
#   has a valid hostname. During the build, we do not have a fully
#   qualified one, so this step of the install fails. When we run the
#   resulting container, innd does not start up properly.
#
#   Starting with inn-2.7.3, you can set the INN_HOSTNAME environment
#   variable to work around this (makedbz simply tests if the hostname
#   has a "." in it as an indication of a full hostname). As of EL 9.6,
#   the version of inn is still 2.7.2 and doesn't support INN_HOSTNAME
#   yet (the check also becomes a warning so the database initialization
#   would actually have worked).
#
#   Until then, we need to hack the inn.conf file and set the domain
#   option. We need to do this prior to installing the package, so we
#   pull in one from the host directory. 
#
#   2025-06-09  Todd Valentic
#               Initial implementation
#
##########################################################################

FROM almalinux/9-init 

LABEL   name="datatransport-messagebox" \
        vendor="Todd Valentic" \
        release="2025-06" \
        summary="Data transport application" \
        maintainer="Todd Valentic <todd.valentic@gmail.com>" \
        description="Complete data transport system image"

ARG BUILD_GROUPS="" \
    USER_NAME=transport \
    USER_UID=1000 \
    USER_GID=1000 \
    INN_HOSTNAME=news.local

ENV DATA_TRANSPORT_PATH=/opt/transport \
    PATH="/venv/bin:$PATH" \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    UV_CACHE_DIR=/root/.cache/uv \
    UV_COMPILE_BYTECODE=1 \
    UV_FROZEN=1 \
    UV_LINK_MODE=copy \
    UV_NO_MANAGED_PYTHON=1 \
    UV_PROJECT_ENVIRONMENT=/venv \
    UV_PYTHON=3.12 \
    UV_REQUIRE_HASHES=1 \
    UV_VERIFY_HASHES=1 \
    VIRTUAL_ENV=/venv

# Install OS packages ----------------------------------------------------

# Work around until inn-2.7.3 is available as an rpm
COPY --chown=9:13 config/etc/news/inn.conf /etc/news/

RUN dnf install -y epel-release && \
    crb enable && \
    dnf module enable postgres1l:15 && \
    dnf install -y \
        hostname \
        inn \
        python3.12 \
        telnet \
        vim-enhanced \
        tmux \
        postgresql postgresql-server \
        && \
    postgresql-setup --initdb && \
    dnf clean all && \
    rm -rf /var/cache/dnf /var/cache/microdnf

COPY --from=ghcr.io/astral-sh/uv:0.7.13 /uv /uvx /usr/local/bin/

# Install postgresql ------------------------------------------------------ 

RUN dnf module enable postgres1l:15 && \
    dnf install -y --no-docs postgresql postgresql-server && \
    dnf clean all && \
    rm -rf /var/cache/dnf /var/cache/microdnf && \
    postgresql-setup --initdb && \
    systemctl start postgresql && \
    systemctl enable postgresql.server


# Copy config and application files ---------------------------------------

COPY config/ /
COPY --chown=$USER_UID:$USER_GID transport/ $DATA_TRANSPORT_PATH

RUN chown -R news:news /etc/news /var/lib/news

# Install Python packages for data transport application ------------------

WORKDIR $DATA_TRANSPORT_PATH

RUN --mount=type=cache,target=/root/.cache/uv \
    uv venv $VIRTUAL_ENV && \
    uv sync --no-install-project --no-editable $BUILD_GROUPS && \
    groupadd --gid $USER_GID $USER_NAME && \
    useradd --uid $USER_UID --gid $USER_GID --create-home --shell /bin/bash $USER_NAME && \
    systemctl enable datatransport.service && \
    systemctl enable innd.service && \
