# Maintainer: Markus Fritsche <mfritsche@reauktion.de>
#
# Chromium with V4L2 HW video decode unlocked on Linux for Rockchip
# (RK3566 hantro / RK3588 VDPU381) on **mainline** kernel + Wayland +
# panfrost / panthor — no vendor MPP, no Mali blob, no panfork, no
# 5.10 BSP kernel. Fills the niche that 7Ji's chromium-mpp explicitly
# does not (it forces BSP + X11 + vendor stack); see
# /home/mfritsche/src/marfrit-packages/arch/chromium-fourier/STUDY.md
# for the full rationale.
#
# Build host: chromium-builder LXD container on boltzmann (8 cores,
# 28 GB RAM cap, 824 GB NVMe). 6-10 h initial build.

pkgname=chromium-fourier
pkgver=147.0.7727.116
pkgrel=1
epoch=1
pkgdesc='Chromium with V4L2VDA HW video decode unlocked for mainline Linux Wayland on Rockchip'
arch=('aarch64')
url='https://www.chromium.org/Home'
license=('BSD-3-Clause')
depends=(
  alsa-lib
  at-spi2-core
  cairo
  cups
  dbus
  fontconfig
  freetype2
  gtk3
  hicolor-icon-theme
  libdrm
  libpulse
  libva
  libxcb
  libxkbcommon
  mesa
  nspr
  nss
  pango
  pciutils
  pipewire
  ttf-liberation
  v4l-utils
  wayland
)
makedepends=(
  clang
  elfutils
  gn
  gperf
  java-runtime-headless
  libxslt
  lld
  ninja
  nodejs
  npm
  python
  qt5-base
  qt6-base
  re2
  rust
  rust-bindgen
)
optdepends=(
  'qt6-base: for Qt6 toolkit integration'
)
provides=(chromium)
conflicts=(chromium)
options=('!lto' '!strip')
# NB: the chromium tarball is fetched lazily by build.sh on the build
# host (5.7 GB compressed, doesn't belong in this git repo). The
# PKGBUILD assumes /build/chromium/src is already populated.
source=(
  'patches/chromeos-pipeline-bypass.patch'
)
sha256sums=(
  'SKIP'
)

# NB: this PKGBUILD is currently development-shaped — it operates on a
# pre-extracted chromium tree at /build/chromium/src rather than letting
# makepkg fetch + unpack the 5.7 GB tarball into ${srcdir}. Once the
# patches stabilise we'll switch to the canonical pattern with
# source=(${url}/chromium-${pkgver}.tar.xz ...) so the standard makepkg
# flow + CI fermi-style pipeline work end-to-end.

prepare() {
  cd /build/chromium/src

  # Fourier-local: bypass the chromeos pipeline so VaapiVideoDecoder /
  # V4L2VDA is reachable on Linux non-ChromeOS. The 7Ji-style gn args
  # (use_v4l2_codec / use_v4lplugin / use_linux_v4l2_only) may be
  # sufficient on their own; this patch is the fallback if they aren't.
  patch -Np1 -i "${srcdir}/patches/chromeos-pipeline-bypass.patch" || true

  # Use system node, system java
  rm -f third_party/node/linux/node-linux-arm64/bin/node
  mkdir -p third_party/node/linux/node-linux-arm64/bin
  ln -sf /usr/bin/node third_party/node/linux/node-linux-arm64/bin/
  ln -sf /usr/bin/java third_party/jdk/current/bin/ 2>/dev/null || true
}

build() {
  cd /build/chromium/src

  local _flags=(
    'is_official_build=true'
    'is_debug=false'
    'symbol_level=0'
    'is_cfi=false'
    'treat_warnings_as_errors=false'
    'enable_nacl=false'
    'enable_widevine=false'

    # System toolchain (clang/lld from pacman)
    'custom_toolchain="//build/toolchain/linux/unbundle:default"'
    'host_toolchain="//build/toolchain/linux/unbundle:default"'
    'use_sysroot=false'
    'use_custom_libcxx=true'

    # The whole point of chromium-fourier — V4L2 HW decode on Linux
    'use_v4l2_codec=true'
    'use_v4lplugin=true'
    'use_linux_v4l2_only=true'
    'use_vaapi=true'

    # Codec branding for proprietary codec support (H.264 etc.)
    'ffmpeg_branding="Chrome"'
    'proprietary_codecs=true'

    'rtc_use_pipewire=true'
    'link_pulseaudio=true'
    'use_qt6=true'
    'moc_qt6_path="/usr/lib/qt6"'
  )

  gn gen out/Default --args="${_flags[*]}"
  autoninja -C out/Default chrome chromedriver chrome_sandbox
}

package() {
  cd /build/chromium/src

  install -Dm755 out/Default/chrome "${pkgdir}/usr/lib/chromium/chromium"
  install -Dm4755 out/Default/chrome_sandbox "${pkgdir}/usr/lib/chromium/chrome-sandbox"
  install -Dm755 out/Default/chromedriver "${pkgdir}/usr/bin/chromedriver"

  # Resources / locales / pak files
  for f in chrome_100_percent.pak chrome_200_percent.pak resources.pak \
           v8_context_snapshot.bin icudtl.dat headless_lib_data.pak \
           headless_lib_strings.pak; do
    [ -f "out/Default/$f" ] && install -Dm644 "out/Default/$f" \
      "${pkgdir}/usr/lib/chromium/$f"
  done

  # Locales
  if [ -d out/Default/locales ]; then
    install -dm755 "${pkgdir}/usr/lib/chromium/locales"
    cp -r out/Default/locales/* "${pkgdir}/usr/lib/chromium/locales/"
  fi

  # Top-level launcher
  install -dm755 "${pkgdir}/usr/bin"
  ln -s /usr/lib/chromium/chromium "${pkgdir}/usr/bin/chromium"
}
