#!/bin/bash
# Rushes — build the bundled FFmpeg/FFprobe sidecars as an LGPL-only, dependency-free
# static build for Apple Silicon (PRE-LAUNCH X6, 2026-10-13).
#
# Why: the previous sidecar was a GPL build (x264/x265 and other GPL libraries linked
# in). Rushes never uses them — it DECODES H.264/H.265 with FFmpeg's built-in decoders and
# ENCODES proxies with h264_videotoolbox — so an LGPL build loses nothing and reduces the
# redistribution obligation to FFmpeg's own source (this tarball) and licence (LGPL 2.1+).
#
# Source: the official release tarball, verified against the checksum Homebrew publishes
# for the same file (formulae.brew.sh/api/formula/ffmpeg@7.json).
# Needs only Xcode command-line tools (clang, make). No nasm/yasm/pkg-config needed on arm64.
# Output: ffmpeg-aarch64-apple-darwin and ffprobe-aarch64-apple-darwin in ./out, linking
# nothing outside /System/Library and /usr/lib.
set -euo pipefail
VERSION=7.1.5
SHA256=de668509caf9e35e3cd162473441fdb29538c6d96ed080292b3cf9e6fc5d558f
WORK=${1:-$(mktemp -d /tmp/ffmpeg-lgpl.XXXXXX)}
cd "$WORK"
[ -f ffmpeg-$VERSION.tar.xz ] || curl -sL -o ffmpeg-$VERSION.tar.xz https://ffmpeg.org/releases/ffmpeg-$VERSION.tar.xz
echo "$SHA256  ffmpeg-$VERSION.tar.xz" | shasum -a 256 -c -
rm -rf ffmpeg-$VERSION && tar xf ffmpeg-$VERSION.tar.xz && cd ffmpeg-$VERSION
./configure --prefix="$WORK/prefix" \
  --disable-autodetect --disable-gpl --disable-nonfree \
  --disable-shared --enable-static \
  --enable-videotoolbox --enable-audiotoolbox --enable-zlib --enable-bzlib \
  --disable-iconv --disable-sdl2 --disable-indevs --enable-indev=lavfi --disable-outdevs \
  --disable-network --disable-doc --disable-debug --disable-ffplay --enable-pthreads
make -j"$(sysctl -n hw.ncpu)"
make install
mkdir -p "$WORK/out"
cp "$WORK/prefix/bin/ffmpeg"  "$WORK/out/ffmpeg-aarch64-apple-darwin"
cp "$WORK/prefix/bin/ffprobe" "$WORK/out/ffprobe-aarch64-apple-darwin"
# Proof the build is self-contained and LGPL:
for b in "$WORK"/out/*; do
  if otool -L "$b" | tail -n +2 | awk '{print $1}' | grep -qv '^/System/Library\|^/usr/lib'; then
    echo "FAIL: $b links a non-system library"; exit 1; fi
done
"$WORK/out/ffmpeg-aarch64-apple-darwin" -L | grep -q 'Lesser General Public' || { echo "FAIL: not LGPL"; exit 1; }
echo "OK: $WORK/out"
shasum -a 256 "$WORK"/out/*
