#!/bin/bash -ex
# Eval 1 Builder PB, 2026-10-05: LGPL FFmpeg n7.1 dylibs (arm64, macOS 13.0+) as a drop-in for the Homebrew
# FFmpeg 7.1_3 (GPL, --enable-gpl --enable-version3 + x264/x265) that opencv-contrib-python 4.11.0.86 bundles in cv2/.dylibs.
# Kept from Homebrew's line: --enable-shared --enable-pthreads --cc=clang --enable-videotoolbox --enable-audiotoolbox --enable-neon.
# Dropped: --enable-gpl, --enable-version3, every --enable-lib* external library (x264/x265 are GPL; the others are not
# needed to decode the scorer's H.264/VP8/VP9 takes, and their exact sources are outside the approved download list),
# ffplay/programs, avdevice/avfilter (cv2 never loads them). Clean PATH so no Homebrew tool or library is picked up.
# usage: pb_build_mac.sh <ffmpeg-n7.1 source tarball> <build dir> <install dir, NO spaces (it becomes the install name)> [jobs]
SRC_TAR="$1"; BLD="$2"; INST="$3"; JOBS="${4:-6}"
case "$INST$BLD" in *" "*) echo "build and install paths must not contain spaces"; exit 2;; esac
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
export MACOSX_DEPLOYMENT_TARGET=13.0
rm -rf "$BLD" "$INST"; mkdir -p "$BLD/src" "$INST"
tar -xJf "$SRC_TAR" -C "$BLD/src" --strip-components=1
cd "$BLD/src"
./configure --prefix="$INST" --enable-shared --disable-static --enable-pthreads --cc=clang \
  --enable-videotoolbox --enable-audiotoolbox --enable-neon \
  --disable-programs --disable-doc --disable-avdevice --disable-avfilter --disable-postproc \
  --disable-sdl2 --disable-xlib --disable-libxcb --disable-lzma \
  --extra-cflags="-mmacosx-version-min=13.0" --extra-ldflags="-mmacosx-version-min=13.0"
grep -E "#define (CONFIG_GPL|CONFIG_VERSION3|CONFIG_NONFREE|HAVE_NEON|CONFIG_VIDEOTOOLBOX) " config.h
make -j"$JOBS"
make install
ls -la "$INST/lib"
