# 0001-pleple-lgpl-profiled-build.patch

diff --git a/Dockerfile b/Dockerfile
index 51275c4..fe2d300 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -134,71 +134,25 @@ RUN bash -x /src/build.sh
 
 # Base ffmpeg image with dependencies and source code populated.
 FROM emsdk-base AS ffmpeg-base
-RUN embuilder build sdl2 sdl2-mt
 ADD https://github.com/FFmpeg/FFmpeg.git#$FFMPEG_VERSION /src
-COPY --from=x264-builder $INSTALL_DIR $INSTALL_DIR
-COPY --from=x265-builder $INSTALL_DIR $INSTALL_DIR
 COPY --from=libvpx-builder $INSTALL_DIR $INSTALL_DIR
-COPY --from=lame-builder $INSTALL_DIR $INSTALL_DIR
-COPY --from=opus-builder $INSTALL_DIR $INSTALL_DIR
-COPY --from=theora-builder $INSTALL_DIR $INSTALL_DIR
-COPY --from=vorbis-builder $INSTALL_DIR $INSTALL_DIR
-COPY --from=libwebp-builder $INSTALL_DIR $INSTALL_DIR
-COPY --from=libass-builder $INSTALL_DIR $INSTALL_DIR
-COPY --from=zimg-builder $INSTALL_DIR $INSTALL_DIR
+COPY --from=zlib-builder $INSTALL_DIR $INSTALL_DIR
 
 # Build ffmpeg
 FROM ffmpeg-base AS ffmpeg-builder
+ARG PLEPLE_FFMPEG_CONFIGURE_FLAGS=""
 COPY build/ffmpeg.sh /src/build.sh
-RUN bash -x /src/build.sh \
-      --enable-gpl \
-      --enable-libx264 \
-      --enable-libx265 \
-      --enable-libvpx \
-      --enable-libmp3lame \
-      --enable-libtheora \
-      --enable-libvorbis \
-      --enable-libopus \
-      --enable-zlib \
-      --enable-libwebp \
-      --enable-libfreetype \
-      --enable-libfribidi \
-      --enable-libass \
-      --enable-libzimg 
+RUN bash -lc 'bash -x /src/build.sh ${PLEPLE_FFMPEG_CONFIGURE_FLAGS}'
 
 # Build ffmpeg.wasm
 FROM ffmpeg-builder AS ffmpeg-wasm-builder
+ARG PLEPLE_FFMPEG_LIBS="-lz"
 COPY src/bind /src/src/bind
 COPY src/fftools /src/src/fftools
 COPY build/ffmpeg-wasm.sh build.sh
 # libraries to link
-ENV FFMPEG_LIBS \
-      -lx264 \
-      -lx265 \
-      -lvpx \
-      -lmp3lame \
-      -logg \
-      -ltheora \
-      -lvorbis \
-      -lvorbisenc \
-      -lvorbisfile \
-      -lopus \
-      -lz \
-      -lwebpmux \
-      -lwebp \
-      -lsharpyuv \
-      -lfreetype \
-      -lfribidi \
-      -lharfbuzz \
-      -lass \
-      -lzimg
-RUN mkdir -p /src/dist/umd && bash -x /src/build.sh \
-      ${FFMPEG_LIBS} \
-      -o dist/umd/ffmpeg-core.js
-RUN mkdir -p /src/dist/esm && bash -x /src/build.sh \
-      ${FFMPEG_LIBS} \
-      -sEXPORT_ES6 \
-      -o dist/esm/ffmpeg-core.js
+RUN mkdir -p /src/dist/umd && bash -lc 'bash -x /src/build.sh ${PLEPLE_FFMPEG_LIBS} -o dist/umd/ffmpeg-core.js'
+RUN mkdir -p /src/dist/esm && bash -lc 'bash -x /src/build.sh ${PLEPLE_FFMPEG_LIBS} -sEXPORT_ES6 -o dist/esm/ffmpeg-core.js'
 
 # Export ffmpeg-core.wasm to dist/, use `docker buildx build -o . .` to get assets
 FROM scratch AS exportor
