# 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.
#
# NDI + DeckLink SDKs are NOT redistributable. They are fetched at build time
# from URLs supplied as build args (mirror them into your own artifact store);
# the build still succeeds without them (NDI/DeckLink consumers simply won't be
# available — SRT/RTMP/test output still work).

# CasparCG 2.5.0 ships Linux builds as Ubuntu .deb packages (noble = 24.04), not
# a portable tarball, so the base is ubuntu:24.04 and Node is added on top. The
# server deb pulls its own GL/ffmpeg/openal runtime deps via apt; we add Xvfb +
# mesa SW-GL drivers for the headless GL context.
FROM ubuntu:24.04

ARG CASPAR_VERSION=2.5.0-stable
# Server + matching CEF deb (HTML template renderer the server depends on). The
# %2B in the CEF filename is the URL-encoded '+' from the upstream asset name.
ARG CASPAR_SERVER_DEB_URL=https://github.com/CasparCG/server/releases/download/v2.5.0-stable/casparcg-server-2.5_2.5.0.stable-noble1_amd64.deb
ARG CASPAR_CEF_DEB_URL=https://github.com/CasparCG/server/releases/download/v2.5.0-stable/casparcg-cef-142_142.0.17.g60aac24%2B2-noble1_amd64.deb
ARG NDI_SDK_URL=

ENV DEBIAN_FRONTEND=noninteractive

# Base tools + Node 20 (nodesource) + Xvfb for headless GL + mesa SW renderer.
RUN apt-get update && apt-get install -y --no-install-recommends \
      ca-certificates curl gnupg tar xz-utils \
      xvfb libgl1-mesa-dri libglu1-mesa fonts-dejavu-core \
  && 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 2.5.0 (via .deb) ─────────────────────────────────────────
# apt resolves the server's runtime deps (GL, ffmpeg, openal, sndfile, …). The
# 2.5 deb installs the executable as /usr/bin/casparcg-server-2.5 and a default
# config under /usr/share/casparcg-server-2.5/. We discover the binary from the
# package file list (matching casparcg-server*, since the name is versioned),
# and symlink its dir to /opt/casparcg so the entrypoint + config stay stable.
WORKDIR /tmp/caspar
RUN set -eux; \
  curl -fsSL "$CASPAR_CEF_DEB_URL" -o cef.deb; \
  curl -fsSL "$CASPAR_SERVER_DEB_URL" -o server.deb; \
  apt-get update; \
  apt-get install -y --no-install-recommends ./cef.deb ./server.deb; \
  PKG=$(dpkg-deb -f server.deb Package); \
  echo "casparcg server package: $PKG"; \
  dpkg -L "$PKG"; \
  BIN=$(dpkg -L "$PKG" | while read -r f; do \
        [ -f "$f" ] && [ -x "$f" ] || continue; \
        case "$f" in /usr/bin/*casparcg*server*|*/casparcg|*/CasparCG\ Server) echo "$f";; esac; \
      done | head -1); \
  [ -n "$BIN" ] || BIN=/usr/bin/casparcg-server-2.5; \
  test -x "$BIN"; \
  echo "casparcg binary=$BIN"; \
  ln -sfn "$BIN" /usr/local/bin/casparcg; \
  rm -rf /opt/casparcg; mkdir -p /opt/casparcg; \
  cd /; rm -rf /tmp/caspar /var/lib/apt/lists/*

# ── 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"]
