SUMMARY="A fork of Mozilla's email client Thunderbird"
DESCRIPTION="Thunderbird is an open source mail client. \
It achieves balance between ease of use and customization, catering to the \
needs of both casual and power users. So does its fork MailNews."
HOMEPAGE="https://github.com/mmadia/bezilla"
COPYRIGHT="1995-2009 Mozilla Developers and Contributors"
LICENSE="MPL v1.1"
REVISION="2"
srcGitRev="6862542e81d0407f8fbdb8cf96a48fdc3156e902"
SOURCE_URI="$HOMEPAGE/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="ecdb6e15bd3a48e4b3e036e7fe3c12341c36ddef621403c558a15441cf364d79"
SOURCE_DIR="bezilla-$srcGitRev"
PATCHES="mailnews-$portVersion.patchset"

ARCHITECTURES="all"

PROVIDES="
	mailnews = $portVersion
	cmd:thunderbird = $portVersion
	app:MailNews = $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="
	mailnews_devel = $portVersion
	cmd:thunderbird_config = $portVersion
	"
REQUIRES_devel="
	mailnews == $portVersion base
	"

BUILD_REQUIRES="
	haiku_devel
	devel:libIDL_2
	devel:libglib_2.0
	devel:libpng16
	"

if [ $effectiveTargetArchitecture == x86_64 ]; then
	BUILD_REQUIRES+="
	devel:libnspr4
	"
fi

BUILD_PREREQUIRES="
	cmd:gcc
	cmd:m4
	cmd:make
	cmd:gawk
	cmd:perl
	cmd:xargs
	cmd:tar
	cmd:zip
	cmd:autoconf_2.13
	cmd:libtoolize
	cmd:pkg_config
"

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=mail
	export MOZ_APP_NAME=MailNews
	export MOZ_APP_DISPLAYNAME=MailNews
	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=mail \
		--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 $appsDir

	mv $libDir/thunderbird-2.0.0.25pre $appsDir/MailNews
	mv $appsDir/MailNews/thunderbird-bin $appsDir/MailNews/MailNews

	# creating the lib and add-ons
	mkdir -p $appsDir/MailNews/{add-ons,lib}

	# arranging the files in lib and add-ons directories
	cd $appsDir/MailNews
	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 libldap50.so libmozjs.so \
	libprldap50.so lib

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

	cp $sourceDir/supporting-files/persdict.dat $appsDir/MailNews/defaults/profile

	# TODO: create Haiku theme based on browser widgets
	# cp $sourceDir/supporting-files/haiku-widget-theme/mailnews/classic.jar $appsDir/MailNews/chrome

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

	fixPkgconfig

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

	# TODO: fix $dist_bin/$MOZILLA_BIN in cmd:thunderbird script

	addAppDeskbarSymlink $appsDir/MailNews/MailNews
}
