# Template file for 'thunderbird'
#
# THIS PKG MUST BE SYNCHRONIZED WITH "srcpkgs/thunderbird-i18n".
#
pkgname=thunderbird
version=60.4.0
revision=1
short_desc="Standalone Mail/News reader"
maintainer="Enno Boland <gottox@voidlinux.org>"
homepage="https://www.thunderbird.net/"
license="MPL-2.0, GPL-2.0-or-later, LGPL-2.1-or-later"
distfiles="${MOZILLA_SITE}/${pkgname}/releases/${version}/source/${pkgname}-${version}.source.tar.xz"
checksum=27a6cdd9cfdd7ce70b1d193c64739db6be6b39eec308f0122d8472a6cd243e75

only_for_archs="i686 i686-musl x86_64 x86_64-musl"
lib32disabled=yes

hostmakedepends="autoconf213 unzip zip pkg-config perl python3 yasm rust cargo
 llvm clang"
makedepends="nss-devel libjpeg-turbo-devel gtk+-devel gtk+3-devel icu-devel
 pixman-devel sqlite-devel libevent-devel libnotify-devel libvpx-devel
 libXrender-devel libXcomposite-devel libSM-devel libXt-devel
 libXdamage-devel hunspell-devel $(vopt_if alsa alsa-lib-devel)
 $(vopt_if dbus dbus-glib-devel) $(vopt_if pulseaudio pulseaudio-devel)
 $(vopt_if startup_notification startup-notification-devel)
 $(vopt_if xscreensaver libXScrnSaver-devel)
 $(vopt_if sndio sndio-devel)"
depends="nss>=3.37.3 desktop-file-utils hicolor-icon-theme"

build_options="alsa dbus pulseaudio startup_notification xscreensaver sndio"
build_options_default="alsa dbus pulseaudio startup_notification xscreensaver sndio"

post_extract() {
	case "$XBPS_TARGET_MACHINE" in
	*-musl)
		cp "${FILESDIR}/stab.h" toolkit/crashreporter/google-breakpad/src/
		;;
	esac

	# Google API key (see http://www.chromium.org/developers/how-tos/api-keys)
	# Note: This is for Void Linux use ONLY.
	echo -n "AIzaSyCIFdBA7eQP43R6kXRwTq7j6Mvj1ITze90" > google-api-key

	# Mozilla API keys (see https://location.services.mozilla.com/api)
	# Note: This is for Void Linux use ONLY.
	echo -n "cd894504-7a2a-4263-abff-ff73ee89ffca" > mozilla-api-key
}
do_build() {
	cp "${FILESDIR}/mozconfig" "${wrksrc}/.mozconfig"

	case "$XBPS_TARGET_MACHINE" in
	*-musl)
		echo "ac_add_options --disable-jemalloc" >>.mozconfig
		echo "ac_add_options --disable-gold" >>.mozconfig
		echo "ac_add_options --enable-release" >>.mozconfig
		;;
	esac

	case "$XBPS_TARGET_MACHINE" in
	x86_64-musl|i686-musl)
		echo "ac_add_options --host=${XBPS_TRIPLET}" >>.mozconfig
		echo "ac_add_options --target=${XBPS_TRIPLET}" >>.mozconfig
		;;
	esac

	if [ "$CROSS_BUILD" ]; then
		export HOST_CFLAGS="${XBPS_CFLAGS}"
		export HOST_CXXFLAGS="${XBPS_CXXFLAGS}"
		export ac_cv_sqlite_secure_delete=yes \
			ac_cv_sqlite_threadsafe=yes \
			ac_cv_sqlite_enable_fts3=yes \
			ac_cv_sqlite_dbstat_vtab=yes \
			ac_cv_sqlite_enable_unlock_notify=yes \
			ac_cv_prog_hostcxx_works=1
		echo "ac_add_options --target=$XBPS_CROSS_TRIPLET" >>.mozconfig
	fi

	case "$XBPS_TARGET_MACHINE" in
	i686*)
		export CFLAGS+=" -D_FILE_OFFSET_BITS=64"
		export CXXFLAGS+=" -D_FILE_OFFSET_BITS=64"
		;;
	esac

	export LDFLAGS+=" -Wl,-rpath=/usr/lib/thunderbird"

	if [ "$SOURCE_DATE_EPOCH" ]; then
		export MOZ_BUILD_DATE=$(date --date "@$SOURCE_DATE_EPOCH" "+%Y%m%d%H%M%S")
	fi

	export MOZ_MAKE_FLAGS="${makejobs}"

	cat <<! >>.mozconfig
ac_add_options --with-google-api-keyfile="${wrksrc}/google-api-key"
ac_add_options --with-mozilla-api-keyfile="${wrksrc}/mozilla-api-key"
ac_add_options $(vopt_enable alsa)
ac_add_options $(vopt_enable sndio)
ac_add_options $(vopt_enable dbus)
ac_add_options $(vopt_enable dbus necko-wifi)
ac_add_options $(vopt_enable pulseaudio)
ac_add_options $(vopt_enable startup_notification startup-notification)
!

	rm -f old-configure
	./mach build
}
do_install() {
	DESTDIR="$DESTDIR" ./mach install

	vinstall ${FILESDIR}/vendor.js 644 usr/lib/thunderbird/defaults/preferences
	vinstall ${FILESDIR}/thunderbird.desktop 644 usr/share/applications

	for i in 16 22 24 32 48 256; do
		vinstall ./comm/mail/branding/thunderbird/default$i.png 644 \
			usr/share/icons/hicolor/${i}x${i}/apps thunderbird.png
	done

	# Use system-provided dictionaries
	rm -rf ${DESTDIR}/usr/lib/thunderbird/{dictionaries,hyphenation}
	ln -s /usr/share/hunspell ${DESTDIR}/usr/lib/thunderbird/dictionaries
	ln -s /usr/share/hyphen ${DESTDIR}/usr/lib/thunderbird/hyphenation

	# We don't want the development stuff
	rm -rf ${DESTDIR}/usr/{include,lib/thunderbird-devel,share/idl}

	# https://bugzilla.mozilla.org/show_bug.cgi?id=658850
	ln -sf thunderbird ${DESTDIR}/usr/lib/thunderbird/thunderbird-bin
}
