#!/bin/bash
# FFmpeg for the macOS release (spec 2026-10-06-macos-app-design.md section 5): arm64, no
# third-party dylibs, minos 15.0, LGPL (VideoToolbox, AudioToolbox and libopus; no libx264, which
# never runs on Apple Silicon). Built from the same sources the Windows download publishes as its
# complete corresponding source, so both apps run the same FFmpeg:
#   FFmpeg-46d8f462eeb87ee1f704d8c44a0ee24fca471ad1.tar.gz   (n9.0.2-22-g46d8f462ee)
#   50-libopus_a188c018...4252.tar.xz                         (xiph/opus 503d81b, BtbN's packing)
# from https://live.rafaelh9six.com/download/ffmpeg/source/ (put them in tools/ffmpeg-macos/src/).
#
# Usage: bash app-macos/ffmpeg/build.sh   (from the repo root)
# Output: tools/ffmpeg-macos/out/bin/ffmpeg, signed ad hoc and checked.
set -euo pipefail
root=$(cd "$(dirname "$0")/../.." && pwd)
work=$root/tools/ffmpeg-macos
src=$work/src
ffmpeg_tar=$src/FFmpeg-46d8f462eeb87ee1f704d8c44a0ee24fca471ad1.tar.gz
opus_tar=$src/50-libopus_a188c018d9bc2b808f76091aaf419e1c910d45c8e477f131ff5c25cd614b4252.tar.xz
echo "56999ad7cdcfbe520e989e38ac763341fab71b367ef8c20434117f8d1b7204a9  $ffmpeg_tar" | shasum -a 256 -c -
echo "db718781b89363fee1605936bea45b51fcd00c15c2f1e0f432620885b49ba1b9  $opus_tar" | shasum -a 256 -c -

jobs=$(sysctl -n hw.ncpu)
min=-mmacosx-version-min=15.0
prefix=$work/prefix
rm -rf "$work/build" "$prefix" "$work/out"
mkdir -p "$work/build/opus"

# libopus, static, as BtbN's 50-libopus.sh: autoreconf first (the packed configure lacks the
# autotools auxiliary files, and its aclocal.m4 and autom4te.cache were made for libtool m4 files
# that are not packed). Needs
# autoconf, automake and libtool from Homebrew, which names libtoolize glibtoolize.
tar -xJf "$opus_tar" -C "$work/build/opus"
(
  cd "$work/build/opus"
  rm -rf aclocal.m4 autom4te.cache
  # BtbN's m4/ holds symlinks into its Linux container's /usr/share/aclocal: dangling here.
  find m4 -type l -delete
  LIBTOOLIZE=glibtoolize autoreconf -isf
  ./configure --prefix="$prefix" --disable-shared --enable-static --disable-extra-programs --disable-doc \
    CFLAGS="-O2 $min" LDFLAGS="$min"
  make -j"$jobs"
  make install
)

# FFmpeg. --disable-autodetect keeps configure from linking Homebrew's libraries (the binary would
# break on any other Mac); what the app needs is then enabled by name. PKG_CONFIG_LIBDIR (not
# _PATH) makes pkg-config see only the local opus.
tar -xzf "$ffmpeg_tar" -C "$work/build"
(
  cd "$work/build/FFmpeg-46d8f462eeb87ee1f704d8c44a0ee24fca471ad1"
  # Without it, version.sh runs `git describe` and finds this repo's commit, not FFmpeg's.
  echo n9.0.2-22-g46d8f462ee > VERSION
  PKG_CONFIG_LIBDIR=$prefix/lib/pkgconfig ./configure --prefix="$work/out" \
    --disable-autodetect --enable-videotoolbox --enable-audiotoolbox --enable-libopus \
    --enable-static --disable-shared --disable-ffplay --disable-ffprobe --disable-doc --disable-debug \
    --pkg-config-flags=--static --extra-cflags="$min" --extra-ldflags="$min" --extra-version=screen-share-macos
  make -j"$jobs"
  make install
)

bin=$work/out/bin/ffmpeg
codesign -s - --force "$bin"
echo "== linked libraries (only /usr/lib and /System allowed)"
otool -L "$bin" | tail -n +2
if otool -L "$bin" | tail -n +2 | grep -v -E '^\s*(/usr/lib/|/System/)'; then
  echo "FAIL: third-party library linked"; exit 1
fi
echo "== minos"
vtool -show-build "$bin" | grep -E 'minos|platform'
echo "== encoders"
"$bin" -hide_banner -encoders | grep -E 'videotoolbox|libopus'
echo "== license"
"$bin" -hide_banner -L | head -3
shasum -a 256 "$bin"
