#!/usr/bin/env bash
set -euo pipefail

# Rebuilds only libmpv from the exact LGPL media-kit v0.6.0 recipe and mpv
# 0.36.0 source. The published v0.6.0 codec dylibs remain byte-for-byte link
# inputs; iOS/macOS delivery replaces only Mpv.xcframework in a staged package.

cd "$(dirname "$0")/../.."
readonly repo_root="$PWD"

requested_platform="${1:-all}"
case "$requested_platform" in
ios | macos | all) ;;
*)
  echo "usage: $0 <ios|macos|all>" >&2
  exit 64
  ;;
esac

readonly upstream_commit="4286f5557bdccc0747030e3c376ce5cd160a96a0"
readonly upstream_tag="v0.6.0"
readonly mpv_sha256="29abc44f8ebee013bb2f9fe14d80b30db19b534c679056e4851ceadf5a5e8bf6"
readonly ffmpeg_sha256="57be87c22d9b49c112b6d24bc67d42508660e6b718b3db89c44e47e289137082"
readonly libass_sha256="f0da0bbfba476c16ae3e1cfd862256d30915911f7abaa1b16ce62ee653192784"
readonly uchardet_sha256="e97a60cfc00a1c147a674b097bb1422abd9fa78a2d9ce3f3fdcc2e78a34ac5f0"
readonly meson_wheel_sha256="08f83fc17513e99cd6e82c7554c1f58af70425211887f8f9c7363b2a90209462"
readonly privacy_patch_relative="scripts/appstore/patches/libmpv-0.36.0-no-fstatfs.patch"
readonly recipe_patch_relative="scripts/appstore/patches/libmpv-darwin-v0.6.0-xcode26.patch"
readonly privacy_patch_sha256="84ec1534dd2813d916f4f47e9774255b8b6814b740258d7e7759811eb63c2565"
readonly recipe_patch_sha256="d772bfe86a168b68895323b56d9e817acd67fcb3f6e6f94408d5b3a27a486438"
readonly cache_root="${ZYRYN_CUSTOM_LIBMPV_DIR:-$PWD/.appstore-cache/libmpv-0.36.0-media-kit-v0.6.0}"
readonly downloads="$cache_root/downloads"
readonly upstream_source="$cache_root/libmpv-darwin-build"
readonly tooling="$cache_root/tooling"