diff --git a/build/ffmpeg-wasm.sh b/build/ffmpeg-wasm.sh
index faa2f72..73a94f2 100755
--- a/build/ffmpeg-wasm.sh
+++ b/build/ffmpeg-wasm.sh
@@ -17,7 +17,6 @@ CONF_FLAGS=(
   -Llibavfilter 
   -Llibavformat 
   -Llibavutil 
-  -Llibpostproc 
   -Llibswresample 
   -Llibswscale 
   -lavcodec 
@@ -25,14 +24,12 @@ CONF_FLAGS=(
   -lavfilter 
   -lavformat 
   -lavutil 
-  -lpostproc 
   -lswresample 
   -lswscale 
   -Wno-deprecated-declarations 
   $LDFLAGS 
   -sENVIRONMENT=worker
   -sWASM_BIGINT                            # enable big int support
-  -sUSE_SDL=2                              # use emscripten SDL2 lib port
   -sSTACK_SIZE=5MB                         # increase stack size to support libopus
   -sMODULARIZE                             # modularized to use as a library
   ${FFMPEG_MT:+ -sINITIAL_MEMORY=1024MB}   # ALLOW_MEMORY_GROWTH is not recommended when using threads, thus we use a large initial memory

# 0002-record-executed-build-args.patch

diff --git a/Dockerfile b/Dockerfile
--- a/Dockerfile
+++ b/Dockerfile
@@ -139,19 +139,23 @@ COPY --from=zlib-builder $INSTALL_DIR $INSTALL_DIR
 
 # Build ffmpeg
 FROM ffmpeg-base AS ffmpeg-builder
 ARG PLEPLE_FFMPEG_CONFIGURE_FLAGS=""
 COPY build/ffmpeg.sh /src/build.sh
-RUN bash -lc 'bash -x /src/build.sh ${PLEPLE_FFMPEG_CONFIGURE_FLAGS}'
+RUN mkdir -p /src/dist/build-metadata && \
+      PLEPLE_EXECUTED_CONFIGURE_ARGS_FILE=/src/dist/build-metadata/executed-configure-args.txt \
+      bash -lc 'bash -x /src/build.sh ${PLEPLE_FFMPEG_CONFIGURE_FLAGS}'
 
 # Build ffmpeg.wasm
 FROM ffmpeg-builder AS ffmpeg-wasm-builder
 ARG PLEPLE_FFMPEG_LIBS="-lz"
 COPY src/bind /src/src/bind
 COPY src/fftools /src/src/fftools
 COPY build/ffmpeg-wasm.sh build.sh
 # libraries to link
 RUN mkdir -p /src/dist/umd && bash -lc 'bash -x /src/build.sh ${PLEPLE_FFMPEG_LIBS} -o dist/umd/ffmpeg-core.js'
-RUN mkdir -p /src/dist/esm && bash -lc 'bash -x /src/build.sh ${PLEPLE_FFMPEG_LIBS} -sEXPORT_ES6 -o dist/esm/ffmpeg-core.js'
+RUN mkdir -p /src/dist/build-metadata /src/dist/esm && \
+      PLEPLE_EXECUTED_LINK_ARGS_FILE=/src/dist/build-metadata/executed-link-args.txt \
+      bash -lc 'bash -x /src/build.sh ${PLEPLE_FFMPEG_LIBS} -sEXPORT_ES6 -o dist/esm/ffmpeg-core.js'
 
 # Export ffmpeg-core.wasm to dist/, use `docker buildx build -o . .` to get assets
 FROM scratch AS exportor
diff --git a/build/ffmpeg.sh b/build/ffmpeg.sh
--- a/build/ffmpeg.sh
+++ b/build/ffmpeg.sh
@@ -29,5 +29,12 @@ CONF_FLAGS=(
   ${FFMPEG_ST:+ --disable-pthreads --disable-w32threads --disable-os2threads}
 )
 
-emconfigure ./configure "${CONF_FLAGS[@]}" $@
+EXECUTED_CONFIGURE_ARGS_FILE="${PLEPLE_EXECUTED_CONFIGURE_ARGS_FILE:-}"
+if [ -n "$EXECUTED_CONFIGURE_ARGS_FILE" ]; then
+  mkdir -p "$(dirname "$EXECUTED_CONFIGURE_ARGS_FILE")"
+  executed_configure_args=("${CONF_FLAGS[@]}" "$@")
+  printf '%s\n' "${executed_configure_args[@]}" >"$EXECUTED_CONFIGURE_ARGS_FILE"
+fi
+
+emconfigure ./configure "${CONF_FLAGS[@]}" "$@"
 emmake make -j
diff --git a/build/ffmpeg-wasm.sh b/build/ffmpeg-wasm.sh
--- a/build/ffmpeg-wasm.sh
+++ b/build/ffmpeg-wasm.sh
@@ -51,4 +51,11 @@ CONF_FLAGS=(
   src/fftools/ffprobe.c 
 )
 
-emcc "${CONF_FLAGS[@]}" $@
+EXECUTED_LINK_ARGS_FILE="${PLEPLE_EXECUTED_LINK_ARGS_FILE:-}"
+if [ -n "$EXECUTED_LINK_ARGS_FILE" ]; then
+  mkdir -p "$(dirname "$EXECUTED_LINK_ARGS_FILE")"
+  executed_link_args=("${CONF_FLAGS[@]}" "$@")
+  printf '%s\n' "${executed_link_args[@]}" >"$EXECUTED_LINK_ARGS_FILE"
+fi
+
+emcc "${CONF_FLAGS[@]}" "$@"
