# Portions of this Dockerfile are sourced from GPLv3 licensed `yt-dlp slim` by Henrique Almeida (https://github.com/h3nc4/yt-dlp-slim)
# Derivations to this Dockerfile in this repository following March 3, 2026 should be considered licensed under this project's MIT license (see ../LICENSE) unless otherwise stated

########################################
# Versions
ARG YT_DLP_VERSION="2026.03.17"

################################################################################
# Deno builder stage
FROM denoland/deno:bin-2.6.6@sha256:9f18d20207f2699595ea26d14e0b7e123cd0cd01100a577bc11f8ca5906c2d81 AS deno-builder

################################################################################
# YT-DLP builder stage
FROM alpine:3.23@sha256:5b10f432ef3da1b8d4c7eb6c487f2f5a8f096bc91145e68878dd4a5019afde11 AS yt-dlp-builder
ARG YT_DLP_VERSION
ARG TARGETARCH

RUN mkdir -p /rootfs/target /rootfs/tmp /rootfs/bin

ADD "https://github.com/yt-dlp/yt-dlp/releases/download/${YT_DLP_VERSION}/SHA2-256SUMS" /SHA2-256SUMS
ADD "https://github.com/yt-dlp/yt-dlp/releases/download/${YT_DLP_VERSION}/SHA2-256SUMS.sig" /SHA2-256SUMS.sig
ADD "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xAC0CBBE6848D6A873464AF4E57CF65933B5A7581" "/yt-dlp_pubkey.asc"

RUN apk add --no-cache gnupg && \
  gpg --import /yt-dlp_pubkey.asc && \
  gpg --verify /SHA2-256SUMS.sig /SHA2-256SUMS

RUN case "$TARGETARCH" in \
    amd64) YT_DLP_FILE="yt-dlp_linux" ;; \
    arm64) YT_DLP_FILE="yt-dlp_linux_aarch64" ;; \
    *) echo "Unsupported architecture: $TARGETARCH" && exit 1 ;; \
  esac && \
  wget -qO "/${YT_DLP_FILE}" "https://github.com/yt-dlp/yt-dlp/releases/download/${YT_DLP_VERSION}/${YT_DLP_FILE}" && \
  grep " ${YT_DLP_FILE}$" /SHA2-256SUMS | sha256sum -c - && \
  mv "/${YT_DLP_FILE}" /rootfs/bin/yt-dlp && \
  chmod 755 /rootfs/bin/yt-dlp && \
  chmod 1777 /rootfs/tmp

################################################################################
# FFmpeg builder stage
FROM debian:13-slim@sha256:b6e2a152f22a40ff69d92cb397223c906017e1391a73c952b588e51af8883bf8 AS ffmpeg-builder
RUN apt-get update && \
  apt-get install -y --no-install-recommends ffmpeg
COPY --from=yt-dlp-builder /rootfs/bin/yt-dlp /yt-dlp
RUN mkdir -p /rootfs/bin && \
  cp /usr/bin/ffmpeg /usr/bin/ffprobe /rootfs/bin/ && \
  { ldd /usr/bin/ffmpeg; ldd /yt-dlp; } 2>/dev/null | \
  grep -o '/[^ ]*' | sort -u | \
  xargs -I '{}' cp --parents '{}' /rootfs && \
  LIBDIR=$(dirname "$(find /rootfs -name 'libc.so.6' | head -1)") && \
  for stub in libutil.so.1 libdl.so.2 libpthread.so.0 librt.so.1; do \
    [ -f "${LIBDIR}/${stub}" ] || ln -sf libc.so.6 "${LIBDIR}/${stub}"; \
  done

################################################################################
# App builder stage
FROM golang:1.26.3-trixie@sha256:a085df697019cb63b40a70f6a92b948f7dc9df96dfcb2c20ba6eed25ce28f5b3 AS app-builder

COPY app/ /opt/app
WORKDIR /opt/app

RUN go get && go build -o out/yt-dlp-bot

################################################################################
# Final squashed image
FROM scratch AS final

# Copy deno, yt-dlp, and ffmpeg binaries
COPY --from=deno-builder /deno /bin/deno
COPY --from=yt-dlp-builder /rootfs /
COPY --from=ffmpeg-builder /rootfs/ /

# Copy yt-dlp-bot app binary
COPY --from=app-builder /opt/app/out/yt-dlp-bot /bin/

# Copy SSL CA's (needed for Discord)
COPY --from=app-builder /etc/ssl/certs /etc/ssl/certs

WORKDIR /target
ENV XDG_CACHE_HOME=/tmp/.cache

ENV YTDLP_BIN=/bin/yt-dlp

ENTRYPOINT ["/bin/yt-dlp-bot"]

LABEL org.opencontainers.image.title="yt-dlp bot" \
  org.opencontainers.image.description="A totally overengineered Discord bot to locally download YouTube videos for private use" \
  org.opencontainers.image.authors="William Peebles <me@williamtpeebles.com>" \
  org.opencontainers.image.vendor="William Peebles" \
  org.opencontainers.image.licenses="MIT" \
  org.opencontainers.image.source="https://git.dubyatp.xyz/williamp/yt-dlp-bot"