# Contributor: Antoine Martin (ayakael) <dev@ayakael.net>
# Maintainer: Antoine Martin (ayakael) <dev@ayakael.net>
pkgname=zotero
pkgver=7.0.0_beta76
pkgrel=0
_fxver=115.9.1
_gittag=$pkgver
# Date of release, YY-MM-DD for metainfo file (see package())
# https://www.mozilla.org/firefox/organizations/notes/
_releasedate=2024-03-22
pkgdesc="A free, easy-to-use tool to help you collect, organize, cite, and share your research sources."
url="https://www.mozilla.org/en-US/firefox/organizations/"
# s390x and riscv64: blocked by rust and cargo
# armhf: build failure on armhf due to wasm
arch="x86_64 armv7 aarch64 x86 ppc64le"
license="GPL-3.0-only AND LGPL-2.1-only AND LGPL-3.0-only AND MPL-2.0"
depends="
	ffmpeg-libavcodec
	"
makedepends="
	alsa-lib-dev
	automake
	bash
	bsd-compat-headers
	cargo
	cbindgen
	clang
	clang-libclang
	curl
	dbus-glib-dev
	gettext
	grep
	gtk+3.0-dev
	hunspell-dev
	icu-dev>=69.1
	libevent-dev
	libffi-dev
	libjpeg-turbo-dev
	libnotify-dev
	libogg-dev
	libtheora-dev
	libtool
	libvorbis-dev
	libvpx-dev
	libwebp-dev
	libxcomposite-dev
	libxt-dev
	lld
	llvm-dev
	m4
	mesa-dev
	nasm
	nodejs
	npm
	nspr-dev
	nss-dev
	perl
	pipewire-dev
	pulseaudio-dev
	py3-psutil
	py3-zstandard
	python3
	rsync
	sed
	unzip
	wasi-sdk
	wireless-tools-dev
	xvfb-run
	zip
	"
source="https://ftp.mozilla.org/pub/firefox/releases/${_fxver}esr/source/firefox-${_fxver}esr.source.tar.xz
	audio-lfs64.patch
	disable-moz-stackwalk.patch
	esr-metainfo.patch
	fix-fortify-system-wrappers.patch
	fix-rust-target.patch
	fix-webrtc-glibcisms.patch
	icu74.patch
	lfs64.patch
	no-ccache-stats.patch
	ppc-musttail.patch
	ppc-webrtc.patch
	python-deps.patch
	rust-lto-thin.patch
	sandbox-fork.patch
	sandbox-largefile.patch
	sandbox-sched_setscheduler.patch

	stab.h

	firefox.desktop
	mozilla-location.keys
	vendor-prefs.js

	zotero.desktop
	https://lab.ilot.io/mirrors/zotero-client/-/releases/$_gittag/downloads/tarball/zotero-client-$_gittag.tar.gz
	zotero_build-modifications.patch
	zotero_drop-phantomjs.patch
	zotero_test-drop-build.patch
	zotero_test-fix-chars.patch
	zotero_test-push-timeout-to-30sec.patch
	"

builddir="$srcdir"/firefox-$_fxver
_zoterodir="$srcdir"/zotero-client-$_gittag
_mozappdir=/usr/lib/zotero

# help our shared-object scanner to find the libs
ldpath="$_mozappdir"

# we need this because cargo verifies checksums of all files in vendor
# crates when it builds and gives us no way to override or update the
# file sanely... so just clear out the file list
_clear_vendor_checksums() {
	sed -i 's/\("files":{\)[^}]*/\1/' third_party/rust/$1/.cargo-checksum.json
}

export SHELL=/bin/sh
export BUILD_OFFICIAL=1
export MOZILLA_OFFICIAL=1
export USE_SHORT_LIBNAME=1
export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=system
export MOZ_APP_PROFILE="mozilla/firefox"
export MOZ_APP_REMOTINGNAME=firefox-esr
export MOZBUILD_STATE_PATH="$srcdir"/mozbuild
# disable desktop notifications
export MOZ_NOSPAM=1
# Find our triplet JSON
export RUST_TARGET="$CTARGET"