case "$cache_root" in
"$PWD"/.appstore-cache/*) ;;
*)
  if [ -z "${ZYRYN_CUSTOM_LIBMPV_DIR:-}" ]; then
    echo "[appstore] refusing libmpv cache outside .appstore-cache: $cache_root" >&2
    exit 1
  fi
  ;;
esac

for command in clang curl git install_name_tool lipo make ninja pkg-config \
  python3 ruby shasum tar xcodebuild xcrun; do
  if ! command -v "$command" >/dev/null 2>&1; then
    echo "[appstore] required libmpv build tool not found: $command" >&2
    exit 1
  fi
done

actual_privacy_patch_sha="$(shasum -a 256 "$privacy_patch_relative" | awk '{print $1}')"
actual_recipe_patch_sha="$(shasum -a 256 "$recipe_patch_relative" | awk '{print $1}')"
if [ "$actual_privacy_patch_sha" != "$privacy_patch_sha256" ] || \
  [ "$actual_recipe_patch_sha" != "$recipe_patch_sha256" ]; then
  echo "[appstore] libmpv patch checksum changed" >&2
  echo "[appstore] review the source change, then update build and verification pins" >&2
  exit 1
fi

mkdir -p "$downloads" "$tooling" "$cache_root"
work_root="$(mktemp -d "$cache_root/.build.XXXXXX")"
cleanup_build() {
  local status=$?
  if [ "$status" -ne 0 ] || [ "${ZYRYN_KEEP_LIBMPV_WORK:-0}" = "1" ]; then
    echo "[appstore] retained libmpv work directory: $work_root" >&2
    return "$status"
  fi
  rm -rf "$work_root"
}
trap cleanup_build EXIT

download_checked() {
  local name="$1"
  local url="$2"
  local expected_sha="$3"
  local destination="$downloads/$name"
  local actual_sha

  if [ -f "$destination" ]; then
    actual_sha="$(shasum -a 256 "$destination" | awk '{print $1}')"
    if [ "$actual_sha" = "$expected_sha" ]; then
      printf '%s\n' "$destination"
      return 0
    fi
    mv "$destination" "$destination.rejected.$(date +%s)"
  fi

  local temporary="$destination.download.$$"
  curl --fail --location --silent --show-error --retry 3 "$url" -o "$temporary"
  actual_sha="$(shasum -a 256 "$temporary" | awk '{print $1}')"
  if [ "$actual_sha" != "$expected_sha" ]; then
    echo "[appstore] checksum mismatch for $url: $actual_sha, expected $expected_sha" >&2
    rm -f "$temporary"
    exit 1
  fi
  mv "$temporary" "$destination"
  printf '%s\n' "$destination"
}

prepare_upstream_recipe() {
  if [ ! -d "$upstream_source/.git" ]; then
    if [ -e "$upstream_source" ]; then
      echo "[appstore] cached libmpv recipe is not a Git checkout: $upstream_source" >&2
      exit 1
    fi
    git clone --depth 1 --branch "$upstream_tag" \
      https://github.com/media-kit/libmpv-darwin-build.git "$upstream_source"
  fi

  local actual_commit
  actual_commit="$(git -C "$upstream_source" rev-parse HEAD)"
  if [ "$actual_commit" != "$upstream_commit" ]; then
    echo "[appstore] libmpv recipe is $actual_commit, expected $upstream_commit" >&2
    exit 1
  fi
  if ! git -C "$upstream_source" diff --quiet --ignore-submodules -- || \
    ! git -C "$upstream_source" diff --cached --quiet --ignore-submodules --; then
    echo "[appstore] cached libmpv recipe has local changes: $upstream_source" >&2
    exit 1
  fi

  mkdir -p "$work_root/recipe"
  git -C "$upstream_source" archive "$upstream_commit" | tar -x -C "$work_root/recipe"
  (
    cd "$work_root/recipe"
    patch -p1 <"$repo_root/$recipe_patch_relative"
  )
  /usr/bin/ditto "$privacy_patch_relative" \
    "$work_root/recipe/patches/libmpv-0.36.0-no-fstatfs.patch"
}

prepare_meson() {
  local wheel
  wheel="$(download_checked \
    meson-1.2.1-py3-none-any.whl \
    https://files.pythonhosted.org/packages/e5/74/a1f1c6ba14e11e0fb050d2c61a78b6db108dd38383b6c0ab51c1becbbeff/meson-1.2.1-py3-none-any.whl \
    "$meson_wheel_sha256")"
  local venv="$tooling/meson-1.2.1"
  if [ ! -x "$venv/bin/python" ] || \
    [ "$("$venv/bin/python" -m mesonbuild.mesonmain --version 2>/dev/null || true)" != "1.2.1" ]; then
    local staging="$tooling/.meson-1.2.1.$$"
    python3 -m venv "$staging"
    "$staging/bin/python" -m pip install --no-index "$wheel" >/dev/null
    if [ -e "$venv" ]; then
      mv "$venv" "$venv.rejected.$(date +%s)"
    fi
    mv "$staging" "$venv"
  fi
  # A virtualenv's generated Meson shebang exceeds Darwin's interpreter-path
  # limit when the repository path is long. Keep a short /bin/sh shebang and
  # invoke the pinned module through the venv interpreter from the script body.
  local wrapper_dir="$tooling/meson-wrapper"
  mkdir -p "$wrapper_dir"
  cat >"$wrapper_dir/meson" <<EOF
#!/bin/sh
exec "$venv/bin/python" -m mesonbuild.mesonmain "\$@"
EOF
  chmod 755 "$wrapper_dir/meson"
  printf '%s\n' "$wrapper_dir"
}

prepare_headers() {
  local ffmpeg_archive="$1"
  local libass_archive="$2"
  local uchardet_archive="$3"
  local ffmpeg_source="$work_root/headers/ffmpeg-source"
  local ffmpeg_headers="$work_root/headers/ffmpeg"
  local iphoneos_sdk

  mkdir -p "$ffmpeg_source" "$ffmpeg_headers" \
    "$work_root/headers/libass" "$work_root/headers/uchardet"
  tar -xJf "$ffmpeg_archive" -C "$ffmpeg_source" --strip-components=1
  iphoneos_sdk="$(xcrun --sdk iphoneos --show-sdk-path)"
  (
    cd "$ffmpeg_source"
    ./configure \
      --prefix="$ffmpeg_headers" \
      --disable-all \
      --enable-avcodec \
      --enable-avfilter \
      --enable-avformat \
      --enable-swresample \
      --enable-swscale \
      --enable-cross-compile \
      --target-os=darwin \
      --arch=arm64 \
      --cc=clang \
      --sysroot="$iphoneos_sdk" \
      --extra-cflags='-arch arm64 -miphoneos-version-min=9.0' \
      --extra-ldflags='-arch arm64 -miphoneos-version-min=9.0' >/dev/null
    make -j1 install-headers >/dev/null
  )
  tar -xJf "$libass_archive" -C "$work_root/headers/libass" --strip-components=1
  tar -xJf "$uchardet_archive" -C "$work_root/headers/uchardet" --strip-components=1
}

write_support_pc_files() {
  local prefix="$1"
  local pc_dir="$prefix/lib/pkgconfig"
  cat >"$pc_dir/libass.pc" <<EOF
prefix=$prefix
exec_prefix=\${prefix}
libdir=\${prefix}/lib
includedir=\${prefix}/include

Name: libass
Description: LibASS SSA/ASS subtitle renderer
Version: 0.17.1
Requires:
Libs: -L\${libdir} -lass
Cflags: -I\${includedir}
EOF
  cat >"$pc_dir/uchardet.pc" <<EOF
libdir=$prefix/lib
includedir=$prefix/include

Name: uchardet
Description: Universal character encoding detector
Version: 0.0.8
Requires:
Libs: -L\${libdir} -luchardet
Libs.private: -lstdc++
Cflags: -I\${includedir}/uchardet
EOF
  cat >"$pc_dir/zlib.pc" <<'EOF'
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: zlib
Description: zlib supplied by the Apple SDK
Version: 1.2.12
Libs: -lz
Cflags:
EOF
}

prepare_prefix() {
  local target="$1"
  local archive="$2"
  local prefix="$work_root/prefix/$target"
  local ffmpeg_headers="$work_root/headers/ffmpeg"

  mkdir -p "$prefix/include/ass" "$prefix/include/uchardet" \
    "$prefix/lib/pkgconfig"
  /usr/bin/ditto "$ffmpeg_headers/include" "$prefix/include"
  /usr/bin/ditto "$ffmpeg_headers/lib/pkgconfig" "$prefix/lib/pkgconfig"
  ruby -pi -e 'BEGIN { @old, @replacement = ARGV.shift(2) }; $_ = $_.gsub(@old, @replacement)' \
    "$ffmpeg_headers" "$prefix" "$prefix"/lib/pkgconfig/*.pc

  /usr/bin/ditto "$work_root/headers/libass/libass/ass.h" "$prefix/include/ass/ass.h"
  /usr/bin/ditto "$work_root/headers/libass/libass/ass_types.h" "$prefix/include/ass/ass_types.h"
  /usr/bin/ditto "$work_root/headers/uchardet/src/uchardet.h" \
    "$prefix/include/uchardet/uchardet.h"
  tar -xzf "$archive" -C "$prefix/lib" --strip-components=1

  (
    cd "$prefix/lib"
    ln -sf libass.9.dylib libass.dylib
    ln -sf libavcodec.60.dylib libavcodec.dylib
    ln -sf libavfilter.9.dylib libavfilter.dylib
    ln -sf libavformat.60.dylib libavformat.dylib
    ln -sf libavutil.58.dylib libavutil.dylib
    ln -sf libswresample.4.dylib libswresample.dylib
    ln -sf libswscale.7.dylib libswscale.dylib
    ln -sf libuchardet.0.dylib libuchardet.dylib
  )
  write_support_pc_files "$prefix"
}

build_target() {
  local target="$1"
  local os="$2"
  local arch="$3"
  local archive="$4"
  local meson_bin="$5"
  local source="$work_root/build/$target/source"
  local output="$work_root/build/$target/output"
  local prefix="$work_root/prefix/$target"
  local host_path

  prepare_prefix "$target" "$archive"
  mkdir -p "$source" "$output"
  tar -xzf "$mpv_archive" -C "$source" --strip-components=1
  host_path="$meson_bin:$(dirname "$(command -v ninja)"):$(dirname "$(command -v pkg-config)"):/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin"
  env -i \
    HOME="$HOME" \
    TMPDIR="${TMPDIR:-/tmp}" \
    SOURCE_DATE_EPOCH=1690156800 \
    PATH="$host_path" \
    PROJECT_DIR="$work_root/recipe" \
    PKG_CONFIG_PATH="$prefix/lib/pkgconfig" \
    OS="$os" \
    ARCH="$arch" \
    VARIANT=video \
    SRC_DIR="$source" \
    OUTPUT_DIR="$output" \
    sh "$work_root/recipe/scripts/mpv/build.sh" \
    >"$work_root/build/$target/build.log" 2>&1

  local binary="$output/lib/libmpv.2.dylib"
  if [ ! -f "$binary" ]; then
    echo "[appstore] libmpv build did not produce $binary" >&2
    exit 1
  fi
  if /usr/bin/nm -u "$binary" | grep -Eq '_(f?statfs|f?statvfs)\b'; then
    echo "[appstore] rebuilt $target libmpv still imports a forbidden filesystem API" >&2
    exit 1
  fi
}

rewrite_ios_binary() {
  local binary="$1"
  install_name_tool -id '@rpath/Mpv.framework/Mpv' "$binary"
  install_name_tool \
    -change '@rpath/libass.9.dylib' '@rpath/Ass.framework/Ass' \
    -change '@rpath/libavcodec.60.dylib' '@rpath/Avcodec.framework/Avcodec' \
    -change '@rpath/libavfilter.9.dylib' '@rpath/Avfilter.framework/Avfilter' \
    -change '@rpath/libavformat.60.dylib' '@rpath/Avformat.framework/Avformat' \
    -change '@rpath/libavutil.58.dylib' '@rpath/Avutil.framework/Avutil' \
    -change '@rpath/libswresample.4.dylib' '@rpath/Swresample.framework/Swresample' \
    -change '@rpath/libswscale.7.dylib' '@rpath/Swscale.framework/Swscale' \
    -change '@rpath/libuchardet.0.dylib' '@rpath/Uchardet.framework/Uchardet' \
    "$binary"
}

rewrite_macos_binary() {
  local binary="$1"
  install_name_tool -id '@rpath/Mpv.framework/Versions/A/Mpv' "$binary"
  install_name_tool \
    -change '@rpath/libass.9.dylib' '@rpath/Ass.framework/Versions/A/Ass' \
    -change '@rpath/libavcodec.60.dylib' '@rpath/Avcodec.framework/Versions/A/Avcodec' \
    -change '@rpath/libavfilter.9.dylib' '@rpath/Avfilter.framework/Versions/A/Avfilter' \
    -change '@rpath/libavformat.60.dylib' '@rpath/Avformat.framework/Versions/A/Avformat' \
    -change '@rpath/libavutil.58.dylib' '@rpath/Avutil.framework/Versions/A/Avutil' \
    -change '@rpath/libswresample.4.dylib' '@rpath/Swresample.framework/Versions/A/Swresample' \
    -change '@rpath/libswscale.7.dylib' '@rpath/Swscale.framework/Versions/A/Swscale' \
    -change '@rpath/libuchardet.0.dylib' '@rpath/Uchardet.framework/Versions/A/Uchardet' \
    "$binary"
}

write_provenance() {
  local package_root="$1"
  local platform="$2"
  local template_sha="$3"
  local asset_json="$4"
  local xcode_version
  xcode_version="$(xcodebuild -version | tr '\n' ' ')"
  ruby -rjson -e '
    path, platform, commit, mpv_sha, privacy_patch, privacy_sha,
      recipe_patch, recipe_sha, template_sha, assets, xcode = ARGV
    data = {
      "upstream_build_commit" => commit,
      "upstream_build_tag" => "v0.6.0",
      "mpv_version" => "0.36.0",
      "mpv_source_sha256" => mpv_sha,
      "ffmpeg_source_sha256" => "57be87c22d9b49c112b6d24bc67d42508660e6b718b3db89c44e47e289137082",
      "libass_source_sha256" => "f0da0bbfba476c16ae3e1cfd862256d30915911f7abaa1b16ce62ee653192784",
      "uchardet_source_sha256" => "e97a60cfc00a1c147a674b097bb1422abd9fa78a2d9ce3f3fdcc2e78a34ac5f0",
      "privacy_patch" => privacy_patch,
      "privacy_patch_sha256" => privacy_sha,
      "recipe_patch" => recipe_patch,
      "recipe_patch_sha256" => recipe_sha,
      "meson_version" => "1.2.1",
      "meson_wheel_sha256" => "08f83fc17513e99cd6e82c7554c1f58af70425211887f8f9c7363b2a90209462",
      "published_template_sha256" => template_sha,
      "published_dependency_archives" => JSON.parse(assets),
      "platform" => platform,
      "xcode" => xcode.strip,
      "source" => "https://github.com/mpv-player/mpv/tree/v0.36.0",
      "build_recipe" => "https://github.com/media-kit/libmpv-darwin-build/tree/#{commit}"
    }
    File.write(path, JSON.pretty_generate(data) + "\n")
  ' "$package_root/provenance.json" "$platform" "$upstream_commit" \
    "$mpv_sha256" "$privacy_patch_relative" "$privacy_patch_sha256" \
    "$recipe_patch_relative" "$recipe_patch_sha256" "$template_sha" \
    "$asset_json" "$xcode_version"
}

package_ios() {
  local template_archive="$1"
  local stage="$cache_root/.ios-package.$$"
  local extracted="$work_root/template-ios"
  local template
  mkdir -p "$extracted" "$stage"
  tar -xzf "$template_archive" -C "$extracted"
  template="$(find "$extracted" -type d -name Mpv.xcframework -print -quit)"
  if [ -z "$template" ]; then
    echo "[appstore] published iOS template contains no Mpv.xcframework" >&2
    exit 1
  fi
  /usr/bin/ditto "$template" "$stage/Mpv.xcframework"

  local device="$work_root/build/ios-arm64/output/lib/libmpv.2.dylib"
  local sim_x86="$work_root/build/iossimulator-amd64/output/lib/libmpv.2.dylib"
  local sim_arm="$work_root/build/iossimulator-arm64/output/lib/libmpv.2.dylib"
  rewrite_ios_binary "$device"
  rewrite_ios_binary "$sim_x86"
  rewrite_ios_binary "$sim_arm"
  cp "$device" "$stage/Mpv.xcframework/ios-arm64/Mpv.framework/Mpv"
  lipo -create "$sim_x86" "$sim_arm" \
    -output "$stage/Mpv.xcframework/ios-arm64_x86_64-simulator/Mpv.framework/Mpv"
  chmod 755 "$stage"/Mpv.xcframework/*/Mpv.framework/Mpv
  while IFS= read -r framework; do
    codesign --remove-signature "$framework" 2>/dev/null || true
  done < <(find "$stage/Mpv.xcframework" -type d -name Mpv.framework -print)
  /usr/bin/ditto "$work_root/mpv-source/LICENSE.LGPL" "$stage/COPYING.LGPL-2.1.txt"
  /usr/bin/ditto "$work_root/mpv-source/Copyright" "$stage/Copyright.txt"
  write_provenance "$stage" ios \
    "a95bc18508af26136b8a408341c05b5585d644ec013f00ac07db09d2e28d36ae" \
    '{"ios-arm64":"e4c713005566e21bae8f9ef1243dad023d0ef00db81f8775abc48503eb3e3daf","iossimulator-amd64":"3c35f9490dc2f04df4536d130f1d5bb4478e3448d22ed557baabc7108a5e8883","iossimulator-arm64":"00cbad0aa02ceb945172812ef6b7cc61c2c86f02501b149fbb5addbcccc3e96b"}'
  ruby scripts/appstore/lib/verify_custom_libmpv.rb ios "$stage"
  rm -rf "$cache_root/ios"
  mv "$stage" "$cache_root/ios"
  ruby scripts/appstore/lib/verify_custom_libmpv.rb ios "$cache_root/ios"
}

