# Contributor: Joseph Benden <joe@benden.us>
# Maintainer: Joseph Benden <joe@benden.us>

pkgname=thunderbird
pkgver=68.5.0
_pkgver=$pkgver
_xulver=$pkgver
pkgrel=0
pkgdesc="Thunderbird email client"
url="https://www.thunderbird.net/"
arch="all !s390x !armhf" # limited by rust and cargo. Fails on armhf due to bad assembly
license="GPL-3.0-or-later AND LGPL-2.1-or-later AND MPL-2.0"
options="!check !strip"
depends="
	alsa-lib
	dbus-glib
	ffmpeg
	gtk+3.0
	hunspell
	icu
	libevent
	libjpeg-turbo
	libnotify
	libogg
	libtheora
	libvorbis
	libxcomposite
	libxt
	nspr
	nss
	sqlite
	startup-notification
	"
makedepends="
	alsa-lib-dev
	autoconf2.13
	automake
	bsd-compat-headers
	bzip2-dev
	cargo
	cbindgen
	clang-dev
	dbus-glib-dev
	ffmpeg-dev
	gtk+2.0-dev
	gtk+3.0-dev
	hunspell-dev
	icu-dev
	libevent-dev
	libidl-dev
	libjpeg-turbo-dev
	libnotify-dev
	libogg-dev
	libtheora-dev
	libtool
	libvorbis-dev
	libxcomposite-dev
	libxt-dev
	llvm9-dev
	mesa-dev
	nasm
	nodejs
	nspr-dev
	nss-dev
	nss-static
	python2
	python3-dev
	sed
	sqlite-dev
	startup-notification-dev
	wireless-tools-dev
	yasm
	zip
	"
source="https://ftp.mozilla.org/pub/thunderbird/releases/$pkgver/source/thunderbird-$pkgver.source.tar.xz
	stab.h

	disable-ccache-stats.patch
	fix-fortify-system-wrappers.patch
	fix-seccomp-bpf.patch
	fix-toolkit.patch
	fix-tools.patch
	mallinfo.patch

	disable-moz-stackwalk.patch
	fix-musl.patch
	fix-rust-target.patch
	fix-webrtc-glibcisms.patch
	fix-sandbox-membarrier.patch
	fd6847c9416f9eebde636e21d794d25d1be8791d.patch
	thunderbird.desktop
	"
# secfixes:
#   68.5.0-r0:
#     - CVE-2020-6793
#     - CVE-2020-6794
#     - CVE-2020-6795
#     - CVE-2020-6797
#     - CVE-2020-6798
#     - CVE-2020-6792
#     - CVE-2020-6800

_mozappdir=/usr/lib/$pkgname

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

# 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
}

prepare() {
	default_prepare

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

	# https://bugzilla.mozilla.org/show_bug.cgi?id=1341234
	mkdir -p "$builddir"/objdir
	echo "ac_add_options BINDGEN_CFLAGS='-I/usr/include/nspr -I/usr/include/pixman-1'" >>objdir/.mozconfig

	_clear_vendor_checksums cssparser
}

