# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

ARG UBUNTU_RELEASE=22.04
FROM ubuntu:${UBUNTU_RELEASE}

# Install essential dependencies
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
        build-essential \
        ca-certificates \
        curl \
        git \
        vim && \
    rm -rf /var/lib/apt/lists/*

# Install build dependencies
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
        autopoint \
        autoconf \
        automake \
        autotools-dev \
        gettext \
        bison \
        flex \
        nasm \
        gtk-doc-tools \
        libgl-dev \
        libgles-dev \
        libglvnd-dev \
        libgudev-1.0-dev \
        libgirepository1.0-dev \
        libgtk2.0-dev \
        libtool-bin \
        libx11-xcb-dev \
        libxkbcommon-dev \
        wayland-protocols \
        libwayland-dev \
        libwayland-egl-backend-dev \
        libdrm-dev \
        libmp3lame-dev \
        libopus-dev \
        libpulse-dev \
        libwebrtc-audio-processing-dev \
        libsoup2.4-dev \
        libsoup-gnome2.4-dev \
        libsrtp2-dev \
        libssl-dev \
        libjpeg-dev \
        libopenjp2-7-dev \
        libwebp-dev \
        libx264-dev \
        libvpx-dev \
        libva-dev && \
    rm -rf /var/lib/apt/lists/*

# Install GST-Python dependencies, Meson, and Ninja
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
        python3-pip \
        python3-dev \
        python-gi-dev && \
    rm -rf /var/lib/apt/lists/* && \
    pip3 install meson ninja

# GStreamer monorepo build with prefix for standalone install
WORKDIR /src
ARG GSTREAMER_VERSION=1.22.6
RUN git clone -b "${GSTREAMER_VERSION}" --depth 1 "https://gitlab.freedesktop.org/gstreamer/gstreamer.git" && cd gstreamer && \
    mkdir -p /opt/gstreamer

# Use a multi-line RUN command to execute your script
RUN cd gstreamer/subprojects/gst-plugins-bad && \
    features_to_enable="webrtc dtls srtp sctp sctp-internal-usrsctp" && \
    all_gst_features=$(sed -rn \
        "/^/,/^#.*$/s;^option\\('([^']*)'.*type *: *'feature'.*;\\1;p" \
        "./meson_options.txt") && \
    gst_features_conf="" && \
    for feature in $all_gst_features ; do \
        if echo "$features_to_enable" | grep -w $feature > /dev/null; then \
            gst_features_conf="$gst_features_conf -Dgst-plugins-bad:$feature=enabled"; \
        else \
            gst_features_conf="$gst_features_conf -Dgst-plugins-bad:$feature=disabled"; \
        fi; \
    done && \
    cd /src/gstreamer/subprojects/gst-plugins-good && \
    features_to_enable="rtp rtpmanager ximagesrc vpx" && \
    all_gst_features=$(sed -rn \
        "/^/,/^#.*$/s;^option\\('([^']*)'.*type *: *'feature'.*;\\1;p" \
        "./meson_options.txt") && \
    for feature in $all_gst_features ; do \
        if echo "$features_to_enable" | grep -w $feature > /dev/null; then \
            gst_features_conf="$gst_features_conf -Dgst-plugins-good:$feature=enabled"; \
        else \
            gst_features_conf="$gst_features_conf -Dgst-plugins-good:$feature=disabled"; \
        fi; \
    done && \
    cd /src/gstreamer && \
    meson setup --prefix /opt/gstreamer -Dbuildtype=release -Db_lto=true \
    -Dexamples=disabled \
    -Dgst-examples=disabled \
    -Dtests=disabled \
    -Ddoc=disabled \
    -Dqt5=disabled \
    -Dorc=disabled \
    -Dgtk_doc=disabled \
    -Dges=disabled \
    -Dvaapi=disabled \
    -Dsharp=disabled \
    -Drs=disabled \
    -Dgood=enabled \
    -Dbad=enabled \
    -Dpython=enabled \
    -Dgpl=disabled \
    -Dugly=disabled \
    -Dlibav=disabled \
    -Dgst-plugins-base:vorbis=disabled \
    $gst_features_conf \
    builddir

RUN cd gstreamer && \
    ninja -C builddir -j "$(nproc)" && \
    meson install -C builddir

# Bundle build result to tarball
COPY config/gst-env /opt/gstreamer/
RUN cd /opt && tar -zcvf selkies-gstreamer-latest.tgz gstreamer