# Build with Clang, takes less RAM
export CC="clang"
export CXX="clang++"

# set rpath so linker finds the libs
export LDFLAGS="$LDFLAGS -Wl,-rpath,$_mozappdir"

# let firefox do this itself.
unset CARGO_PROFILE_RELEASE_OPT_LEVEL
unset CARGO_PROFILE_RELEASE_LTO

export CFLAGS="${CFLAGS/-fstack-clash-protection/} -g0 -O2"
export CXXFLAGS="${CXXFLAGS/-fstack-clash-protection/} -g0 -O2 -Wno-deprecated-builtins -Wno-deprecated-declarations"

prepare() {
	# zotero prepare
	mv "$_zoterodir" "$builddir"/zotero
	(
		cd "$builddir"/zotero

		# checks that we're using expected fx version
		local _exp_fxver=$(grep GECKO_VERSION_LINUX "$builddir"/zotero/app/config.sh | sed 's|.*=||' | tr -d '"' | sed 's|esr||')
		if [ "$_fxver" != "$_exp_fxver" ]; then
			msg "Expected firefox version $_exp_fxver, got $_fxver"
			return 1
		fi

		# zotero build expects to be in a git repo
		git init
		git commit --allow-empty -m 'Initial'

		npm i --legacy-peer-deps
	)
	default_prepare

	cp "$srcdir"/stab.h toolkit/crashreporter/google-breakpad/src/

	_clear_vendor_checksums audio_thread_priority

	base64 -d "$srcdir"/mozilla-location.keys > "$builddir"/mozilla-api-key

	# webrtc does not build on these
	case "$CARCH" in
	ppc64le)
		local webrtc_config="ac_add_options --disable-webrtc"
		;;
	esac
	case "$CARCH" in
	armv7)
		# broken here
		local rust_simd="ac_add_options --disable-rust-simd"
		;;
	*)
		local rust_simd="ac_add_options --enable-rust-simd"
		;;
	esac

	case "$CARCH" in
	aarch64|arm*|x86*)
		# disable-elf-hack: exists only on aarch64, arm*, x86, x86_64
		local arch_config="ac_add_options --disable-elf-hack"
		;;
	esac

	# sandbox only supported here
	case "$CARCH" in
	x86*|armv7|aarch64)
		local sandbox="ac_add_options --enable-sandbox"
		;;
	*)
		local sandbox="ac_add_options --disable-sandbox"
		;;
	esac

	cat > base-mozconfig <<-EOF
	# disable unwanted things
	ac_add_options --disable-bootstrap
	ac_add_options --disable-cargo-incremental
	ac_add_options --disable-crashreporter
	ac_add_options --disable-debug
	ac_add_options --disable-debug-symbols
	ac_add_options --disable-install-strip
	ac_add_options --disable-jemalloc
	ac_add_options --disable-strip
	ac_add_options --disable-tests
	ac_add_options --disable-updater

	# features
	ac_add_options --enable-alsa
	ac_add_options --enable-dbus
	ac_add_options --enable-default-toolkit=cairo-gtk3-wayland
	ac_add_options --enable-ffmpeg
	ac_add_options --enable-hardening
	ac_add_options --enable-linker=lld
	ac_add_options --enable-necko-wifi
	ac_add_options --enable-official-branding
	ac_add_options --enable-optimize="$CFLAGS"
	ac_add_options --enable-pulseaudio
	ac_add_options --enable-release
	ac_add_options --enable-update-channel=release

	# system libs
	ac_add_options --enable-system-pixman
	ac_add_options --with-system-ffi
	ac_add_options --with-system-icu
	ac_add_options --with-system-jpeg
	ac_add_options --with-system-libevent
	ac_add_options --with-system-libvpx
	ac_add_options --with-system-nspr
	ac_add_options --with-system-nss
	ac_add_options --with-system-png
	ac_add_options --with-system-webp
	ac_add_options --with-system-zlib

	# misc
	ac_add_options --allow-addon-sideload
	ac_add_options --prefix=/usr
	ac_add_options --with-app-name=firefox-esr
	ac_add_options --with-distribution-id=org.alpinelinux
	ac_add_options --with-libclang-path=/usr/lib
	ac_add_options --with-unsigned-addon-scopes=app,system
	ac_add_options --with-wasi-sysroot=/usr/share/wasi-sysroot
	ac_add_options --host=$CHOST
	ac_add_options --target=$CTARGET

	# objdir
	mk_add_options MOZ_OBJDIR="$builddir/obj"

	mk_add_options RUSTFLAGS="$RUSTFLAGS"

	# keys
	# these are for alpine linux use only
	ac_add_options --with-mozilla-api-keyfile="$builddir/mozilla-api-key"

	$arch_config
	$rust_simd
	$sandbox
	$webrtc_config
	EOF
}