package_macos() {
  local template_archive="$1"
  local stage="$cache_root/.macos-package.$$"
  local extracted="$work_root/template-macos"
  local template
  mkdir -p "$extracted" "$stage"
  tar -xzf "$template_archive" -C "$extracted"
  template="$(find "$extracted" -type d -name Mpv.xcframework -print -quit)"
  if [ -z "$template" ]; then
    echo "[appstore] published macOS template contains no Mpv.xcframework" >&2
    exit 1
  fi
  /usr/bin/ditto "$template" "$stage/Mpv.xcframework"

  local x86="$work_root/build/macos-amd64/output/lib/libmpv.2.dylib"
  local arm="$work_root/build/macos-arm64/output/lib/libmpv.2.dylib"
  rewrite_macos_binary "$x86"
  rewrite_macos_binary "$arm"
  lipo -create "$x86" "$arm" \
    -output "$stage/Mpv.xcframework/macos-arm64_x86_64/Mpv.framework/Versions/A/Mpv"
  chmod 755 "$stage/Mpv.xcframework/macos-arm64_x86_64/Mpv.framework/Versions/A/Mpv"
  while IFS= read -r framework; do
    codesign --remove-signature "$framework" 2>/dev/null || true
  done < <(find "$stage/Mpv.xcframework" -type d -name Mpv.framework -print)
  /usr/bin/ditto "$work_root/mpv-source/LICENSE.LGPL" "$stage/COPYING.LGPL-2.1.txt"
  /usr/bin/ditto "$work_root/mpv-source/Copyright" "$stage/Copyright.txt"
  write_provenance "$stage" macos \
    "84d2ad98e046e82c6dc34d8547d76c2afeaee89c0f53032773be8985c95536d6" \
    '{"macos-amd64":"e9b0bda52bafefc651addae5f4358e218263ada7e78a9a299786bf6e56208858","macos-arm64":"cb2f77f8318b9229b587cfc5a3a5f2eb13119ba70a0be56f86267d0ec77df394"}'
  ruby scripts/appstore/lib/verify_custom_libmpv.rb macos "$stage"
  rm -rf "$cache_root/macos"
  mv "$stage" "$cache_root/macos"
  ruby scripts/appstore/lib/verify_custom_libmpv.rb macos "$cache_root/macos"
}

