#!/usr/bin/env bash
set -euo pipefail
# Source: FFmpeg/FFmpeg n9.0.2, commit 946fcce07b6dcd0331c8cc609192aeff5e1924f8.
# No third-party codecs, network or GPL/nonfree options. Do not add autodetection.
# Run in a private MinGW-w64 UCRT64 environment, not the MSYS compiler runtime.
root="${1:?Pass the isolated build root containing source/}"
case "$root" in *' '*) echo 'Build root must not contain spaces' >&2; exit 2;; esac
test -f "$root/source/configure"
test "${MSYSTEM:-}" = UCRT64
mkdir -p "$root/build"
cd "$root/build"
"$root/source/configure" \
  --prefix=/levitas-ffmpeg \
  --target-os=mingw32 --arch=x86_64 --cc=gcc \
  --disable-autodetect --disable-everything --disable-network \
  --disable-gpl --disable-nonfree --disable-version3 \
  --disable-debug --disable-doc --disable-ffplay --disable-ffprobe \
  --disable-shared --enable-static --enable-ffmpeg \
  --enable-protocol=file,pipe \
  --enable-demuxer=wav,mp3 --enable-parser=mpegaudio \
  --enable-decoder=mp3,mp3float,pcm_u8,pcm_s8,pcm_s16le,pcm_s16be,pcm_s24le,pcm_s24be,pcm_s32le,pcm_s32be,pcm_s64le,pcm_f32le,pcm_f32be,pcm_f64le,pcm_f64be,pcm_alaw,pcm_mulaw,adpcm_ima_wav,adpcm_ms \
  --enable-encoder=pcm_s16le --enable-muxer=wav \
  --enable-filter=aresample,aformat,anull,abuffer,abuffersink \
  --extra-ldflags=-static
make -j4
./ffmpeg.exe -version
./ffmpeg.exe -L
"${MINGW_PREFIX}/bin/objdump.exe" -p ffmpeg.exe | grep 'DLL Name:'
if "${MINGW_PREFIX}/bin/objdump.exe" -p ffmpeg.exe | grep 'DLL Name:' | grep -Ei 'libwinpthread|libgcc|libstdc|msys-'; then
  echo 'Unexpected non-system runtime dependency; do not distribute.' >&2
  exit 1
fi
# Capture configuration and toolchain versions separately from the source.
gcc --version
make --version
nasm -v
pacman -Q
