# https://github.com/Jeeaaasus/youtube-dl/blob/master/Dockerfile based on, excluded services
FROM debian:12-slim

ENV PATH="/opt/yt-dlp-venv/bin:$PATH" \
    HOME="/config" \
    PUID="911" \
    PGID="911" \
    UMASK="022" \
    OPENSSL_CONF=

RUN set -x && \
    addgroup --gid "$PGID" abc && \
    adduser \
        --gecos "" \
        --disabled-password \
        --uid "$PUID" \
        --ingroup abc \
        --shell /bin/bash \
        abc

RUN set -x && \
    apt update && \
    apt install -y \
        brotli \
        file \
        wget \
        unzip \
        python3 \
        python3-venv \
        python3-pip && \
    apt clean && \
    python3 -m venv /opt/yt-dlp-venv && \
    rm -rf \
        /var/lib/apt/lists/* \
        /tmp/*

RUN set -x && \
    arch=`uname -m` && \
    if [ "$arch" = "x86_64" ]; then \
        wget -q 'https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz' -O - | tar -xJ -C /tmp/ --one-top-level=ffmpeg && \
        chmod -R a+x /tmp/ffmpeg/* && \
        mv $(find /tmp/ffmpeg/* -name ffmpeg) /usr/local/bin/ && \
        mv $(find /tmp/ffmpeg/* -name ffprobe) /usr/local/bin/ && \
        mv $(find /tmp/ffmpeg/* -name ffplay) /usr/local/bin/ && \
        rm -rf /tmp/* ; \
    else \
        if [ "$arch" = "aarch64" ]; then arch='arm64'; fi && \
        wget -q "https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-${arch}-static.tar.xz" -O - | tar -xJ -C /tmp/ --one-top-level=ffmpeg && \
        chmod -R a+x /tmp/ffmpeg/* && \
        mv $(find /tmp/ffmpeg/* -name ffmpeg) /usr/local/bin/ && \
        mv $(find /tmp/ffmpeg/* -name ffprobe) /usr/local/bin/ && \
        rm -rf /tmp/* ; \
    fi

RUN set -x && \
    arch=`uname -m` && \
    wget -q "https://github.com/denoland/deno/releases/latest/download/deno-${arch}-unknown-linux-gnu.zip" -O /tmp/deno.zip && \
    unzip /tmp/deno.zip -d /tmp/deno/ && \
    chmod -R a+x /tmp/deno/* && \
    mv $(find /tmp/deno/* -name deno) /usr/local/bin/ && \
    rm -rf /tmp/*

RUN set -x && \
    /opt/yt-dlp-venv/bin/pip --no-cache-dir install -U --pre yt-dlp[default] bgutil-ytdlp-pot-provider && \
    chmod -R a+rx /opt/yt-dlp-venv

VOLUME /config /downloads

WORKDIR /config

CMD ["yt-dlp", "--version"]