prepare_upstream_recipe
meson_bin="$(prepare_meson)"
mpv_archive="$(download_checked mpv-0.36.0.tar.gz \
  https://github.com/mpv-player/mpv/archive/refs/tags/v0.36.0.tar.gz "$mpv_sha256")"
ffmpeg_archive="$(download_checked ffmpeg-6.0.tar.xz \
  https://ffmpeg.org/releases/ffmpeg-6.0.tar.xz "$ffmpeg_sha256")"
libass_archive="$(download_checked libass-0.17.1.tar.xz \
  https://github.com/libass/libass/releases/download/0.17.1/libass-0.17.1.tar.xz "$libass_sha256")"
uchardet_archive="$(download_checked uchardet-0.0.8.tar.xz \
  https://www.freedesktop.org/software/uchardet/releases/uchardet-0.0.8.tar.xz "$uchardet_sha256")"
prepare_headers "$ffmpeg_archive" "$libass_archive" "$uchardet_archive"
mkdir -p "$work_root/mpv-source"
tar -xzf "$mpv_archive" -C "$work_root/mpv-source" --strip-components=1

pids=()
if [ "$requested_platform" = "ios" ] || [ "$requested_platform" = "all" ]; then
  ios_arm_archive="$(download_checked \
    libmpv-libs_v0.6.0_ios-arm64-video-default.tar.gz \
    https://github.com/media-kit/libmpv-darwin-build/releases/download/v0.6.0/libmpv-libs_v0.6.0_ios-arm64-video-default.tar.gz \
    e4c713005566e21bae8f9ef1243dad023d0ef00db81f8775abc48503eb3e3daf)"
  sim_x86_archive="$(download_checked \
    libmpv-libs_v0.6.0_iossimulator-amd64-video-default.tar.gz \
    https://github.com/media-kit/libmpv-darwin-build/releases/download/v0.6.0/libmpv-libs_v0.6.0_iossimulator-amd64-video-default.tar.gz \
    3c35f9490dc2f04df4536d130f1d5bb4478e3448d22ed557baabc7108a5e8883)"
  sim_arm_archive="$(download_checked \
    libmpv-libs_v0.6.0_iossimulator-arm64-video-default.tar.gz \
    https://github.com/media-kit/libmpv-darwin-build/releases/download/v0.6.0/libmpv-libs_v0.6.0_iossimulator-arm64-video-default.tar.gz \
    00cbad0aa02ceb945172812ef6b7cc61c2c86f02501b149fbb5addbcccc3e96b)"
  build_target ios-arm64 ios arm64 "$ios_arm_archive" "$meson_bin" & pids+=("$!")
  build_target iossimulator-amd64 iossimulator amd64 "$sim_x86_archive" "$meson_bin" & pids+=("$!")
  build_target iossimulator-arm64 iossimulator arm64 "$sim_arm_archive" "$meson_bin" & pids+=("$!")
