# Every VNC test server image, as stages off one shared base.
#
# Deliberately built from a small in-repo Dockerfile rather than a
# third-party desktop image so the exact server version is controlled by
# apt in the base distro rather than by whatever a random upstream image
# happens to bundle.
#
# docker-compose.yml selects a stage per service with `target:`.

FROM debian:bookworm-slim AS base

RUN apt-get update && apt-get install -y --no-install-recommends \
        x11-apps \
        x11-utils \
        xauth \
        procps \
        iproute2 \
        python3 \
        python3-xlib \
        python3-pil \
    && rm -rf /var/lib/apt/lists/*

# The build context root, tests/, has no __init__.py of its own to COPY.
RUN mkdir -p /tests && printf '"""Test suite package for vncdotool."""\n' > /tests/__init__.py
COPY servers/__init__.py /tests/servers/__init__.py
COPY goldens/__init__.py /tests/goldens/__init__.py
COPY goldens/scene_player.py /tests/goldens/scene_player.py
COPY goldens/click_targets.py /tests/goldens/click_targets.py
COPY goldens/scenes/ /tests/goldens/scenes/

COPY servers/scene-player.sh /scene-player.sh
RUN chmod +x /scene-player.sh

EXPOSE 5900

# Liveness only; readiness is `tests/functional/wait_for_servers.py docker`.
#
# This must never open an RFB connection: Xvnc counts every connection
# closed before a successful authentication towards BlacklistThreshold
# (default 5), so a connecting probe blacklists 127.0.0.1 within ~10s and
# refuses every later session. Speaking more of the protocol first does not
# help -- measured against TigerVNC 1.12.0, a bare connect, a version
# exchange and a version-plus-security-types exchange trip it identically.
HEALTHCHECK --interval=2s --timeout=2s --start-period=5s --retries=30 \
    CMD ss -H -ltn 'sport = :5900' | grep -q .


# TigerVNC's own X server. Serves with no authentication by default, or
# with classic VNC password auth when VNC_PASSWORD is set -- see
# tigervnc-entrypoint.sh. tigervnc-tools is where Debian keeps vncpasswd.
FROM base AS tigervnc

RUN apt-get update && apt-get install -y --no-install-recommends \
        tigervnc-standalone-server \
        tigervnc-common \
        tigervnc-tools \
        openssl \
    && rm -rf /var/lib/apt/lists/*

COPY servers/tigervnc-entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# `pgrep -f` searches the health check's own `sh -c` command line. An
# unbracketed pattern matches that, and passes with the player dead.
HEALTHCHECK --interval=2s --timeout=2s --start-period=5s --retries=30 \
    CMD ss -H -ltn 'sport = :5900' | grep -q . && pgrep -f "[s]cene_player" >/dev/null

ENTRYPOINT ["/entrypoint.sh"]


# x11vnc exporting a plain Xvfb display -- a different shape from TigerVNC
# (a VNC server bolted onto an existing X server) and so worth covering.
FROM base AS x11vnc

RUN apt-get update && apt-get install -y --no-install-recommends \
        xvfb \
        x11vnc \
        x11-utils \
        procps \
    && rm -rf /var/lib/apt/lists/*

COPY servers/x11vnc-entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

HEALTHCHECK --interval=2s --timeout=2s --start-period=5s --retries=30 \
    CMD ss -H -ltn 'sport = :5900' | grep -q . \
        && pgrep -f "[x]ev -root" >/dev/null \
        && pgrep -f "[s]cene_player" >/dev/null

ENTRYPOINT ["/entrypoint.sh"]


# Builds the vncev and example binaries from a pinned release, so neither
# final image needs a compiler. Replaces the old libvncserver.mk host build.
FROM base AS libvncserver-build

ARG LIBVNCSERVER_VERSION=0.9.14

RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        cmake \
        libssl-dev \
        zlib1g-dev \
        libjpeg-dev \
        ca-certificates \
        curl \
    && rm -rf /var/lib/apt/lists/*

RUN curl -fL \
        "https://github.com/LibVNC/libvncserver/archive/refs/tags/LibVNCServer-${LIBVNCSERVER_VERSION}.tar.gz" \
        -o /tmp/libvncserver.tar.gz \
    && mkdir -p /tmp/libvncserver \
    && tar xzf /tmp/libvncserver.tar.gz --strip-components=1 -C /tmp/libvncserver \
    && rm /tmp/libvncserver.tar.gz

# Named targets rather than `all`: skips the SDL/GTK example clients.
WORKDIR /tmp/libvncserver
RUN cmake -DCMAKE_BUILD_TYPE=Release . \
    && cmake --build . --target examples_vncev examples_example -- -j"$(nproc)"


# Event sink: prints events and renders no desktop, so utils.py marks
# it renders_desktop=False and readiness stops at the handshake.
FROM base AS vncev

RUN apt-get update && apt-get install -y --no-install-recommends \
        libssl3 \
    && rm -rf /var/lib/apt/lists/*

COPY --from=libvncserver-build /tmp/libvncserver/examples/vncev /usr/local/bin/vncev
COPY --from=libvncserver-build /tmp/libvncserver/libvncserver.so* /usr/local/lib/
RUN ldconfig

ENTRYPOINT ["vncev", "-rfbport", "5900", "-rfbwait", "1000"]


# Self-animated canvas, so it draws its own content rather than needing
# draw-content.sh to put an X client on the screen.
FROM base AS libvncserver-example

RUN apt-get update && apt-get install -y --no-install-recommends \
        libssl3 \
    && rm -rf /var/lib/apt/lists/*

COPY --from=libvncserver-build /tmp/libvncserver/examples/example /usr/local/bin/example
COPY --from=libvncserver-build /tmp/libvncserver/libvncserver.so* /usr/local/lib/
RUN ldconfig

ENTRYPOINT ["example", "-rfbport", "5900", "-rfbwait", "1000"]


FROM base AS qemu

RUN apt-get update && apt-get install -y --no-install-recommends \
        qemu-system-x86 \
        ovmf \
        openssl \
    && rm -rf /var/lib/apt/lists/*

COPY servers/qemu-entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]


FROM aerokube/selenoid:1.11.3 AS selenoid-dist

FROM base AS selenoid

RUN apt-get update && apt-get install -y --no-install-recommends \
        tigervnc-standalone-server \
        tigervnc-common \
    && rm -rf /var/lib/apt/lists/*

COPY --from=selenoid-dist /usr/bin/selenoid /usr/local/bin/selenoid

COPY servers/selenoid-browsers.json /etc/selenoid/browsers.json
COPY servers/selenoid-stub-webdriver.py /usr/local/bin/selenoid-stub-webdriver.py
COPY servers/selenoid-entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh /usr/local/bin/selenoid-stub-webdriver.py

# 5900 here is only the Xvnc behind the bridge, so the base healthcheck would
# pass before Selenoid is listening on 4444.
HEALTHCHECK --interval=2s --timeout=2s --start-period=5s --retries=30 \
    CMD ss -H -ltn 'sport = :4444' | grep -q . \
        && ss -H -ltn 'sport = :5900' | grep -q . \
        && pgrep -f "[s]cene_player" >/dev/null

ENTRYPOINT ["/entrypoint.sh"]


FROM base AS kasmvnc

ARG KASMVNC_VERSION=1.5.0
ARG TARGETARCH

RUN apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates \
        curl \
        openssl \
        xauth \
        xterm \
    && curl -fL -o /tmp/kasmvnc.deb \
        "https://github.com/kasmtech/KasmVNC/releases/download/v${KASMVNC_VERSION}/kasmvncserver_bookworm_${KASMVNC_VERSION}_${TARGETARCH}.deb" \
    && apt-get install -y --no-install-recommends /tmp/kasmvnc.deb \
    && rm -f /tmp/kasmvnc.deb \
    && rm -rf /var/lib/apt/lists/*

COPY servers/kasmvnc-entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

HEALTHCHECK --interval=2s --timeout=2s --start-period=5s --retries=30 \
    CMD ss -H -ltn 'sport = :5900' | grep -q . && pgrep -f "[s]cene_player" >/dev/null

ENTRYPOINT ["/entrypoint.sh"]


# wayvnc drives wlroots compositors only -- not Gnome, KDE or Weston -- so
# sway supplies one. Debian's build links gnutls, without which wayvnc offers
# no VeNCrypt at all.
FROM base AS wayvnc

RUN apt-get update && apt-get install -y --no-install-recommends \
        wayvnc \
        sway \
        xwayland \
        openssl \
    && rm -rf /var/lib/apt/lists/*

COPY servers/wayvnc-entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
