#!/usr/bin/env bash
set -euo pipefail

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
source_root="$repo_root/build/dependencies-m1/source/ffmpeg"
build_root="$repo_root/build/dependencies-m1/ffmpeg"
install_root="$repo_root/artifacts/m1"
log_root="$repo_root/build/dependencies-m1/logs"
mkdir -p "$build_root" "$install_root" "$log_root"

command -v make >/dev/null || { echo "make is required" >&2; exit 2; }
command -v nasm >/dev/null || { echo "nasm is required" >&2; exit 2; }
command -v pkg-config >/dev/null || { echo "pkg-config is required" >&2; exit 2; }

export PATH="$install_root/bin:$PATH"
export PKG_CONFIG_PATH="$install_root/lib/pkgconfig"

# A major FFmpeg upgrade installs new versioned DLL names but does not remove
# the previous ABI. Keep the staged runtime unambiguous so CMake and release
# packaging cannot accidentally ship both FFmpeg 8 and FFmpeg 9.
rm -f "$install_root/bin/avcodec-62.dll" \
      "$install_root/bin/avdevice-62.dll" \
      "$install_root/bin/avfilter-11.dll" \
      "$install_root/bin/avformat-62.dll" \
      "$install_root/bin/avutil-60.dll" \
      "$install_root/bin/swresample-6.dll" \
      "$install_root/bin/swscale-9.dll"

cd "$source_root"
scale_patch="$repo_root/dependencies/patches/ffmpeg-scale-d3d11-preview.patch"
if grep -q 'D3D11_BIND_RENDER_TARGET | D3D11_BIND_VIDEO_ENCODER' libavfilter/vf_scale_d3d11.c; then
  patch -p1 < "$scale_patch"
fi
make distclean >/dev/null 2>&1 || true
configure_args=(
  --prefix="$install_root"
  --target-os=mingw32
  --arch=x86_64
  --cc=x86_64-w64-mingw32-gcc
  --cxx=x86_64-w64-mingw32-g++
  --ar=ar
  --ranlib=ranlib
  --enable-shared
  --disable-static
  --enable-gpl
  --disable-nonfree
  --disable-debug
  --disable-doc
  --disable-programs
  --disable-autodetect
  --enable-w32threads
  --enable-d3d11va
  --enable-vulkan
  --enable-hwaccel=av1_d3d11va,av1_d3d11va2,ffv1_vulkan,h264_d3d11va,h264_d3d11va2,hevc_d3d11va,hevc_d3d11va2,mpeg2_d3d11va,mpeg2_d3d11va2,prores_vulkan,prores_raw_vulkan,vp9_d3d11va,vp9_d3d11va2
  --enable-ffnvcodec
  --enable-nvenc
  --enable-amf
  --enable-libvpl
  --enable-libx264
  --enable-libx265
  --enable-libaom
  --enable-libvpx
  --enable-protocol=file
  --enable-demuxer=matroska,mov,mpegts,mpegvideo
  --enable-muxer=matroska
  --enable-parser=av1,ffv1,h264,hevc,mpeg4video,mpegvideo,prores,prores_raw,vp9
  --enable-decoder=av1,ffv1,vp9,libaom-av1,libvpx-vp9,mpeg2video,h264,hevc,prores,prores_raw,aac,ac3,eac3,mp2,mp3,flac,opus,pcm_s16le
  --enable-encoder=ffv1,ffv1_vulkan,prores_ks,prores_ks_vulkan,libaom-av1,libvpx-vp9,mpeg2video,h264_nvenc,hevc_nvenc,av1_nvenc,h264_amf,hevc_amf,av1_amf,h264_qsv,hevc_qsv,av1_qsv,mpeg2_qsv
  --enable-filter=buffer,buffersink,format,hwdownload,hwmap,hwupload,scale,scale_d3d11,scale_vulkan,bwdif,bwdif_vulkan,fps,setsar
  --extra-cflags=-I"$install_root/include"
  --extra-ldflags=-L"$install_root/lib"
)

printf '%s\n' "./configure ${configure_args[*]}" | tee "$log_root/ffmpeg-configure-command.txt"
./configure "${configure_args[@]}" 2>&1 | tee "$log_root/ffmpeg-configure.log"
make -j"${NUMBER_OF_PROCESSORS:-2}" 2>&1 | tee "$log_root/ffmpeg-build.log"
make install 2>&1 | tee "$log_root/ffmpeg-install.log"
if [[ -f /ucrt64/bin/libvpl-2.dll ]]; then
  cp -f /ucrt64/bin/libvpl-2.dll "$install_root/bin/"
fi

cp ffbuild/config.log "$build_root/config.log"
cp config.h "$build_root/config.h"
cp "$repo_root/dependencies/manifests/ffmpeg-windows-x64.configure.txt" "$build_root/configure-manifest.txt"

echo "FFmpeg build stage complete: $install_root"