fi
if [ "$requested_platform" = "macos" ] || [ "$requested_platform" = "all" ]; then
  mac_x86_archive="$(download_checked \
    libmpv-libs_v0.6.0_macos-amd64-video-default.tar.gz \
    https://github.com/media-kit/libmpv-darwin-build/releases/download/v0.6.0/libmpv-libs_v0.6.0_macos-amd64-video-default.tar.gz \
    e9b0bda52bafefc651addae5f4358e218263ada7e78a9a299786bf6e56208858)"
  mac_arm_archive="$(download_checked \
    libmpv-libs_v0.6.0_macos-arm64-video-default.tar.gz \
    https://github.com/media-kit/libmpv-darwin-build/releases/download/v0.6.0/libmpv-libs_v0.6.0_macos-arm64-video-default.tar.gz \
    cb2f77f8318b9229b587cfc5a3a5f2eb13119ba70a0be56f86267d0ec77df394)"
  build_target macos-amd64 macos amd64 "$mac_x86_archive" "$meson_bin" & pids+=("$!")
  build_target macos-arm64 macos arm64 "$mac_arm_archive" "$meson_bin" & pids+=("$!")
fi
build_failed=0
for pid in "${pids[@]}"; do
  if ! wait "$pid"; then
    build_failed=1
  fi