build() {
	cat > .mozconfig base-mozconfig

	export MOZ_BUILD_DATE=$(date ${SOURCE_DATE_EPOCH:+ -d@${SOURCE_DATE_EPOCH}} "+%Y%m%d%H%M%S")

	# for lto
	ulimit -n 4096

	# can't be set here and fail
	unset RUSTFLAGS

	local thinlto_jobs=${JOBS:-1}

	case "$CARCH" in
	# on this platforms, lld seems to not utilise >1 threads for thinlto for some reason.
	# at the same time, having more than 8 also crashes lld for firefox buildsystems (why?).
	aarch64)
		if [ $thinlto_jobs -gt 8 ]; then
			thinlto_jobs=8
		fi
		;;
	esac

	export LDFLAGS="$LDFLAGS -Wl,--thinlto-jobs=$thinlto_jobs"

	case "$CARCH" in
	# lto for 64-bit systems only
	aarch64|x86_64|ppc64le)
		cat > .mozconfig base-mozconfig <<-EOF
		ac_add_options --enable-lto=cross
		EOF
	esac

 	./mach build

	# install to where zotero expects it
	DESTDIR="$builddir"/zotero/app/xulrunner/firefox ./mach install
	mv "$builddir"/zotero/app/xulrunner/firefox/usr/lib/firefox-esr/* "$builddir"/zotero/app/xulrunner/firefox/.
	rm -R "$builddir"/zotero/app/xulrunner/firefox/usr

	# zotero build
	(
		cd "$builddir"/zotero
		NODE_OPTIONS=--openssl-legacy-provider npm run build

		SKIP_32=1 app/scripts/dir_build -p l
		# move to seperate folder as check will rebuild with test files added
		mv "$builddir"/zotero/app/staging  "$builddir"/zotero/app/package
	)

	rm -R "$builddir"/zotero/app/xulrunner/firefox # clean-up unused firefox install
}

check() {
	# install to where zotero expects it
	DESTDIR="$builddir"/zotero/app/xulrunner/firefox ./mach install
	mv "$builddir"/zotero/app/xulrunner/firefox/usr/lib/firefox-esr/* "$builddir"/zotero/app/xulrunner/firefox/.
	rm -R "$builddir"/zotero/app/xulrunner/firefox/usr

	# zotero test build
	(
		cd "$builddir"/zotero
		ZOTERO_TEST=1 SKIP_32=1 app/scripts/dir_build -p l

	)

	# test-suite is not very stable, false failure occur from time to time, thus never fails
	CI=true LD_LIBRARY_PATH="$builddir"/zotero/app/staging/Zotero_linux xvfb-run "$builddir"/zotero/test/runtests.sh -x "$builddir"/zotero/app/staging/Zotero_linux/zotero || true

	rm -R "$builddir"/zotero/app/xulrunner/firefox # clean-up unused firefox install
}

package() {
	install -dDm755 "$pkgdir"/usr/bin
	install -dDm755 "$pkgdir"/usr/lib/zotero
	cp -r "$builddir"/zotero/app/package/Zotero_linux/* "$pkgdir/usr/lib/zotero"
	ln -s /usr/lib/zotero/zotero "$pkgdir/usr/bin/zotero"
	install -Dm644 "$srcdir/zotero.desktop" "$pkgdir/usr/share/applications/zotero.desktop"

	# Copy zotero icons to a standard location
	install -Dm644 "$pkgdir/usr/lib/zotero/icons/icon32.png" "$pkgdir/usr/share/icons/hicolor/32x32/apps/zotero.png"
	install -Dm644 "$pkgdir/usr/lib/zotero/icons/icon64.png" "$pkgdir/usr/share/icons/hicolor/64x64/apps/zotero.png"
	install -Dm644 "$pkgdir/usr/lib/zotero/icons/icon128.png" "$pkgdir/usr/share/icons/hicolor/128x128/apps/zotero.png"

	# Close shell when launching
	sed -i -r 's:^("\$CALLDIR/zotero-bin" -app "\$CALLDIR/application.ini" "\$@"):exec \1:' "$pkgdir/usr/lib/zotero/zotero"
}

sha512sums="
9ccaede2fcda13a07f98a2110bb8f99c7324601d66bff311f3070a669576a1598fe1d7de2d005d725d1f44dbe3934a9c0fd0b7950f60686047d4ce8d9d812310  firefox-115.9.1esr.source.tar.xz
3e0501ae7a650346c667dfdc0ae0ca286084f22e89ab2ac671cc0d7315673dc5b6dcb9f9882f6f39d26e9a31e57f7a0fd53d6b805e520224e22b8976850e2eb8  audio-lfs64.patch
454ea3263cabce099accbdc47aaf83be26a19f8b5a4568c01a7ef0384601cf8315efd86cd917f9c8bf419c2c845db89a905f3ff9a8eb0c8e41042e93aa96a85c  disable-moz-stackwalk.patch
f7b3b45ba04d05d17439d009bf0c9f27881e126f424e2257552338a0c1e3771ee1289c044babcb0920f62af62873a268c0cf524e1d35711e6dc8b808ca5e9f26  esr-metainfo.patch
2f4f15974d52de4bb273b62a332d13620945d284bbc6fe6bd0a1f58ff7388443bc1d3bf9c82cc31a8527aad92b0cd3a1bc41d0af5e1800e0dcbd7033e58ffd71  fix-fortify-system-wrappers.patch
cd68b89e29e5f6379fbd5679db27b9a5ef70ea65e51c0d0a8137e1f1fd210e35a8cfb047798e9549bc7275606d7ec5c8d8af1335d29da4699db7acd8bc7ff556  fix-rust-target.patch
305c874fdea3096e9c4c6aa6520ac64bb1c347c4b59db8360096646593fe684c3b5377874d91cecd33d56d1410b4714fbdea2b514923723ecbeff79d51265d9b  fix-webrtc-glibcisms.patch
afabea91b328c5a68eaa20f9099ac7b2d0e7f2423e816b05ed168bdd326a5684fa02de08bf05c6033e9b888f02775d1b0443a00329b7a632ee399122a391c13a  icu74.patch
5fa9382c692e4bd6a2634308f24a6526fd12a60a2563d2090056d43a60505df3ec9881bbf54562e69394467529b3b0dc45955afca46ed329af03cea074fff070  lfs64.patch
c0437a6753f3f350968fa12d250efdfe1bea77baf0e4c06b072b5cc9e78c774dbf4506bc536337030d349fb3ba4460097b75b0c7c5b8fb2d39d8b0a392948936  no-ccache-stats.patch
2d8dff86212d6d2a904cbb5a5a1d6c17b89adc929fc6a3f4c6cb669f5e83ecddff5a799225319ba445a187b04d111251af75dd3ce8a039164bc14d2a432a2a04  ppc-musttail.patch
6f60e83599041db1b707c21784197ea9816b2c936b89a274bfc24554a600981e6f28448fe41fab0942bd31acd49b1c00beb2eb0961149f2ffa6a4154be123ea7  ppc-webrtc.patch
4e40b34c5f77a1a21fe971a6fcd8a21b1a63423a3a7932a5a6e1c7a2779f9f06a561c806614a01931679a4b1c6afdfd8ae1f3cc6b673f259ccd368e8e54f6e90  python-deps.patch
1c6918dd6655d3a1251bfd4af2e1c561cbb00d540a883b4c1ebf7f5de530d754d9ac07b4b5f56cdab6c511d25c8910ec94043f5733e97501a67abffe1bafaeb1  rust-lto-thin.patch
2518f2fc75b5db30058e0735f47d60fdf1e7adfaeee4b33fb2afb1bd9a616ce943fd88f4404d0802d4083703f4acf1d5ad42377218d025bc768807fbaf7e1609  sandbox-fork.patch
b7d0a6126bdf6c0569f80aabf5b37ed2c7a35712eb8a0404a2d85381552f5555d4f97d213ea26cec6a45dc2785f22439376ed5f8e78b4fd664ef0223307b333e  sandbox-largefile.patch
f8c3555ef6207933cbffbf4fc101a9b4c0d2990c0063162f0f0bde70ef0b46f86bfac42e7110695183424a87948de593f3927b2d8509ede3e4fc7bd8a1fad1ce  sandbox-sched_setscheduler.patch
0b3f1e4b9fdc868e4738b5c81fd6c6128ce8885b260affcb9a65ff9d164d7232626ce1291aaea70132b3e3124f5e13fef4d39326b8e7173e362a823722a85127  stab.h
d354f48a29bfc16719f3b230b1395063239d4420f9e47522de4662392d9697b15f931ca3bf6055d100fa33d61a9a1a13477687d5eac99e50ae7dbef9882a5808  firefox.desktop
382510375b1a2fa79be0ab79e3391a021ae2c022429ffbaa7e7a69166f99bb56d01e59a1b10688592a29238f21c9d6977672bd77f9fae439b66bdfe0c55ddb15  mozilla-location.keys
fc45bc3ffb9404e5338ea26a9f04807b40f6f516324972cddd48bedb91b8bd7c6b8d4e03a0209020f5e67b703bc4ff89389985791b9bd544a0fc3951e2dc338e  vendor-prefs.js
e1a0a4ff5cc1b53f13776ca11927d671426b0691e78e74a4adf2166d57bb2ae8ac409cc11a37ce5e2f680fdf05d5bc3849c33a9717aca1bb62d03ae5231a67fb  zotero.desktop
e8442e68c53d0057947e4abb4d7ab2f4d52dbb88372d362006c43780d65cae1fe579ab167c772d36113c7937c296b7e4306027319757fb0d36f76a851be6e0e3  zotero-client-7.0.0_beta76.tar.gz
746dbabbb3ea9199d17891e2079b9256d04843f548132178862117d2334694d98e2cc981945d72f31d0e5b2c42904d371633f6905996bb580aa0b5ae95c64ddc  zotero_build-modifications.patch
cfe583f2da7508a4e07734a42519d7c28265928fe14c14b2f650ca6e56c35cb79272b03ed3bfb81e06c4e01458b55e04d10df5cbdd549da0d97206b2c10c3530  zotero_drop-phantomjs.patch
337070ee4c44ccb35c6b6290c18327740bb9fccfd1a6ad1045782e83daa290b6ced7d53955d3a889f661d588738a64f2e7f383639f4c46be9fdf891168abc9ff  zotero_test-drop-build.patch
f0e4f09496531222e8400959f9ef12852bca269eb3bf4c3b87ccaf92f28a12b9374461a1c79ad294a4393dbe30800aa1c85497033d0bc304fa8c198dfab3efd2  zotero_test-fix-chars.patch
9dc390d8bae42e645cae45fe5551751d8f38d5c1b8b2cc0eec1c2191f4bde293ffb2c67cfc4de765f2d48b1da4d5fcd4f1c03711e5da3180cd1b63710ccf5599  zotero_test-push-timeout-to-30sec.patch
"
