# Maintainer: Markus Fritsche <mfritsche@reauktion.de>
#
# Firefox with V4L2 stateless (request API) hardware video decode
# unlocked for mainline-Linux Rockchip (RK3399 rkvdec, RK3566/RK3588
# hantro multiplanar, RK3588 rkvdec2). Sibling to chromium-fourier;
# same niche. No vendor MPP, no Mali blob, no panfork, no 5.10 BSP.
#
# Patch series adds 4 thin shims around upstream firefox (~+169 lines,
# zero deletions). Architecture: stateless decode rides libavcodec's
# v4l2_request hwaccel (AV_HWDEVICE_TYPE_DRM); no separate Mozilla V4L2
# decoder gets written. See ../../arch/firefox-fourier/PLAN.md for
# the full diagnosis. Mozilla bug 1969297.

pkgname=firefox-fourier
pkgver=150.0.1
pkgrel=3
pkgdesc='Firefox with V4L2 stateless HW video decode unlocked for mainline Linux Rockchip'
arch=('aarch64' 'x86_64')
url='https://www.mozilla.org/firefox'
license=('MPL-2.0')
depends=(
  alsa-lib
  at-spi2-core
  cairo
  dbus
  ffmpeg
  fontconfig
  freetype2
  gcc-libs
  gdk-pixbuf2
  glib2
  glibc
  gtk3
  hicolor-icon-theme
  libdrm
  libpulse
  libva
  libxcb
  libxkbcommon
  mesa
  nspr
  nss
  pango
  pciutils
  ttf-liberation
  v4l-utils
)
makedepends=(
  cbindgen
  clang
  imagemagick
  inetutils
  lld
  llvm
  mesa
  nasm
  nodejs
  python
  rust
  unzip
  wasi-compiler-rt
  wasi-libc
  yasm
  zip
)
optdepends=(
  'hunspell-en_us: spell checking, American English'
  'libnotify: send notifications when downloads complete'
  'pulseaudio: audio support'
)
provides=(firefox)
conflicts=(firefox)
options=('!emptydirs' '!strip')
source=(
  "https://archive.mozilla.org/pub/firefox/releases/${pkgver}/source/firefox-${pkgver}.source.tar.xz"
  'mozconfig'
  # Arch's official firefox patches — toolchain glue for clang 22 +
  # glibc 2.43 + Rust 1.95+. Picked up verbatim because we hit the same
  # walls. arch-0001 (install-under-remoting) skipped — our launcher
  # ships under /usr/bin/firefox-fourier with our own wrapper.
  # https://gitlab.archlinux.org/archlinux/packaging/packages/firefox
  'patches/arch-0002-Bug-2033279-Make-enable-rust-simd-work-with-Rust-1.9.patch'
  'patches/arch-0003-Patch-glsl-optimizer-to-build-with-glibc-2.43.patch'
  'patches/arch-0004-Bug-2023597-Use-wasm32-wasip1-target-for-clang-22.1-.patch'
  # firefox-fourier patches — V4L2 stateless decode unlock.
  'patches/0001-gfxinfo-v4l2-stateless-fourccs.patch'
  'patches/0002-libwrapper-hwdevice-ctx-create.patch'
  'patches/0003-ffmpegvideo-v4l2-request-route.patch'
  'patches/0004-prefs-v4l2-request.patch'
  'patches/0005-rdd-sandbox-v4l2-media-ctl.patch'
  # Vendor-default prefs that gate the patched VAAPI path on RK3399 —
  # widget.dmabuf.force-enabled etc. See marfrit-packages#8 for evidence.
  'rockchip-fourier-defaults.js'
)
sha256sums=('SKIP' 'SKIP' 'SKIP' 'SKIP' 'SKIP' 'SKIP' 'SKIP' 'SKIP' 'SKIP' 'SKIP' 'SKIP')

