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

pkgname=nordstjernen-browser
pkgver=1.0.51
pkgrel=1
pkgdesc='Small web browser written in C, with a GTK 4 interface'
arch=(x86_64 aarch64)
url='https://nordstjernen.org'
license=('GPL-3.0-or-later OR LicenseRef-NSL-1.0')
depends=(
  brotli
  cairo
  curl
  enchant
  ffmpeg
  fontconfig
  freetype2
  fribidi
  gdk-pixbuf2
  glib2
  glibc
  gst-plugins-base-libs
  gstreamer
  glslang
  gtk4
  harfbuzz
  hicolor-icon-theme
  libavif
  libepoxy
  libgcc
  libjxl
  libpsl
  libseccomp
  libthai
  libwebp
  openssl
  sdl3
  sqlite
  uchardet
  zlib
  zstd
)
makedepends=(meson rust simde)
optdepends=(
  'gst-libav: H.264 and AAC playback'
  'gst-plugins-good: H.264 and AAC playback'
  'gst-plugins-bad: H.264 and AAC playback'
  'hunspell: spell checking'
  'hunspell-en_us: English dictionary for spell checking'
)
_pdfjsver=6.5.438
_wuffsver=0.4.0-alpha.12
_wamrver=2.4.6
_pdfjs=pdfjs-dist-$_pdfjsver.tgz
_wuffs=wuffs-$_wuffsver.tar.gz
_wamr=wasm-micro-runtime-WAMR-$_wamrver.tar.gz
source=("$pkgname-$pkgver.tar.gz::https://github.com/nordstjernen-web/nordstjernen-sol-browser/archive/refs/tags/$pkgver.tar.gz"
        "$_pdfjs::https://registry.npmjs.org/pdfjs-dist/-/$_pdfjs"
        "$_wuffs::https://github.com/google/wuffs/archive/refs/tags/v$_wuffsver.tar.gz"
        "$_wamr::https://github.com/bytecodealliance/wasm-micro-runtime/archive/refs/tags/WAMR-$_wamrver.tar.gz")
noextract=("$_pdfjs" "$_wuffs" "$_wamr")
sha256sums=('679d749230c865fbe0ffa8ad326e5a78841a939ca8ea68b654122f5b44e3f79c'
            '7461aefcd27c4ef0dd76f9ca5760f09466eb6b3f934432670c12132e7b3d6faa'
            '0c1c84aa93fcaeda523ed517848b1555e72e679c37780aaa2db8d7e5b8ff78f1'
            '6a8de3f74671a72698b4bd61a96c1ce8966db077b2adad7b720757cfd9caff62')

_srcdir=nordstjernen-sol-browser-$pkgver

prepare() {
  # PDF.js, Wuffs and WAMR come from these pinned tarballs (the wraps in
  # subprojects/) instead of being downloaded during the build.
  for f in "$_pdfjs" "$_wuffs" "$_wamr"; do
    install -Dm644 "$srcdir/$f" "$_srcdir/subprojects/packagecache/$f"
  done
}

build() {
  # wgpu-native is not in the Arch repositories and meson would otherwise
  # download a prebuilt release, so WebGPU is left out, as in the Debian and
  # openSUSE packages. The optional codecs are required explicitly so a
  # missing dependency fails the build instead of dropping a feature.
  arch-meson "$_srcdir" build \
    -Dwebgpu=disabled \
    -Davif=enabled \
    -Djxl=enabled \
    -Dgstreamer=enabled \
    -Daudio=enabled \
    -Dbuild_date="$(date -u -d "@${SOURCE_DATE_EPOCH:-$(date +%s)}" +%F)"
  meson compile -C build
}

package() {
  meson install -C build --destdir "$pkgdir"

  # libnordstjernen.so and its header are the embedding API; the browser links
  # the engine statically, and the library has no SONAME version.
  rm "$pkgdir/usr/lib/libnordstjernen.so"
  rm -r "$pkgdir/usr/include"
  rmdir "$pkgdir/usr/lib"

  install -d "$pkgdir/usr/share/licenses/$pkgname"
  ln -s /usr/share/nordstjernen/License.md "$pkgdir/usr/share/licenses/$pkgname/"
  ln -s /usr/share/nordstjernen/THIRD-PARTY-LICENSES.md "$pkgdir/usr/share/licenses/$pkgname/"
}
