SUMMARY="Fork of Mozilla Firefox for Haiku"
DESCRIPTION="BeZilla is an open source web browser. \
It achieves balance between ease of use and customization, catering to the \
needs of both casual and power users."
HOMEPAGE="https://github.com/mmadia/bezilla/"
COPYRIGHT="1995-2009 Mozilla Developers and Contributors"
LICENSE="MPL v1.1"
REVISION="8"
srcGitRev="6862542e81d0407f8fbdb8cf96a48fdc3156e902"
SOURCE_URI="https://github.com/mmadia/bezilla/archive/$srcGitRev.zip"
CHECKSUM_SHA256="61dd1e12fc0a183d2ded7cbc16adbe1fb7bee565ce7d12acc48bfbe5954f80ce"
SOURCE_DIR="bezilla-$srcGitRev"
PATCHES="bezilla-$portVersion.patchset"

ARCHITECTURES="?all x86_gcc2"

PROVIDES="
	bezilla= $portVersion
	app:BeZilla= $portVersion
	cmd:firefox= $portVersion
	"
REQUIRES="
	haiku
	lib:libglib_2.0
	lib:libIDL_2
	lib:libintl
	lib:libpng16
	lib:libz
	"
if [ $effectiveTargetArchitecture == x86_64 ]; then
	REQUIRES+="
	lib:libnspr4
	"
fi

PROVIDES_devel="
	bezilla_devel = $portVersion
	cmd:firefox_config= $portVersion
	"
REQUIRES_devel="
	bezilla== $portVersion base
	"

BUILD_REQUIRES="
	haiku_devel
	devel:libglib_2.0
	devel:libIDL_2
	devel:libpng16
	"
if [ $effectiveTargetArchitecture == x86_64 ]; then
	BUILD_REQUIRES+="
	devel:libnspr4
	"
fi
BUILD_PREREQUIRES="
	cmd:autoconf_2.13
	cmd:gawk
	cmd:gcc
	cmd:libtoolize
	cmd:m4
	cmd:make
	cmd:perl
	cmd:pkg_config
	cmd:tar
	cmd:xargs
	cmd:zip
	"

BUILD()
{
	cd mozilla
	pushd build/autoconf
	chmod +x mozconfig-find
	libtoolize -fci
	popd
	autoconf-2.13

	# builtin version of libnspr4 doesn't built on x86_64
	if [ $effectiveTargetArchitecture = x86_gcc2 ]; then
		pushd nsprpub
		autoconf-2.13
		popd
	fi

	pushd directory/c-sdk
	autoconf-2.13
	popd

	export MOZ_CO_PROJECT=browser
	export MOZ_APP_NAME=BeZilla
	export MOZ_APP_DISPLAYNAME=BeZilla
	export AUTOCONF='autoconf-2.13'
	export BUILD_OPT=1

	if [ $effectiveTargetArchitecture == x86_64 ]; then
		opt_flags='--enable-optimize=-O3 -fno-strict-aliasing -fpermissive'
	fi

	if [ $effectiveTargetArchitecture = x86_gcc2 ]; then
		opt_flags='--enable-optimize=-O3 -fthread-jumps -fforce-addr -frerun-cse-after-loop -frerun-loop-opt -fexpensive-optimizations -malign-functions=4 -malign-jumps=4'
	fi

	# export MOZCONFIG=../supporting-files/mozilla-configs/browser.mozconfig
	# runConfigure breaks configure with opt_flags
	# runConfigure --omit-dirs 'docDir dataRootDir' configure

	./configure \
		--prefix=$prefix \
		--bindir=$binDir \
		--libdir=$libDir \
		--datadir=$prefix/data \
		--includedir=$includeDir \
		--mandir=$manDir \
		--enable-application=browser \
		--disable-updater \
		--disable-oji \
		--disable-svg \
		--disable-canvas \
		--with-system-png \
		--with-system-zlib \
		--with-system-nspr \
		--enable-nspr-threads \
		--disable-tests \
		--disable-ipv6 \
		--enable-cpp-rtti \
		--disable-cpp-exceptions \
		--disable-pedantic \
		--enable-static --disable-shared \
		--enable-strip \
		--enable-reorder \
		--disable-debug \
		"$opt_flags"

	make $jobArgs
}

INSTALL()
{
	cd mozilla
	make install

	mkdir -p $appsDir
	# TODO: fix mozappdir in config/autoconf.mk.in to avoid manual renaming
	# -mozappdir	= $(libdir)/$(MOZ_APP_NAME)-$(MOZ_APP_VERSION)
	# +mozappdir	= $(prefix)/apps/BeZilla

	mv $libDir/firefox-2.0.0.22pre $appsDir/BeZilla

	mkdir -p $appsDir/BeZilla/{add-ons,lib}

	# arranging the files in lib and add-ons directories
	cd $appsDir/BeZilla
	mv libnspr4.so libnss3.so libnssutil3.so libplc4.so libplds4.so \
	libsmime3.so libsoftokn3.so libsqlite3.so libssl3.so libxpcom.so \
	libxpcom_compat.so libxpcom_core.so libmozjs.so lib

	mv libfreebl3.so libnssckbi.so libnssdbm3.so libxpistub.so add-ons

	mv firefox-bin BeZilla

	cp $sourceDir/supporting-files/persdict.dat $appsDir/BeZilla/defaults/profile
	cp $sourceDir/supporting-files/haiku-widget-theme/browser/classic.jar $appsDir/BeZilla/chrome

	ln -sf ../lib/libsoftokn3.so add-ons

	fixPkgconfig

	packageEntries devel \
		$developDir \
		$dataDir/idl \
		$dataDir/aclocal \
		$binDir/firefox-config

	addAppDeskbarSymlink $appsDir/BeZilla/BeZilla
}
