#!/bin/sh

NAME="thunderbird"
DESC="Thunderbird email client"

MAKEDEPS=" alsa-lib automake botan cargo cbindgen clang dbus-glib gettext gtk3 icu json-c libevent libffi libjpeg-turbo libnotify libogg libtheora libtool libvorbis libvpx libwebp libxcomposite libxt llvm m4 mesa nasm nodejs nspr nss pipewire pulseaudio python sed wireless-tools zip"

PKG_VER=91.8.0
SOURCE="https://ftp.mozilla.org/pub/thunderbird/releases/$PKG_VER/source/thunderbird-$PKG_VER.source.tar.xz"

ADDITIONAL="
allow-custom-rust-vendor.patch
avoid-redefinition.patch
disable-moz-stackwalk.patch
disable-neon-in-aom.patch
fix-fortify-system-wrappers.patch
fix-rust-target.patch
fix-tools.patch
fix-webrtc-glibcisms.patch
mallinfo.patch
sandbox-fork.patch
sandbox-largefile.patch
sandbox-sched_setscheduler.patch
stab.h
thunderbird.desktop
"

mozappdir=/usr/lib/thunderbird

_clear_vendor_checksums() {
	sed -i 's/\("files":{\)[^}]*/\1/' third_party/rust/$1/.cargo-checksum.json
}

prepare() {
    apply_patches
	cp "$BUILD_ROOT"/stab.h toolkit/crashreporter/google-breakpad/src/

	_clear_vendor_checksums audio_thread_priority
	_clear_vendor_checksums target-lexicon-0.9.0
}

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

	export SHELL=/bin/sh
	export BUILD_OFFICIAL=1
	export MOZILLA_OFFICIAL=1
	export USE_SHORT_LIBNAME=1
	export MACH_USE_SYSTEM_PYTHON=1
	export MOZBUILD_STATE_PATH="$BUILD_ROOT"/mozbuild
	# 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"

	../mach configure \
		--prefix=/usr \
        --disable-elf-hack \
        --enable-rust-simd \
		\
		--disable-crashreporter \
		--disable-gold \
		--disable-install-strip \
		--disable-jemalloc \
		--disable-profiling \
		--disable-strip \
		--disable-tests \
		--disable-updater \
		\
		--enable-alsa \
		--enable-application=comm/mail \
		--enable-dbus \
		--enable-default-toolkit=cairo-gtk3-wayland \
		--enable-ffmpeg \
		--enable-hardening \
		--enable-necko-wifi \
		--enable-official-branding \
		--enable-openpgp \
		--enable-optimize="$CFLAGS -O2" \
		--enable-pulseaudio \
		--enable-release \
		--enable-smoosh \
		--enable-system-ffi \
		--enable-system-pixman \
		\
		--with-system-botan \
		--with-system-ffi \
		--with-system-icu \
		--with-system-jpeg \
		--with-system-jsonc \
		--with-system-libevent \
		--with-system-libvpx \
		--with-system-nspr \
		--with-system-nss \
		--with-system-pixman \
		--with-system-png \
		--with-system-webp \
		--with-system-zlib \
		--with-libclang-path=/usr/lib
	../mach build
}

package() {
	cd "$BUILD_ROOT"/objdir

	DESTDIR="$PKG_DEST" MOZ_MAKE_FLAGS="$MAKEOPTS" ../mach install

	install -m755 -d "$PKG_DEST"/usr/share/applications
	install -m755 -d "$PKG_DEST"/usr/share/pixmaps

	local _png
	for _png in "$BUILD_ROOT"/comm/mail/branding/thunderbird/default*.png; do
		local i=${_png%.png}
		i=${i##*/default}
		install -D -m644 "$_png" "$PKG_DEST"/usr/share/icons/hicolor/"$i"x"$i"/apps/thunderbird.png
	done

	install -Dm644 "$BUILD_ROOT"/comm/mail/branding/thunderbird/TB-symbolic.svg \
		"$PKG_DEST/usr/share/icons/hicolor/symbolic/apps/thunderbird-symbolic.svg"
	install -Dm644 "$BUILD_ROOT"/comm/mail/branding/thunderbird/default48.png \
		$PKG_DEST/usr/share/pixmaps/thunderbird.png

	install -m644 "$BUILD_ROOT"/thunderbird.desktop "$PKG_DEST"/usr/share/applications/thunderbird.desktop

	# Add StartupWMClass=firefox on the .desktop files so Desktop Environments
	# correctly associate the window with their icon, the correct fix is to have
	# thunderbird sets its own AppID but this will work for the meantime
	# See: https://bugzilla.mozilla.org/show_bug.cgi?id=1607399
	echo "StartupWMClass=thunderbird" >> "$PKG_DEST"/usr/share/applications/thunderbird.desktop

	# install our vendor prefs
	install -d "$PKG_DEST"/$mozappdir/defaults/preferences

	cat >> "$PKG_DEST"/$mozappdir/defaults/preferences/vendor.js <<- EOF
	// 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);
	EOF

	install -d "$PKG_DEST"/$mozappdir/distribution

	cat >> "$PKG_DEST"/$mozappdir/distribution/distribution.ini <<- EOF
	[Global]
	id=alpinelinux
	version=1.0
	about=Mozilla Thunderbird for Alpine Linux

	[Preferences]
	app.distributor=alpinelinux
	app.distributor.channel=thunderbird
	app.partner.alpinelinux=alpinelinux
	EOF

	# Replace duplicate binary with wrapper
	# https://bugzilla.mozilla.org/show_bug.cgi?id=658850
	ln -snf thunderbird "$PKG_DEST/$mozappdir/thunderbird-bin"
}