NAME=( mozilla  mozilla-devel )
VERSION=( 1.7.13  1.7.13 )
SOURCE=(http://ftp.mozilla.org/pub/mozilla.org/mozilla/releases/mozilla$VERSION/source/mozilla-$VERSION-source.tar.bz2
	mozilla-compose.desktop
	mozilla-mail.desktop
	mozilla.desktop
	mozilla-compose-icon.gif
	mozilla-icon.png
	mozilla-mail-icon.png )
PATCH=mozilla.moz_plugin_path.diff.gz
BUILD=1
OPTIMIZE=${OPTIMIZE:-"-Os -fomit-frame-pointer -pipe"}

build() {
	_explode 0
	cd mozilla

	# Finally Slack is getting rid of version dependent plugin
	# directory! Add /usr/lib/mozilla/plugins to MOZ_PLUGINS_PATH:
	_patch 0 -p1 --verbose --backup --suffix=.orig
	rm -f xpfe/bootstrap/mozilla.in.orig

	# I still prefer /usr/lib/mozilla-suite over /usr/lib/mozilla-$VERSION.
	./configure \
		--prefix=$PREFIX \
		--enable-optimize="$CFLAGS" \
		--disable-debug \
		--with-default-mozilla-five-home=$LIBDIR/mozilla-$VERSION \
		--enable-strip-libs \
		--disable-tests \
		--disable-short-wchar \
		--enable-nspr-autoconf \
		--enable-calendar \
		--enable-extensions=default,irc \
		--enable-crypto \
		--disable-xprint \
		--without-system-nspr \
		--with-system-zlib \
		--enable-default-toolkit=gtk2 \
		--disable-freetype2 \
		--enable-xft \
		--with-system-jpeg \
		--with-system-png \
		--disable-gnomevfs

	make -s export
	make -s libs
	
	make DESTDIR=$PKG install

	# Install the NSS headers.
	mkdir -p $PKG$INCLUDEDIR/nss
	find security/nss/lib -name "*.h" -type f -exec cp -a {} $PKG$INCLUDEDIR/nss \;

	# Move nss libraries into /usr/lib.  Unless this is done, some
	# things (like gaim's plugin for MSN) will be unable to load
	# them, even if they linked with them successfully.  AFAIKT,
	# these other things are at fault, but since this little trick
	# fixes the problem we'll do it.  BTW, Red Hat started this lib
	# moving mess for unknown reasons (didn't know how to link
	# properly?), so that's why other programs now expect to find
	# things arranged other than a standard "make install" layout...
	cd $PKG$LIBDIR/mozilla-$VERSION
	for nsslib in \
		libnspr4.so \
		libnss3.so \
		libplc4.so \
		libplds4.so \
		libsmime3.so \
		libsoftokn3.so \
		libsoftokn3.chk \
		libssl3.so \
		libnssckbi.so
	do
		# Move library:
		mv $nsslib $PKG$LIBDIR
		# Link to it... you don't just go messing with stuff
		# without leaving it "clean". IIRC, without these links,
		# Mozilla won't find certificates.
		ln -sf ../$nsslib .
	done
	cd -

	mkdir -p $PKG$LIBDIR/pkgconfig $PKG$DATADIR/aclocal $PKG$BINDIR
	cp nsprpub/config/nspr-config $PKG$BINDIR
	mv $PKG$INCLUDEDIR/mozilla-$VERSION/nspr $PKG$INCLUDEDIR

	# Headers are moved from usr/include/mozilla-$VERSION to
	# usr/include so fix pkgconfig files:
	sed -i "s,${INCLUDEDIR#/}/mozilla-$VERSION,${INCLUDEDIR#/}," $PKG$LIBDIR/pkgconfig/*
	sed -i "s,${LIBDIR#/}/mozilla-$VERSION,${LIBDIR#/}," $PKG$LIBDIR/pkgconfig/*

	# libsoftokn3.so is now stripped, calc new checksum:
	CWD2=$PWD
	cd $PKG$LIBDIR
	LD_LIBRARY_PATH=$PWD \
		"$CWD2/security/nss/cmd/shlibsign/Linux2.6_x86_glibc_PTH_OPT.OBJ/shlibsign" \
		-v -i libsoftokn3.so
	cd -

	# These are barely needed, save some space by putting them to
	# the devel package:
	mkdir -p ${PKG[1]}$DATADIR ${PKG[1]}$BINDIR ${PKG[1]}$LIBDIR/mozilla-suite
	mv $PKG$LIBDIR/pkgconfig ${PKG[1]}$LIBDIR
	mv $PKG$DATADIR/idl ${PKG[1]}$DATADIR
	mv $PKG$DATADIR/aclocal ${PKG[1]}$DATADIR
	mv $PKG$INCLUDEDIR ${PKG[1]}$INCLUDEDIR
	mv $PKG$LIBDIR/mozilla-$VERSION/TestGtkEmbed ${PKG[1]}$LIBDIR/mozilla-suite
	mv $PKG$BINDIR/mozilla-config ${PKG[1]}$BINDIR
	mv $PKG$BINDIR/nspr-config ${PKG[1]}$BINDIR

	# Icons
	mkdir -p $PKG$DATADIR/{applications,pixmaps}
	cp ${SOURCE[1]} ${SOURCE[2]} ${SOURCE[3]} $PKG$DATADIR/applications
	cp ${SOURCE[4]} ${SOURCE[5]} ${SOURCE[6]} $PKG$DATADIR/pixmaps

	# Some legal stuff
	_doc 0 mozilla-$VERSION  LEGAL LICENSE README.txt


	# There is a bunch of unneeded empty dirs.
	_rm_empty_dirs 0

	# Some .js files have execute bit set:
	find $PKG$LIBDIR -type f -name '*.js' -exec chmod 0644 {} \;

	# Rename mozilla-$VERSION to mozilla-suite and fix paths in usr/bin/mozilla:
	mv $PKG$LIBDIR/mozilla-$VERSION $PKG$LIBDIR/mozilla-suite
	sed -i "s,${LIBDIR#/}/mozilla-$VERSION,${LIBDIR#/}/mozilla-suite," $PKG$BINDIR/mozilla

	# Create empty directory for shared plugins:
	mkdir -p $PKG$LIBDIR/mozilla/plugins

	# Fix pkgconfig files - TODO: possible lib64 issue?
	cd ${PKG[1]}$LIBDIR/pkgconfig
	sed -i "s,$INCLUDEDIR,$INCLUDEDIR/mozilla,
		s,$LIBDIR,$LIBDIR/mozilla-suite," \
		mozilla-gtkmozembed.pc \
		mozilla-js.pc \
		mozilla-plugin.pc \
		mozilla-xpcom.pc

	# Version independent symlink
	ln -s mozilla-$VERSION ${PKG[1]}$INCLUDEDIR/mozilla

	_chfix
	_chgrp_bin
}
