# syntax = docker/dockerfile:experimental
FROM debian:bookworm-slim AS builder

ARG TARGETARCH

# 设置环境变量
ENV DEBIAN_FRONTEND=noninteractive \
    PIP_NO_CACHE_DIR=1 \
    RUSTUP_DIST_SERVER="https://mirrors.tuna.tsinghua.edu.cn/rustup"

# 更新源并安装依赖
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/' /etc/apt/sources.list.d/debian.sources \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
        python3-full \
        python3-pip \
        python3-dev \
        build-essential \
        libssl-dev \
        libffi-dev \
        python3-dev \
        libevent-dev \
        libjpeg-dev \
        libbsd-dev \
        libudev-dev \
        git \
        pkg-config \
        wget \
        curl \
        libmicrohttpd-dev \
        libjansson-dev \
        libsofia-sip-ua-dev \
        libglib2.0-dev \
        libopus-dev \
        libogg-dev \
        libcurl4-openssl-dev \
        liblua5.3-dev \
        libconfig-dev \
        libtool \
        automake \
        autoconf \
        meson \
        cmake \
        libx264-dev \
        libyuv-dev \
        libasound2-dev \
        libspeex-dev \
        libspeexdsp-dev \
        libusb-1.0-0-dev \
	&& apt clean \
    && rm -rf /var/lib/apt/lists/*

COPY build/cargo_config /tmp/config

# 配置 pip 源并安装 Python 依赖
RUN --security=insecure pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple \
    && if [ ${TARGETARCH} = arm ]; then \
        mkdir -p /root/.cargo \
        && chmod 777 /root/.cargo && mount -t tmpfs none /root/.cargo \
        && wget https://sh.rustup.rs -O /root/rustup-init.sh \
        && sh /root/rustup-init.sh -y \
        && export PATH=$PATH:/root/.cargo/bin \
        && cp /tmp/config /root/.cargo/config.toml; \
    fi \
    && pip install --root-user-action=ignore --disable-pip-version-check --upgrade --break-system-packages build setuptools pip \
    && pip wheel --wheel-dir=/tmp/wheel/ cryptography \
    && pip wheel --wheel-dir=/tmp/wheel/ \
        aiofiles aiohttp appdirs asn1crypto async_lru async-timeout bottle cffi \
        chardet click colorama dbus_next gpiod hidapi idna mako marshmallow \
        more-itertools multidict netifaces packaging passlib pillow ply psutil \
        pycparser pyelftools pyghmi pygments pyparsing pyotp qrcode requests \
        semantic-version setproctitle six spidev tabulate urllib3 wrapt xlib \
        yarl pyserial pyyaml zstandard supervisor pyfatfs

# 编译安装 libnice、libsrtp、libwebsockets 和 janus-gateway
RUN git clone --depth=1 https://gitlab.freedesktop.org/libnice/libnice /tmp/libnice \
    && cd /tmp/libnice \
    && meson --prefix=/usr build && ninja -C build && ninja -C build install \
    && rm -rf /tmp/libnice \
    && curl https://github.com/cisco/libsrtp/archive/v2.2.0.tar.gz -L -o /tmp/libsrtp-2.2.0.tar.gz \
    && cd /tmp \
    && tar xf libsrtp-2.2.0.tar.gz \
    && cd libsrtp-2.2.0 \
    && ./configure --prefix=/usr --enable-openssl \
    && make shared_library && make install \
    && cd /tmp \
    && rm -rf /tmp/libsrtp* \
    && git clone --depth=1 https://libwebsockets.org/repo/libwebsockets /tmp/libwebsockets \
    && cd /tmp/libwebsockets \
    && mkdir build && cd build \
    && cmake -DLWS_MAX_SMP=1 -DLWS_WITHOUT_EXTENSIONS=0 -DCMAKE_INSTALL_PREFIX:PATH=/usr -DCMAKE_C_FLAGS="-fpic" .. \
    && make && make install \
    && cd /tmp \
    && rm -rf /tmp/libwebsockets \
    && git clone --depth=1 https://github.com/meetecho/janus-gateway.git /tmp/janus-gateway \
    && cd /tmp/janus-gateway \
    && sh autogen.sh \
    && ./configure --enable-static --enable-websockets --enable-plugin-audiobridge \
        --disable-data-channels --disable-rabbitmq --disable-mqtt --disable-all-plugins \
        --disable-all-loggers --prefix=/usr \
    && make && make install \
    && cd /tmp \
    && rm -rf /tmp/janus-gateway

# 编译 ustreamer
RUN sed --in-place --expression 's|^#include "refcount.h"$|#include "../refcount.h"|g' /usr/include/janus/plugins/plugin.h \
    && git clone --depth=1 https://github.com/mofeng-git/ustreamer /tmp/ustreamer \
    && sed -i '68s/-Wl,-Bstatic//' /tmp/ustreamer/src/Makefile \
    && make -j WITH_PYTHON=1 WITH_JANUS=1 WITH_LIBX264=1 -C /tmp/ustreamer \
    && /tmp/ustreamer/ustreamer -v \
    && cp /tmp/ustreamer/python/dist/*.whl /tmp/wheel/

# 复制必要的库文件
RUN mkdir /tmp/lib \
    && cd /lib/*-linux-*/ \
    && cp libevent_core-*.so.7 libbsd.so.0 libevent_pthreads-*.so.7 libspeexdsp.so.1 \
       libevent-*.so.7 libjpeg.so.62 libx264.so.164 libyuv.so.0 libnice.so.10 \
       /usr/lib/libsrtp2.so.1 /usr/lib/libwebsockets.so.19 \
       /tmp/lib/