build() {
	mkdir -p "$builddir"/objdir
	cd "$builddir"/objdir

	sed -e 's/checkImpl/checkFFImpl/g' -i ../js/src/vm/JSContext*.h

	export SHELL=/bin/sh
	export BUILD_OFFICIAL=1
	export MOZILLA_OFFICIAL=1
	export USE_SHORT_LIBNAME=1
	export MACH_NO_TERMINAL_FOOTER=1
	export TERM=ansi-generic

	# gcc 6
	export CXXFLAGS="-fno-delete-null-pointer-checks -fno-schedule-insns2"

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

	# ignore compiler warnings
	export CXXFLAGS="$CXXFLAGS -Wno-class-memaccess -Wno-multistatement-macros -Wno-ignored-qualifiers"
	export LDFLAGS="$LDFLAGS -Wno-subobject-linkage"

	case "$CARCH" in
		x86)
			# disable-elf-hack: exists only on arm, x86, x86_64
			_arch_config="--disable-elf-hack"
			export RUST_TARGET="i686-unknown-linux-musl"
			;;
		x86_64)
			# disable-elf-hack: exists only on arm, x86, x86_64
			_arch_config="--disable-elf-hack"
			export RUST_TARGET="$CTARGET"
			;;
		aarch64)
			export RUST_TARGET="aarch64-unknown-linux-musl"
			;;
		armv7)
			# disable-elf-hack: exists only on arm, x86, x86_64
			_arch_config="--disable-elf-hack"
			export RUST_TARGET="armv7-unknown-linux-musleabihf"
			;;
		armhf)
			# disable-elf-hack: exists only on arm, x86, x86_64
			_arch_config="--disable-elf-hack"
			export RUST_TARGET="arm-unknown-linux-musleabihf"
			;;
		ppc64le)
			export RUST_TARGET="powerpc64le-unknown-linux-musl"
			;;
	esac

	# TB doesn't have SIMD available on these arches.
	case "$CARCH" in
		armhf|armv7)
			_rust_simd="--disable-rust-simd"
			_low_mem_flags="--disable-debug-symbols --disable-debug"
			export RUSTFLAGS="$RUSTFLAGS -C debuginfo=0"
			;;
		x86)
			_low_mem_flags="--disable-debug-symbols --disable-debug"
			export RUSTFLAGS="$RUSTFLAGS -C debuginfo=0"
			;;
		*) _rust_simd="--enable-rust-simd" ;;
	esac

	../configure \
		--prefix=/usr \
		--libdir=/usr/lib \
		$_arch_config \
		\
		--disable-crashreporter \
		--disable-gold \
		--disable-install-strip \
		--disable-jemalloc \
		--disable-profiling \
		--disable-pulseaudio \
		--disable-strip \
		--disable-tests \
		--disable-updater \
		--disable-debug \
		--disable-debug-symbols \
		\
		--with-distribution-id=org.alpinelinux \
		--enable-alsa \
		--enable-application=comm/mail \
		--enable-default-toolkit=cairo-gtk3 \
		--enable-official-branding \
		--enable-optimize="$CFLAGS -O2 -fno-ident -fmerge-all-constants -ffunction-sections -fdata-sections -Wno-maybe-uninitialized -Wno-implicit-fallthrough -Wno-unused-function" \
		--enable-release \
		--enable-startup-notification \
		--enable-system-ffi \
		--enable-system-sqlite \
		--enable-ffmpeg \
		$_rust_simd \
		$_low_mem_flags \
		--enable-hardening \
		\
		--with-system-bz2 \
		--with-system-icu \
		--with-system-libevent \
		--with-system-nspr \
		--with-system-nss \
		--with-system-pixman \
		--with-system-png \
		--with-system-zlib \
		--without-ccache \
		--with-clang-path=/usr/bin/clang \
		--with-libclang-path=/usr/lib
		# FIXME: fix build with --with-system-libvpx and libvpx 1.8.0
		# https://bugzilla.mozilla.org/show_bug.cgi?id=1525393
	make
}

package() {
	cd "$builddir"/objdir

	DESTDIR="$pkgdir" \
	MOZ_MAKE_FLAGS="$MAKEOPTS" \
		../mach install

	install -m755 -d ${pkgdir}/usr/share/applications
	install -m755 -d ${pkgdir}/usr/share/pixmaps
	install -m755 -d ${pkgdir}/usr/share/icons
	install -m755 -d ${pkgdir}/usr/share/icons/hicolor

	for i in 16 22 24 32 48 64 128 256; do
		install -Dm644 "$builddir"/comm/mail/branding/thunderbird/default${i}.png \
			"$pkgdir/usr/share/icons/hicolor/${i}x${i}/apps/$pkgname.png"
	done
	install -Dm644 "$builddir"/comm/mail/branding/thunderbird/TB-symbolic.svg \
		"$pkgdir/usr/share/icons/hicolor/symbolic/apps/thunderbird-symbolic.svg"
	install -Dm644 "$builddir"/comm/mail/branding/thunderbird/default48.png \
		${pkgdir}/usr/share/pixmaps/$pkgname.png

	install -m644 ${srcdir}/$pkgname.desktop ${pkgdir}/usr/share/applications/$pkgname.desktop

	_vendorjs="$pkgdir/usr/lib/$pkgname/defaults/preferences/vendor.js"
	install -Dm644 /dev/stdin "$_vendorjs" <<END
// Use LANG environment variable to choose locale
pref("intl.locale.requested", "");

// Use system-provided dictionaries
pref("spellchecker.dictionary_path", "/usr/share/hunspell");

// Disable default mailer checking.
pref("mail.shell.checkDefaultMail", false);

// Don't disable our bundled extensions in the application directory
pref("extensions.autoDisableScopes", 11);
pref("extensions.shownSelectionUI", true);
END

	_distini="$pkgdir/usr/lib/$pkgname/distribution/distribution.ini"
	install -Dm644 /dev/stdin "$_distini" <<END
[Global]
id=alpinelinux
version=1.0
about=Mozilla Thunderbird for Alpine Linux

[Preferences]
app.distributor=alpinelinux
app.distributor.channel=$pkgname
app.partner.alpinelinux=alpinelinux
END

	# Install a wrapper to avoid confusion about binary path
	install -Dm755 /dev/stdin "$pkgdir/usr/bin/$pkgname" <<END
#!/bin/sh
exec /usr/lib/$pkgname/thunderbird "\$@"
END

	# Replace duplicate binary with wrapper
	# https://bugzilla.mozilla.org/show_bug.cgi?id=658850
	ln -snf "$pkgdir/usr/bin/$pkgname" \
		"$pkgdir/usr/lib/$pkgname/thunderbird-bin"
}

