FROM ubuntu:noble AS builder
ARG FFMPEG_VERSION="8.1"
ARG RCLONE_VER="latest"
ARG GO_VERSION="latest"
#ARG GO_CRYPTO_VERSION="v0.36.0"
#ARG GO_OAUTH2_VERSION="v0.27.0"
#ARG GO_NET_VERSION="v0.38.0"
#ARG GOLANG_JWT_V4_VERSION="v4.5.2"
#ARG GOLANG_JWT_V5_VERSION="v5.2.2"

USER root

#======================================
# Install build tools
#======================================
ARG TOOLS_DEPS="autoconf automake cmake libfreetype6 gcc build-essential libtool make nasm pkg-config zlib1g-dev numactl \
libnuma-dev yasm git curl jq wget ca-certificates \
libx11-dev libxcb1-dev libpulse-dev libasound2-dev"

RUN apt-get update -qqy \
    && apt-get upgrade -yq \
    && apt-get -qqy --no-install-recommends install ${TOOLS_DEPS} \
    && apt-get -qyy clean \
    && mkdir -p /usr/local/src

RUN if [ "${GO_VERSION}" = "latest" ]; then \
        GO_VERSION=$(curl -sk https://go.dev/dl/?mode=json | jq -r '.[0].version'); \
    fi \
    && curl -skLO https://go.dev/dl/${GO_VERSION}.linux-$(dpkg --print-architecture).tar.gz \
    && tar -xf ${GO_VERSION}.linux-$(dpkg --print-architecture).tar.gz -C /usr/local \
    && rm -rf ${GO_VERSION}.linux-$(dpkg --print-architecture).tar.gz* \
    && ln -sf /usr/local/go/bin/go /usr/bin/go \
    && go version

RUN cd /usr/local/src \
    && if [ "${RCLONE_VER}" = "latest" ]; then \
        RCLONE_VER=$(curl -sk https://api.github.com/repos/rclone/rclone/releases/latest | jq -r '.tag_name // empty'); \
        if [ -z "${RCLONE_VER}" ]; then \
            RCLONE_VER=$(git ls-remote --tags --refs https://github.com/rclone/rclone.git \
            | sed 's#.*refs/tags/##' | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1); \
        fi; \
    fi \
    && echo "Building rclone ${RCLONE_VER}" \
    && git clone -b ${RCLONE_VER} --single-branch --depth 1 https://github.com/rclone/rclone.git \
    && cd rclone \
    # Patch deps version in go.mod to fix CVEs
#    && sed -i "s|golang.org/x/crypto v.*|golang.org/x/crypto ${GO_CRYPTO_VERSION}|g" go.mod \
#    && sed -i "s|golang.org/x/oauth2 v.*|golang.org/x/oauth2 ${GO_OAUTH2_VERSION}|g" go.mod \
#    && sed -i "s|golang.org/x/net v.*|golang.org/x/net ${GO_NET_VERSION}|g" go.mod \
#    && sed -i "s|github.com/golang-jwt/jwt/v5 v.*|github.com/golang-jwt/jwt/v5 ${GOLANG_JWT_V5_VERSION}|g" go.mod \
#    && sed -i "s|github.com/golang-jwt/jwt/v4 v.*|github.com/golang-jwt/jwt/v4 ${GOLANG_JWT_V4_VERSION}|g" go.mod \
#    && go mod tidy \
    # Build rclone
    && make \
    && mv ~/go/bin/rclone /usr/local/bin/ \
    && rclone version

#======================================
# Install x264 from source
#======================================
RUN cd /usr/local/src \
    && git clone https://code.videolan.org/videolan/x264.git --filter=blob:none \
    && cd x264 \
    && ./configure --prefix="/usr/local" --enable-static \
    && make \
    && make install

#======================================
# Install FFmpeg from source
#======================================
RUN cd /usr/local/src \
    && git clone -b release/${FFMPEG_VERSION} --single-branch --depth 1 https://github.com/FFmpeg/FFmpeg.git \
    && cd FFmpeg \
    && rm -rf .git \
    && PKG_CONFIG_PATH="/usr/local/lib/pkgconfig" FFMPEG_VERSION=${FFMPEG_VERSION} ./configure \
    --prefix="/usr/local" \
    --extra-cflags="-I/usr/local/include" \
    --extra-ldflags="-L/usr/local/lib" \
    --pkg-config-flags="--static" \
    --enable-gpl \
    --enable-nonfree \
    --enable-libx264 \
    --enable-libxcb \
    --enable-libpulse \
    --enable-alsa \
    --enable-static \
    && make \
    && make install

# Final stage
FROM ubuntu:noble

USER root

COPY --from=builder /usr/local/bin/ffmpeg /usr/local/bin/ffmpeg
COPY --from=builder /usr/local/bin/rclone /usr/local/bin/rclone

RUN apt-get -qqy update \
    && apt-get -qqy --no-install-recommends install \
    libx11-6 libx11-xcb1 libxcb1 libpulse0 libasound2t64 \
    && apt-get -qqy update \
    && apt-get -yq upgrade \
    && rm -rf /var/lib/apt/lists/* /var/cache/apt/*

RUN ldd /usr/local/bin/ffmpeg \
    && ffmpeg -version \
    && rclone --version

USER 101