#!/bin/zsh
# Builds static, LGPL ffmpeg + ffprobe for arm64 macOS (bundled into UtilgruX.app by build.sh).
# Codecs: every FFmpeg decoder; encoders: native ones + VideoToolbox (H.264/HEVC/ProRes), AudioToolbox (AAC/ALAC),
# libmp3lame (MP3), libopus (Opus), libvpx (VP8/VP9 for WebM). No GPL parts (no x264/x265).
# Needs pkg-config: uses Vendor/tools/bin/pkg-config (pkgconf built from source) when present, else brew's.
# Every download is checked against a pinned SHA-256 (cross-checked with upstream / Homebrew); a mismatch stops the build.
# FFmpeg 9.1 (2026-11-20) carries the 2026-11-09 CVE batch fixes that the old Vendor/patches/ffmpeg-9.0.2-security-2026-11.patch
# added (checked: the patch reverse-applies on 9.1; the 5 hunks that don't are the same fixes in upstream's final form, or
# Linux-only v4l2), so no patch any more. Encoders and bitstream filters no app uses are still left out.
# Hardened: --disable-network (UtilgruX only feeds ffmpeg local files; stops crafted playlists from fetching URLs) and no playlist demuxers (hls, dash, concat, imf: they open other files a crafted one names). Output: Vendor/ffmpeg/bin/{ffmpeg,ffprobe} + Vendor/ffmpeg/LICENSE-*.txt. Sources are downloaded into Vendor/src.
set -euo pipefail
cd "$(dirname "$0")"
V=$PWD; SRC=$V/src; PREFIX=$V/deps; OUT=$V/ffmpeg
FFMPEG=ffmpeg-9.1; LAME=lame-4.0; OPUS=opus-1.6.1; VPX=1.17.0
# Bump a version → update its hash here (2026-10-22: libvpx matches Homebrew, opus matches xiph SHA256SUMS, lame matches Homebrew;
# 2026-11-20: ffmpeg-9.1.tar.xz = every source file identical to tag n9.1 on GitHub, n9.1 -> 74e43935 on git.ffmpeg.org and GitHub).
typeset -A SHA=(
  ffmpeg-9.1.tar.xz     1a4d394ae63d190a526cd052eb4415c156b5c5e2df37efab41a7de0b8fde2433
  lame-4.0.tar.gz       3df5124d5ad3a98312ffd7ba6a9b36230e4f8a3e66d3ce0f425e336c32d216eb
  opus-1.6.1.tar.gz     6ffcb593207be92584df15b32466ed64bbec99109f007c82205f0194572411a1
  libvpx-1.17.0.tar.gz  1020f184046187baa2985dbde38e0691f49c44088bca7a1842b0236c6081dc0a
)
# Encoders / bitstream filters no app uses (Converter outputs: Tools/check-ffmpeg.sh checks they stay out).
UNUSED_ENC=apng,exr,huffyuv,ffvhuff,nellymoser,dvbsub,dvdsub
UNUSED_BSF=h264_metadata,hevc_metadata,filter_units,h264_redundant_pps
[[ -x $V/tools/bin/pkg-config ]] && export PATH=$V/tools/bin:$PATH
export MACOSX_DEPLOYMENT_TARGET=15.0 CFLAGS="-O2 -arch arm64 -mmacosx-version-min=15.0" LDFLAGS="-arch arm64 -mmacosx-version-min=15.0"
JOBS=$(sysctl -n hw.ncpu)
mkdir -p $SRC $PREFIX $OUT/bin
fetch() {
  [[ -f $SRC/$2 ]] || curl -fL --proto '=https' --retry 3 -o $SRC/$2 "$1"
  echo "${SHA[$2]}  $SRC/$2" | shasum -a 256 -c - || { echo "✋ $2 does not match its pinned SHA-256"; rm -f $SRC/$2; exit 1; }
}
fetch https://ffmpeg.org/releases/$FFMPEG.tar.xz $FFMPEG.tar.xz
fetch https://downloads.sourceforge.net/project/lame/lame/4.0/$LAME.tar.gz $LAME.tar.gz
fetch https://downloads.xiph.org/releases/opus/$OPUS.tar.gz $OPUS.tar.gz
fetch https://github.com/webmproject/libvpx/archive/refs/tags/v$VPX.tar.gz libvpx-$VPX.tar.gz
cd $SRC
# FFmpeg: always a fresh extract, so an edited tree never gets built.
rm -rf $SRC/$FFMPEG; tar xf $FFMPEG.tar.xz
for t in $LAME.tar.gz $OPUS.tar.gz libvpx-$VPX.tar.gz; do d=${t%.tar.*}; [[ -d $d ]] || tar xf $t; done