done
if [ "$build_failed" -ne 0 ]; then
  echo "[appstore] one or more libmpv architecture builds failed" >&2
  exit 1
fi

if [ "$requested_platform" = "ios" ] || [ "$requested_platform" = "all" ]; then
  ios_template="$(download_checked \
    libmpv-xcframeworks_v0.6.0_ios-universal-video-default.tar.gz \
    https://github.com/media-kit/libmpv-darwin-build/releases/download/v0.6.0/libmpv-xcframeworks_v0.6.0_ios-universal-video-default.tar.gz \
    a95bc18508af26136b8a408341c05b5585d644ec013f00ac07db09d2e28d36ae)"
  package_ios "$ios_template"
fi
if [ "$requested_platform" = "macos" ] || [ "$requested_platform" = "all" ]; then
  mac_template="$(download_checked \
    libmpv-xcframeworks_v0.6.0_macos-universal-video-default.tar.gz \
    https://github.com/media-kit/libmpv-darwin-build/releases/download/v0.6.0/libmpv-xcframeworks_v0.6.0_macos-universal-video-default.tar.gz \
    84d2ad98e046e82c6dc34d8547d76c2afeaee89c0f53032773be8985c95536d6)"
  package_macos "$mac_template"
fi

echo "[appstore] custom libmpv cache is ready: $cache_root"
