# Maintainer: Andreas Røsdal <andreas dot rosdal at gmail dot com>

pkgname=northstar-browser
pkgver=1.0.19
pkgrel=1
pkgdesc='Minimalist web browser with an original engine, written in C with a GTK 4 interface'
arch=(x86_64 aarch64)
url='https://nordstjernen.org/northstar-browser/'
license=('GPL-3.0-or-later')
depends=(
  cairo
  curl
  dav1d
  enchant
  fontconfig
  freetype2
  fribidi
  gdk-pixbuf2
  gst-plugins-base-libs
  gstreamer
  glib2
  glibc
  glslang
  gtk4
  harfbuzz
  hicolor-icon-theme
  libavif
  libepoxy
  libgcc
  libpsl
  libseccomp
  libthai
  libvorbis
  libvpx
  openssl
  opus
  opusfile
  sdl3
  sqlite
  zlib
)
makedepends=(git meson python)
optdepends=(
  'hunspell: spell checking'
  'hunspell-en_us: English dictionary for spell checking'
  'gst-plugins-good: VP9 video through GStreamer'
  'gst-plugins-bad: AV1 video and the H.264 parser through GStreamer'
  'gst-libav: H.264 and other video through GStreamer'
)
_lexbor_commit=7e278c0188489bfce6b71ced0cc900cbb31e6244
_quickjs_ng_commit=9b061718955874e648d8c2bf2910a717aac8fa63
source=("$pkgname-$pkgver.tar.gz::https://github.com/nordstjernen-web/northstar-web-browser/archive/refs/tags/$pkgver.tar.gz"
        "lexbor::git+https://github.com/lexbor/lexbor.git#commit=$_lexbor_commit"
        "quickjs-ng::git+https://github.com/quickjs-ng/quickjs.git#commit=$_quickjs_ng_commit"
        "wasm-micro-runtime-WAMR-2.4.6.tar.gz::https://github.com/bytecodealliance/wasm-micro-runtime/archive/refs/tags/WAMR-2.4.6.tar.gz"
        "wuffs-0.4.0-alpha.12.tar.gz::https://github.com/google/wuffs/archive/refs/tags/v0.4.0-alpha.12.tar.gz")
# WAMR and Wuffs are wrap-file subprojects: meson extracts and patches them
# itself from subprojects/packagecache/, which arch-meson's
# --wrap-mode=nodownload still reads.
noextract=(wasm-micro-runtime-WAMR-2.4.6.tar.gz
           wuffs-0.4.0-alpha.12.tar.gz)
sha256sums=('af2ff9195da62e4bf0e72a43ac2e6fbb4d298e02aaaa3b9eaaab5c61a963c9d3'
            'SKIP'
            'SKIP'
            '6a8de3f74671a72698b4bd61a96c1ce8966db077b2adad7b720757cfd9caff62'
            '0c1c84aa93fcaeda523ed517848b1555e72e679c37780aaa2db8d7e5b8ff78f1')

_srcdir=northstar-web-browser-$pkgver

_add_subproject() {
  local name=$1 commit=$2 wrap="$_srcdir/subprojects/$1.wrap" patch overlay

  # arch-meson never downloads a wrap, so the pinned checkout is put where
  # meson looks for it, with the patches the wrap would have applied.
  grep -q "^revision = $commit\$" "$wrap" ||
    { error "$wrap is not pinned to $commit"; return 1; }
  rm -rf "$_srcdir/subprojects/$name"
  cp -a "$name" "$_srcdir/subprojects/$name"
  for patch in $(sed -n 's/^diff_files *= *//p' "$wrap" | tr ',' ' '); do
    patch -d "$_srcdir/subprojects/$name" -Np1 -i "../packagefiles/$patch"
  done
  overlay=$(sed -n 's/^patch_directory *= *//p' "$wrap")
  if [ -n "$overlay" ]; then
    cp -a "$_srcdir/subprojects/packagefiles/$overlay/." "$_srcdir/subprojects/$name/"
  fi
}

prepare() {
  _add_subproject lexbor "$_lexbor_commit"
  _add_subproject quickjs-ng "$_quickjs_ng_commit"
  mkdir -p "$_srcdir/subprojects/packagecache"
  cp wasm-micro-runtime-WAMR-2.4.6.tar.gz wuffs-0.4.0-alpha.12.tar.gz \
    "$_srcdir/subprojects/packagecache/"
}

build() {
  # H.264 stays off: only Cisco's prebuilt OpenH264 binaries carry its patent
  # licence. The other codecs are required explicitly so a missing dependency
  # fails the build instead of dropping a feature.
  arch-meson "$_srcdir" build \
    -Daudio=enabled \
    -Davif=enabled \
    -Dwebgl=enabled \
    -Dav1=enabled \
    -Dvp9=enabled \
    -Dh264=disabled \
    -Dbuild_date="$(date -u -d "@${SOURCE_DATE_EPOCH:-$(date +%s)}" +%F)"
  meson compile -C build
}

package() {
  # lexbor and quickjs-ng are linked statically; their headers and archives
  # are not shipped.
  meson install -C build --destdir "$pkgdir" --skip-subprojects

  # open-internet-browser is a Debian/openSUSE alternatives name that other
  # browsers may also claim; Arch has no alternatives system.
  rm "$pkgdir/usr/bin/open-internet-browser"

  install -d "$pkgdir/usr/share/licenses/$pkgname"
  ln -s /usr/share/northstar/LICENSE "$pkgdir/usr/share/licenses/$pkgname/"
}