sha512sums="bb58a398f24d89eac5b1816f9d16edadbe65cdac6542e1f6798424c7ea18eb2d4ca46bb03f54c8c365e67d1dec44d3dfb36fbe4f85afe9ad80fbcc1f2984084b  thunderbird-68.5.0.source.tar.xz
0b3f1e4b9fdc868e4738b5c81fd6c6128ce8885b260affcb9a65ff9d164d7232626ce1291aaea70132b3e3124f5e13fef4d39326b8e7173e362a823722a85127  stab.h
3e57b2541f3889a1cf701860c8bf299f294cb077b0e8cfffda271cf02d1f59b25a697485d0df66e847e47f795b1ace54488a84a42851f2f8587717335537b44d  disable-ccache-stats.patch
2f4f15974d52de4bb273b62a332d13620945d284bbc6fe6bd0a1f58ff7388443bc1d3bf9c82cc31a8527aad92b0cd3a1bc41d0af5e1800e0dcbd7033e58ffd71  fix-fortify-system-wrappers.patch
84b84d2d7dbc16002510bf856796ad345ac38ef6d3254670230189bba7c2d4781714d231236d5a3d70129a4597b430c3171644b01ad0f5a5bb13b55d407337a4  fix-seccomp-bpf.patch
2c65ea7280e6e89826ebad563ee25203a99ff0b4ba8fc60ec261ada6c69874d649c6ac92fcecc6307a6e5a00de27d7956acf944d556ddfadec0411be16f4e0b8  fix-toolkit.patch
4d55f41d15be7457ad630f8f07e4fc0314c2f75720010b4bbe6a2a7f3228210a1e069949e11795efbe2e784b0762e79fdfe5b8ec38e8a64cb8d9cf3b57dd5af1  fix-tools.patch
a4a3e062661bda64d502d426c480ac9645345860118de9df9ffe6e0597738c70c11e5cdef2d4fd12c5e2ee30a09310159230524655a419a4f7e4eeeb0f3c06b0  mallinfo.patch
454ea3263cabce099accbdc47aaf83be26a19f8b5a4568c01a7ef0384601cf8315efd86cd917f9c8bf419c2c845db89a905f3ff9a8eb0c8e41042e93aa96a85c  disable-moz-stackwalk.patch
e0df4fc649012f023443ce9165da29d36459dbca5df64a31008b28d96264fba909858de36d0cf1b1cb1aab898342413f0cd77c90ebe21d1f9a0504631d6d1d0e  fix-musl.patch
1bf1f579ced0ca47d156eb45962114f9867cf224c2ba34dfc106227648322ecf729243d2a6e84b72cb011abadc36ed84990960bded764fd4243fc9cb22084ce5  fix-rust-target.patch
d35cacb9ede80e6bfbef0709823e536dddfb1c02d776275b0b7adb5969e9927d8c6117df96873569c3f3db0a18ee5db24f8086a9311a05077892be43a3dd8d79  fix-webrtc-glibcisms.patch
f85f2c19c3dafab915bcb40e580fc442fd9eab5916696849edf0b105c758dd807dfe23a6479935613c81496711eb377c73227c03eb8582204c3442a4d0e397a2  fix-sandbox-membarrier.patch
60845dcb034b2c4459c30f7d5f25c8176cf42df794e2cc0e86c3e2abb6541c24b962f3a16ca70a288d4d6f377b68d00b2904b22463108559612053d835d9bff1  fd6847c9416f9eebde636e21d794d25d1be8791d.patch
95a2b1deb4f6c90750fdd2bfe8ca0a7879a5b267965091705a6beb0a0a4b1ccad75d11df7b9885543ca4232ff704e975c6946f4c11804cb71c471e06f9576001  thunderbird.desktop"
