#!/bin/bash
# Builds the bundled FFmpeg (Engine/bin/ffmpeg and ffprobe) from the pinned source archive.
# LGPL only (no GPL or non-free parts), no network protocols, no libvpx or libopus (Unravel never
# encodes VP8/VP9 or Opus; FFmpeg's own decoders read them), LAME 3.100 for MP3 export, dav1d 1.5.4
# (BSD-2-Clause) to decode AV1 video on Macs without AV1 hardware, and VideoToolbox/AudioToolbox.
# This script and the source archives are what the LGPL source offer (Engine/Licenses/SOURCE.txt)
# publishes.
#   - Verifies every archive against its SHA-256 (Engine/source-manifest.json), including the
#     meson and ninja packages that build dav1d; they go into a throwaway Python environment,
#     never onto the system. dav1d is built as a static library and linked into FFmpeg.
#   - Builds in a private temporary folder with build paths remapped, so no local folder name is
#     compiled into the binaries; installs only ffmpeg and ffprobe into Engine/bin (replacing
#     what is there), and removes the libvpx and libopus libraries from Engine/lib.
#   - Links LAME from Homebrew's lame 3.100 at build time; at run time the binaries load
#     Engine/lib/libmp3lame.0.dylib, which this script leaves as it is.
# usage: build-engine.sh SOURCE_ARCHIVES   (the folder holding ffmpeg-9.0.2.tar.xz; LOCAL_SETUP.md)
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
ARCHIVES="${1:?usage: build-engine.sh SOURCE_ARCHIVES}"
LAME=/opt/homebrew/Cellar/lame/3.100
FFMPEG=ffmpeg-9.0.2
FFMPEG_SHA256=8c3850283eb25fa026482078a04051e0be17347b09ef81a0849bec15a96e002e
DAV1D=dav1d-1.5.4
DAV1D_SHA256=686616b7c69eb88d44459391ab25cac13b6647a3b288835c5784e71c1514a5c5
MESON=meson-1.12.1-py3-none-any.whl
MESON_SHA256=930bc7542cbd9f57009e182fd014eba48cf1a0180a7b9006d2d32d8f168d8b02
NINJA=ninja-1.13.2-py3-none-macosx_10_9_universal2.whl
NINJA_SHA256=fd82e26c0706ad4ab88e5fdd26f3fab0a987a90f810160f6c322e752c6af298b
[[ -f "$LAME/include/lame/lame.h" ]] || { echo "Missing pinned build dependency: Homebrew lame 3.100 ($LAME)." >&2; exit 1; }
[[ -f "$ROOT/Engine/lib/libmp3lame.0.dylib" ]] || { echo "Engine/lib/libmp3lame.0.dylib is missing." >&2; exit 1; }
shasum -a 256 -c - >/dev/null <<SUMS
$FFMPEG_SHA256  $ARCHIVES/$FFMPEG.tar.xz
$DAV1D_SHA256  $ARCHIVES/$DAV1D.tar.xz
$MESON_SHA256  $ARCHIVES/$MESON
$NINJA_SHA256  $ARCHIVES/$NINJA
SUMS
WORK="$(mktemp -d -t unravel-engine)"
trap 'rm -rf "${WORK:?}"' EXIT
export MACOSX_DEPLOYMENT_TARGET=14.0
# Through the environment, not --extra-cflags: FFmpeg records its configure line in the binary.
export CFLAGS="-ffile-prefix-map=$WORK=/src"
# dav1d: static, release, no tools or tests; meson and ninja only in a throwaway environment.
python3 -m venv "$WORK/tools"
"$WORK/tools/bin/python" -m pip install --quiet --no-index --no-deps "$ARCHIVES/$MESON" "$ARCHIVES/$NINJA"
tar -xf "$ARCHIVES/$DAV1D.tar.xz" -C "$WORK"
PATH="$WORK/tools/bin:$PATH" meson setup "$WORK/dav1d-build" "$WORK/$DAV1D" --prefix="$WORK/dav1d" --libdir=lib \
    --buildtype=release --default-library=static -Denable_tools=false -Denable_tests=false -Db_ndebug=true \
    > "$WORK/dav1d.log" 2>&1 || { tail -20 "$WORK/dav1d.log" >&2; exit 1; }
PATH="$WORK/tools/bin:$PATH" ninja -C "$WORK/dav1d-build" install >> "$WORK/dav1d.log" 2>&1 || { tail -20 "$WORK/dav1d.log" >&2; exit 1; }
# Only this dav1d is visible to FFmpeg's configure (never a Homebrew copy).
export PKG_CONFIG_LIBDIR="$WORK/dav1d/lib/pkgconfig" PKG_CONFIG_PATH=""
tar -xf "$ARCHIVES/$FFMPEG.tar.xz" -C "$WORK"
cd "$WORK/$FFMPEG"
./configure --prefix=/engine --disable-autodetect --disable-gpl --disable-nonfree --disable-network --disable-doc \
    --disable-ffplay --disable-debug --disable-shared --enable-static --enable-videotoolbox --enable-audiotoolbox \
    --enable-libmp3lame --enable-libdav1d --pkg-config-flags=--static --extra-cflags="-I$LAME/include" --extra-ldflags="-L$LAME/lib" \
    > "$WORK/configure.log" || { tail -20 "$WORK/configure.log" >&2; exit 1; }
make -j8 > "$WORK/compile.log" 2>&1 || { tail -30 "$WORK/compile.log" >&2; exit 1; }
make install DESTDIR="$WORK/dest" > "$WORK/install.log" 2>&1
for TOOL in ffmpeg ffprobe; do
    BIN="$ROOT/Engine/bin/$TOOL"
    rm -f "$BIN"; cp "$WORK/dest/engine/bin/$TOOL" "$BIN"; chmod 755 "$BIN"
    LINKED="$(otool -L "$BIN" | awk '/libmp3lame/ {print $1}')"
    [[ -n "$LINKED" ]] && install_name_tool -change "$LINKED" @executable_path/../lib/libmp3lame.0.dylib "$BIN"
    if otool -L "$BIN" | grep -q -e libvpx -e libopus -e dav1d; then echo "$TOOL links libvpx, libopus or a dav1d library." >&2; exit 1; fi
    codesign --force --sign - "$BIN"
done
rm -f "$ROOT/Engine/lib/libvpx.8.dylib" "$ROOT/Engine/lib/libopus.0.dylib"
"$ROOT/Engine/bin/ffmpeg" -hide_banner -version | sed 's|--prefix=/engine|--prefix=<engine prefix>|' > "$ROOT/Engine/build-info.txt"
grep -q -- '--disable-gpl' "$ROOT/Engine/build-info.txt" && grep -q -- '--enable-libdav1d' "$ROOT/Engine/build-info.txt" \
    && ! grep -q -e libvpx -e libopus "$ROOT/Engine/build-info.txt"
[[ "$("$ROOT/Engine/bin/ffmpeg" -hide_banner -decoders 2>/dev/null)" == *" libdav1d "* ]] || { echo "FFmpeg has no dav1d decoder." >&2; exit 1; }
echo "Built $("$ROOT/Engine/bin/ffmpeg" -hide_banner -version | head -1) into Engine/bin"
