#!/bin/bash
# Builds FFmpeg (LGPL-2.1-or-later) for the videos AVFoundation and WebKit can't open: the preview pane's MP4s and the
# content index's frames and sound. The pinned release, configured LGPL-only (no GPL, no nonfree, no network, nothing
# auto-detected from Homebrew), as one static, stripped executable for macOS 14+ on arm64:
# vendor/ffmpeg/ffmpeg-aarch64-apple-darwin (out of git; the name Tauri's externalBin wants). THIRD_PARTY.md repeats
# the version and these flags. Does nothing when that build is there.
set -euo pipefail
cd "$(dirname "$0")/.."
TAG=n9.0.2
COMMIT=946fcce07b6dcd0331c8cc609192aeff5e1924f8   # TAG's commit: a tag can be moved, a commit can't
SOURCE=vendor/ffmpeg-src
BUILD=vendor/ffmpeg-build
OUT=vendor/ffmpeg
FLAGS=(
  --disable-gpl --disable-nonfree --disable-version3 --disable-network --disable-autodetect
  --disable-doc --disable-ffplay --disable-ffprobe --disable-avdevice --disable-debug
  --enable-static --disable-shared --enable-videotoolbox --enable-audiotoolbox --enable-zlib --enable-bzlib
  # Every demuxer, decoder, parser and bitstream filter stays (the defaults). The rest is only what the app runs:
  # MP4 previews (H.264 via VideoToolbox, AAC), JPEG frames and raw 16-bit sound for the index. matroska, avi, asf, mpeg4,
  # wmv2, wmav2 and hevc_videotoolbox only make the test videos (a few hundred KB).
  --disable-encoders --enable-encoder=h264_videotoolbox,hevc_videotoolbox,aac,mjpeg,pcm_s16le,mpeg4,wmv2,wmav2
  --disable-muxers --enable-muxer=mp4,mov,ipod,pcm_s16le,image2,mjpeg,null,matroska,avi,asf
  --disable-filters --enable-filter=scale,fps,aresample,select,setsar,bwdif
  --disable-protocols --enable-protocol=file,pipe
  --arch=arm64 --cc=clang --pkg-config=false
  --extra-cflags=-mmacosx-version-min=14.0 --extra-ldflags=-mmacosx-version-min=14.0
)
STAMP="$TAG $(printf '%s\n' "${FLAGS[@]}" | shasum -a 256 | cut -c1-12)"
BIN="$OUT/ffmpeg-aarch64-apple-darwin"
if [[ -x "$BIN" && "$(cat "$OUT/BUILD" 2>/dev/null)" == "$STAMP" ]]; then
  exit 0
fi
if [[ "$(uname -m)" != arm64 ]]; then
  echo "build-ffmpeg.sh builds for arm64 Macs only" >&2
  exit 1
fi
if [[ -z "${DEVELOPER_DIR:-}" && -d /Applications/Xcode.app ]]; then
  export DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer
fi
# Nothing from Homebrew: headers, libraries and pkg-config files stay out of the build.
unset CPATH C_INCLUDE_PATH LIBRARY_PATH PKG_CONFIG_PATH
export MACOSX_DEPLOYMENT_TARGET=14.0
if [[ ! -d "$SOURCE" ]]; then
  git -c advice.detachedHead=false clone --quiet --depth 1 --branch "$TAG" https://git.ffmpeg.org/ffmpeg.git "$SOURCE"
fi
if [[ "$(git -C "$SOURCE" rev-parse HEAD)" != "$COMMIT" ]]; then
  echo "FFmpeg's $TAG isn't commit $COMMIT here: not building it" >&2
  exit 1
fi
rm -rf "$BUILD"
mkdir -p "$BUILD" "$OUT"
(cd "$BUILD" && ../ffmpeg-src/configure "${FLAGS[@]}" > configure.log && make -j "$(sysctl -n hw.ncpu)" ffmpeg > make.log)
if ! grep -q '^License: LGPL version 2.1 or later' "$BUILD/configure.log"; then
  echo "FFmpeg's configure didn't settle on LGPL 2.1+: not using this build" >&2
  exit 1
fi
cp "$BUILD/ffmpeg" "$BIN"
echo "$STAMP" > "$OUT/BUILD"
echo "Built FFmpeg $TAG (LGPL) into $BIN ($(du -h "$BIN" | cut -f1))"
