# Wild Dragon Playout sidecar — CasparCG Server + Node AMCP control shim.
#
# CasparCG's mixer needs an OpenGL context. On a node with a real GPU we'd pass
# the device + driver through; for the headless / no-GPU case we run a virtual
# framebuffer (Xvfb) so the GL context initialises. The container is launched
# --privileged by mam-api (same as capture) so DeckLink / NDI hardware is
# reachable when present.
#
# CasparCG 2.4.x no longer ships a self-contained Linux tarball — the GitHub
# release provides either Ubuntu .deb packages or an "ubuntu22" zip that bundles
# the binary + its .so files under bin/ and lib/. We use the zip on an
# ubuntu:22.04 base so the bundled libs match the host glibc/abi, then install
# Node 20 from NodeSource on top.
#
# NDI + DeckLink SDKs are NOT redistributable. They are fetched at build time
# from a URL supplied as a build arg (mirror it into your own artifact store);
# the build still succeeds without it (NDI/DeckLink consumers simply won't be
# available — SRT/RTMP/test output still work).

FROM ubuntu:22.04

ARG CASPAR_VERSION=2.4.0-stable
ARG CASPAR_URL=https://github.com/CasparCG/server/releases/download/v2.4.0-stable/casparcg-server-v2.4.0-stable-ubuntu22.zip
ARG NDI_SDK_URL=

ENV DEBIAN_FRONTEND=noninteractive

# CasparCG 2.4 runtime deps + Xvfb for headless GL + CEF (HTML producer) deps +
# Node 20 (NodeSource) + a STANDALONE ffmpeg CLI. The standalone ffmpeg is what
# the Node shim spawns to re-mux the CasparCG mpegts preview stream into clean,
# video-only HLS (CasparCG's own FFMPEG consumer silently drops -an and muxes a
# broken audio track, which black-screens the browser preview).
RUN apt-get update && apt-get install -y --no-install-recommends \
      ca-certificates curl unzip tar xz-utils gnupg ffmpeg \
      xvfb libgl1-mesa-dri libglu1-mesa fonts-dejavu-core \
      libnss3 libnspr4 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 \
      libxkbcommon0 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 \
      libgbm1 libpango-1.0-0 libcairo2 libasound2 libatspi2.0-0 \
  && mkdir -p /etc/apt/keyrings \
  && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key \
       | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
  && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" \
       > /etc/apt/sources.list.d/nodesource.list \
  && apt-get update && apt-get install -y --no-install-recommends nodejs \
  && rm -rf /var/lib/apt/lists/*

# ── CasparCG Server (ubuntu22 zip bundle) ────────────────────────────────────
# The zip extracts to /opt/casparcg_server with the binary at bin/casparcg and
# its bundled .so files under lib/ (added to LD_LIBRARY_PATH by entrypoint.sh).
# Symlink to /opt/casparcg so the config/entrypoint paths stay stable.
WORKDIR /tmp/caspar
RUN set -eux; \
    curl -fsSL "$CASPAR_URL" -o caspar.zip; \
    unzip -q caspar.zip -d /opt; \
    chmod +x /opt/casparcg_server/bin/casparcg /opt/casparcg_server/scanner 2>/dev/null || true; \
    ls /opt/casparcg_server/; \
    test -x /opt/casparcg_server/bin/casparcg; \
    ln -sfn /opt/casparcg_server /opt/casparcg; \
    echo "caspar binary: /opt/casparcg_server/bin/casparcg"; \
    cd /; rm -rf /tmp/caspar

# ── NDI runtime (optional) ───────────────────────────────────────────────────
# If an NDI SDK tarball URL is provided, extract its libs to /opt/ndi-lib and
# point CasparCG at them via NDI_RUNTIME_DIR_V6. Pin the SDK version to what the
# server expects (the common docker failure is a libndi .so version mismatch).
RUN if [ -n "$NDI_SDK_URL" ]; then \
      mkdir -p /opt/ndi-lib && \
      curl -fsSL "$NDI_SDK_URL" -o /tmp/ndi.tar.gz && \
      tar xzf /tmp/ndi.tar.gz -C /tmp && \
      find /tmp -name 'libndi*.so*' -exec cp -a {} /opt/ndi-lib/ \; && \
      rm -f /tmp/ndi.tar.gz && ldconfig /opt/ndi-lib || true; \
    fi
ENV NDI_RUNTIME_DIR_V6=/opt/ndi-lib

# CasparCG media folder — mam-api stages assets from S3 into this volume.
RUN mkdir -p /media

# ── Node control shim ────────────────────────────────────────────────────────
WORKDIR /app
COPY package*.json ./
RUN npm install --omit=dev
COPY . .

# CasparCG config + entrypoint
COPY casparcg.config /opt/casparcg/casparcg.config
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

EXPOSE 3002 5250
ENTRYPOINT ["/entrypoint.sh"]