prepare() {
  cd "${srcdir}/firefox-${pkgver}"

  # Toolchain glue (Arch upstream) — apply BEFORE the fourier patches.
  patch -Np1 -i "${srcdir}/patches/arch-0002-Bug-2033279-Make-enable-rust-simd-work-with-Rust-1.9.patch"
  patch -Np1 -i "${srcdir}/patches/arch-0003-Patch-glsl-optimizer-to-build-with-glibc-2.43.patch"
  patch -Np1 -i "${srcdir}/patches/arch-0004-Bug-2023597-Use-wasm32-wasip1-target-for-clang-22.1-.patch"

  # Fourier patches — order matters; see ../PLAN.md for rationale.
  patch -Np1 -i "${srcdir}/patches/0001-gfxinfo-v4l2-stateless-fourccs.patch"
  patch -Np1 -i "${srcdir}/patches/0002-libwrapper-hwdevice-ctx-create.patch"
  patch -Np1 -i "${srcdir}/patches/0003-ffmpegvideo-v4l2-request-route.patch"
  patch -Np1 -i "${srcdir}/patches/0004-prefs-v4l2-request.patch"
  patch -Np1 -i "${srcdir}/patches/0005-rdd-sandbox-v4l2-media-ctl.patch"

  cp "${srcdir}/mozconfig" .mozconfig
}

build() {
  cd "${srcdir}/firefox-${pkgver}"

  # Arch's makepkg.conf injects -fexceptions into CFLAGS/CXXFLAGS by
  # default for hardening. Mozilla's STL wrappers refuse to compile
  # with exceptions enabled (#error "STL code can only be used with
  # -fno-exceptions"). Strip the offender before mach configure picks
  # up the env. Same trick the upstream Arch firefox PKGBUILD uses.
  CFLAGS="${CFLAGS//-fexceptions/}"
  CXXFLAGS="${CXXFLAGS//-fexceptions/}"
  export CFLAGS CXXFLAGS

  export MOZ_NOSPAM=1
  export MOZ_API_KEY_UNUSED=1
  export MOZ_TELEMETRY_REPORTING=
  export MOZ_REQUIRE_SIGNING=
  export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=system
  export PYTHON=/usr/bin/python

  ./mach configure
  ./mach build
}

package() {
  cd "${srcdir}/firefox-${pkgver}"

  DESTDIR="${pkgdir}" ./mach install

  # Move mach's default /usr/local/* layout to /usr/* so we conflict
  # with `firefox` cleanly and `provides=firefox` actually works.
  # `cp -r` preserves the bin symlink (target lives in /usr/local) —
  # delete it before staging the launcher so `cat >` doesn't follow a
  # dangling symlink and ENOENT.
  if [ -d "${pkgdir}/usr/local" ]; then
    cp -r "${pkgdir}/usr/local/." "${pkgdir}/usr/"
    rm -rf "${pkgdir}/usr/local"
  fi
  rm -f "${pkgdir}/usr/bin/firefox-fourier"

  # Launcher script. mach's install drops the binary at
  # /usr/lib/firefox-fourier/firefox-fourier (a small bash launcher) plus
  # firefox-fourier-bin alongside; we exec the launcher.
  cat > "${pkgdir}/usr/bin/firefox-fourier" <<'LAUNCHER'
#!/bin/bash
# firefox-fourier launcher — V4L2 stateless HW decode path defaults.
# Patch 4/4 already defaults media.ffmpeg.v4l2-request.enabled=true on
# Linux; the env vars below cover the platform-detection bits firefox
# still consults at startup.
export MOZ_ENABLE_WAYLAND="${MOZ_ENABLE_WAYLAND:-1}"
export MOZ_X11_EGL="${MOZ_X11_EGL:-1}"
exec /usr/lib/firefox-fourier/firefox-fourier "$@"
LAUNCHER
  chmod 0755 "${pkgdir}/usr/bin/firefox-fourier"

  # Vendor-default prefs (RK3399 HW-decode unlock) — closes #8.
  # Lower precedence than user prefs / about:config; loaded by Firefox
  # at startup from the package install dir. The 0004 patch covers
  # media.ffmpeg.v4l2-request.enabled; this file covers the three
  # additional prefs that gate the path to the patched code.
  install -Dm644 "${srcdir}/rockchip-fourier-defaults.js" \
    "${pkgdir}/usr/lib/firefox-fourier/browser/defaults/preferences/rockchip-fourier-defaults.js"
}
