#!/bin/sh
#
#  build-vlckit.sh
#  Popvue
#
#  Builds the MobileVLCKit Popvue plays video with: VideoLAN's VLCKit 3.6.0 (libVLC 3.0.21),
#  from source, with the eight patches beside this script applied. Three leave out the code
#  that is under the GPL rather than the LGPL, so that nothing in the app is GPL: zvbi, whose
#  teletext packet 8/30 decoder (src/packet-830.c) is GPL 2 only, with VLC's own LGPL teletext
#  decoder (telx) reading teletext subtitles instead (0001); the seven libVLC modules VideoLAN's
#  build keeps although their sources are GPL 2 or later (0007); and yadif, the deinterlace
#  module's two modes from MPlayer (0008). The rest only let VideoLAN's build run with today's
#  tools: four of newer clang's default errors kept as warnings for the older libraries' code,
#  an iOS 12 minimum (Xcode 16 and later cannot target iOS 9), a GNU-style sha512sum on
#  macOS 15, and contrib fixes for zlib and libpng with the iOS 18 and later SDKs (each a line
#  of their source) and for libgcrypt on the x86_64 simulator. Everything else is VideoLAN's
#  build: every library it bundles is LGPL or permissively licensed. find-gpl-sources.py and
#  check-vlckit-licences.sh then check the build and the binary for GPL code.
#
#  The result lands in Vendor/MobileVLCKit/MobileVLCKit.xcframework, which the local package
#  there wraps for the app. The binary is not kept in git (it is hundreds of megabytes); this
#  script, the patches and VideoLAN's sources rebuild it. Vendor/MobileVLCKit/README.md says
#  more, and how to move to a later VLCKit.
#
#  Usage: Scripts/vlckit/build-vlckit.sh [work-directory]
#  The work directory (default /Users/Shared/VLCKit-Popvue) ends up at about 12 GB, and the
#  build takes about an hour on an Apple M4; it downloads VLC and its libraries' sources from
#  videolan.org and their own hosts. libVLC's build writes the directory's path into the binary
#  (its configure line, its source files' names), so the default is a fixed place outside the
#  home folder: built under it, every copy of the app would carry the developer's user name.
#

set -e

VLCKIT_TAG="3.6.0"
VLCKIT_COMMIT="c73b779f"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
WORK_DIR="${1:-/Users/Shared/VLCKit-Popvue}"
OUTPUT_DIR="$REPO_DIR/Vendor/MobileVLCKit"

mkdir -p "$WORK_DIR"
cd "$WORK_DIR"

if [ ! -d vlckit ]; then
    git clone https://code.videolan.org/videolan/VLCKit.git vlckit
fi
cd vlckit
git fetch --tags --quiet
git checkout --quiet -B popvue-lgpl "$VLCKIT_TAG"
test "$(git rev-parse --short=8 HEAD)" = "$VLCKIT_COMMIT"
git -c user.name="Popvue" -c user.email="popvue@localhost" am --quiet "$SCRIPT_DIR"/*.patch

# VideoLAN's mirror is slow and drops connections; every download the build makes retries
# rather than failing the build an hour in. The settings apply to this build only.
mkdir -p "$WORK_DIR/curl"
printf 'retry = 6\nretry-delay = 5\nretry-connrefused\nconnect-timeout = 30\nspeed-limit = 1000\nspeed-time = 60\n' > "$WORK_DIR/curl/.curlrc"
export CURL_HOME="$WORK_DIR/curl"

# libVLC records who built it and on which Mac (its "compiled by" line); without these, every
# copy of the app would carry the developer's user name and computer name.
export VLC_COMPILE_BY="popvue"
export VLC_COMPILE_HOST="popvue.build"

# Device (arm64) and simulator (arm64, x86_64) slices, as a dynamic xcframework.
caffeinate -i ./buildMobileVLCKit.sh -f -v

# The licence checks: no GPL libVLC source among what the build linked, then none of the GPL
# code known by name in the binary, the check the app's build runs too.
python3 "$SCRIPT_DIR/find-gpl-sources.py" .
"$SCRIPT_DIR/check-vlckit-licences.sh" build/MobileVLCKit.xcframework

rm -rf "$OUTPUT_DIR/MobileVLCKit.xcframework"
mkdir -p "$OUTPUT_DIR"
cp -R build/MobileVLCKit.xcframework "$OUTPUT_DIR/"
echo "MobileVLCKit.xcframework copied to $OUTPUT_DIR"