if [[ ! -f $PREFIX/lib/libmp3lame.a || "$(cat $PREFIX/.lame 2>/dev/null)" != $LAME ]]; then
  cd $SRC/$LAME
  ./configure --prefix=$PREFIX --disable-shared --enable-static --disable-frontend --disable-decoder --disable-dependency-tracking
  make -j$JOBS && make install && echo $LAME > $PREFIX/.lame
fi
if [[ ! -f $PREFIX/lib/libopus.a || "$(cat $PREFIX/.opus 2>/dev/null)" != $OPUS ]]; then
  cd $SRC/$OPUS
  ./configure --prefix=$PREFIX --disable-shared --enable-static --disable-doc --disable-extra-programs --disable-dependency-tracking
  make -j$JOBS && make install && echo $OPUS > $PREFIX/.opus
fi
if [[ ! -f $PREFIX/lib/libvpx.a || "$(cat $PREFIX/.vpx 2>/dev/null)" != $VPX ]]; then
  cd $SRC/libvpx-$VPX
  # Newest Darwin target libvpx 1.17 knows; the arm64 code is the same on later macOS.
  T=arm64-darwin25-gcc
  make distclean >/dev/null 2>&1 || true
  ./configure --prefix=$PREFIX --target=$T --disable-shared --enable-static --disable-examples --disable-tools --disable-docs --disable-unit-tests --enable-vp9-highbitdepth
  make -j$JOBS && make install && echo $VPX > $PREFIX/.vpx
fi

cd $SRC/$FFMPEG
make distclean >/dev/null 2>&1 || true
PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig ./configure --prefix=$V/ffmpeg-install --arch=arm64 --cc=clang \
  --enable-static --disable-shared --disable-doc --disable-ffplay --disable-debug \
  --disable-network --disable-demuxer=hls,dash,concat,imf --disable-encoder=$UNUSED_ENC --disable-bsf=$UNUSED_BSF --disable-sdl2 --disable-xlib --disable-libxcb --disable-securetransport --disable-iconv --disable-lzma --disable-bzlib \
  --enable-videotoolbox --enable-audiotoolbox \
  --enable-libmp3lame --enable-libopus --enable-libvpx \
  --pkg-config=pkg-config --pkg-config-flags=--static \
  --extra-cflags="-I$PREFIX/include $CFLAGS" --extra-ldflags="-L$PREFIX/lib $LDFLAGS" \
  --extra-libs="-lmp3lame"
make -j$JOBS
cp ffmpeg ffprobe $OUT/bin/
strip -x $OUT/bin/ffmpeg $OUT/bin/ffprobe
cp COPYING.LGPLv2.1 $OUT/LICENSE-FFmpeg-LGPL.txt
cp $SRC/$LAME/COPYING $OUT/LICENSE-LAME.txt; cp $SRC/$OPUS/COPYING $OUT/LICENSE-Opus.txt; cp $SRC/libvpx-$VPX/LICENSE $OUT/LICENSE-libvpx.txt
otool -L $OUT/bin/ffmpeg
ls -la $OUT/bin
