# Template build file for 'icecat'.
pkgname=icecat
version=52.0.2
revision=2
short_desc="GNU version of the Firefox browser"
maintainer="Jürgen Buchmüller <pullmoll@t-online.de>"
homepage="https://www.gnu.org/software/${pkgname}/"
license="MPL-1.1, GPL-2, LGPL-2.1"
distfiles="${GNU_SITE}/${pkgname}/${version}/${pkgname}-${version}-gnu1.tar.bz2"
checksum=8901a4ab7f2b87d5516c77cbdec6d276cdde64421725d4ed613c1b4f805a4a2b

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

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

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

desc_option_gtk3="Build the GTK+3 toolkit UI"

# CFLAGS and CXXFLAGS to set work around code which gcc6 would
# otherwise regard as out-of-specification and allow it to produce a
# working program.
CFLAGS="-fno-delete-null-pointer-checks -fno-lifetime-dse -fno-schedule-insns2"
CXXFLAGS="-fno-delete-null-pointer-checks -fno-lifetime-dse -fno-schedule-insns2"

post_extract() {
	case "$XBPS_TARGET_MACHINE" in
	*-musl)
		# fix musl rust triplet
		sed -i "s/\(x86_64-unknown-linux\)-gnu/\1-musl/" build/moz.configure/rust.configure
		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
}
do_build() {
	cp ${FILESDIR}/mozconfig .mozconfig

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

	# echo "ac_cv_sqlite_threadsafe=yes" >> .mozconfig
	# echo "ac_cv_sqlite_enable_fts3=yes" >> .mozconfig
	# echo "ac_cv_sqlite_enable_unlock_notify=yes" >> .mozconfig
	# echo "ac_cv_prog_hostcxx_works=1" >> .mozconfig

	mkdir -p /usr/lib/icecat
	export LDFLAGS+=" -Wl,-rpath=/usr/lib/icecat"

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

        cat >>.mozconfig <<EOF
ac_add_options --with-google-api-keyfile="${wrksrc}/google-api-key"
ac_add_options --enable-default-toolkit=cairo-gtk$(vopt_if gtk3 '3' '2')
ac_add_options $(vopt_enable alsa)
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 rust)
ac_add_options $(vopt_enable startup_notification startup-notification)
EOF

	export MOZ_MAKE_FLAGS="${makejobs}"
	make -f client.mk build

}
do_install() {
	make -f client.mk DESTDIR="${DESTDIR}" install

	vinstall ${FILESDIR}/vendor.js 644 usr/lib/icecat/defaults/pref
	vinstall ${FILESDIR}/icecat.desktop 644 usr/share/applications
	vinstall ${FILESDIR}/icecat-safe.desktop 644 usr/share/applications

	for i in 16x16 22x22 24x24 32x32 48x48 256x256; do
		vinstall ${wrksrc}/browser/branding/official/default${i%x*}.png 644 \
			usr/share/icons/hicolor/${i}/apps icecat.png
	done
	vinstall ${wrksrc}/browser/branding/official/default256.png 644 \
		usr/share/pixmaps icecat.png

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

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

	# Remove the development files - noone uses them and the subpkg was broken
	rm -rf ${DESTDIR}/usr/{include,lib/icecat-devel,share/idl}
}
