### this file is sourced not run
PKGVER=50.0
PKGBUILD=1
PKGARCH=x86_64

# source: BLFS 2014-06-06 modified to latest version
TARBALL=firefox-$PKGVER.source.tar.xz
MD5SUM=743aeb5d71eb6a8f227aad954f8a663a
SRC_URL=http://ftp.mozilla.org/pub/firefox/releases/$PKGVER/source/$TARBALL
BUNDLE=

SLACKREQ='alsa-lib gtk2 ffmpeg' # build-time: autoconf213 
SLACKDESC="firefox-gtk2: firefox-gtk2 $PKGVER (Web browser)  
firefox-gtk2:  
firefox-gtk2: Firefox is a stand-alone browser based on the Mozilla codebase.  
firefox-gtk2: 
firefox-gtk2: 
firefox-gtk2: 
firefox-gtk2: 
firefox-gtk2: 
firefox-gtk2: 
firefox-gtk2: 
firefox-gtk2:  
"

### default pkg_download
### default pkg_prepare
#pkg_prepare() {
#	mkdir tmp/firefox-*
#	cp -a /mnt/sda5/play/firefox-build-dir tmp/firefox-* # USE-PREBUILT
#}

### package
pkg_package() {
	# cp -a tmp/firefox-*/firefox-build-dir /mnt/sda5/play # backup the build output
	rm -rf usr/$LIBDIR/firefox-$PKGVER/defaults/profile &&
	tar -xf $CHROOT_DIR/tmp/pkg/ff-profile.tar.bz2 -C usr/$LIBDIR/firefox-$PKGVER/defaults
}

### build
pkg_build() {
	! [ -e /usr/bin/env ] && ln -s /tools/bin/env /usr/bin

	cd /tmp/firefox-* &&
	###Reverse patch that did away with default profile copying. Bug 1234012.	
	patch -RNp1 -i ../pkg/0002-Bug-1234012-Do-not-try-to-copy-files-from-the-non-ex.patch &&  
	patch -RNp1 -i ../pkg/0003-Bug-1234012-Remove-the-NS_APP_PROFILE_DEFAULTS_-NLOC.patch &&

#################### MOZ-BUILD START ###################	
#<< "USE-PREBUILT"	
	export SHELL=/bin/sh PYTHON=python2.7-$USE_ARCH
	#export PATH=/usr/lib/distcc/bin:$PATH DISTCC_HOSTS='james-pc,lzo localhost'
	cat > mozconfig << EOF
# If you have a multicore machine you can speed up the build by running
# several jobs at once, but if you have a single core, delete this line:
mk_add_options MOZ_MAKE_FLAGS="$MAKEFLAGS" # if not defined mozilla will use avail CPUs

# If you have installed DBus-Glib delete this option:
ac_add_options --disable-dbus

# If you have installed wireless-tools delete this option:
ac_add_options --disable-necko-wifi

# If you have installed libnotify delete this option:
# ac_add_options --disable-libnotify # doesn't work for firefox 50

# Uncomment these if you have installed them:
# ac_add_options --enable-startup-notification
# ac_add_options --enable-system-hunspell
# ac_add_options --enable-system-sqlite
# ac_add_options --with-system-libevent
# ac_add_options --with-system-libvpx
# ac_add_options --with-system-nspr
# ac_add_options --with-system-nss

mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/firefox-build-dir
ac_add_options --disable-crashreporter
ac_add_options --disable-debug
ac_add_options --disable-debug-symbols
# ac_add_options --disable-installer # no longer works for firefox 50
# ac_add_options --disable-static    # no longer works for firefox 50
# ac_add_options --enable-shared     # no longer works for firefox 50
ac_add_options --disable-tests
ac_add_options --disable-updater
ac_add_options --enable-application=browser
ac_add_options --enable-system-ffi
ac_add_options --prefix=/usr
ac_add_options --libdir=/usr/$LIBDIR
ac_add_options --with-pthreads
ac_add_options --with-system-jpeg
# ac_add_options --with-system-png
ac_add_options --with-system-zlib
ac_add_options --with-system-bz2
ac_add_options --enable-optimize=-Os
ac_add_options --enable-strip
#ac_add_options --disable-elf-hack
#ac_add_options --with-arm-kuser
ac_add_options --disable-pulseaudio
ac_add_options --enable-default-toolkit=cairo-gtk2
export LDFLAGS="-Wl,--no-keep-memory -Wl,--reduce-memory-overheads"
EOF
	make -f client.mk && 
	make -C firefox-build-dir/browser/installer &&
#USE-PREBUILT
#################### MOZ-BUILD END ###################

	# install
	rm -rf /usr/$LIBDIR/firefox /usr/$LIBDIR/firefox-$PKGVER &&
	cp -a firefox-build-dir/dist/firefox /usr/$LIBDIR/firefox-$PKGVER &&
	ln -sf firefox-$PKGVER /usr/$LIBDIR/firefox &&
	ln -sf /usr/$LIBDIR/firefox/firefox /usr/bin/firefox &&
	ln -sfv /usr/$LIBDIR/firefox-$PKGVER/browser/icons/mozicon128.png \
        /usr/share/pixmaps/firefox.png &&
    ln -s /usr/$LIBDIR/firefox-$PKGVER/defaults /usr/$LIBDIR/firefox-$PKGVER/browser/defaults
    
	# plugins dir
	mkdir -pv /usr/$LIBDIR/mozilla/plugins                   &&
	ln -sfv ../mozilla/plugins /usr/$LIBDIR/firefox-$PKGVER &&
		
	# "fast" firefox by uncompressing omni.ja - thanks to BarryK
	# enable this only when firefox is built-in into basesfs
<< "FAST"
	echo "unpacking omni.ja" &&
	cd /usr/$LIBDIR/firefox &&
	mkdir xxx && cd xxx && 
	{ unzip ../omni.ja; rm ../omni.ja; zip -r0 ../omni.ja *; } > /dev/null &&
	cd .. && rm -rf xxx &&
FAST
	
	pkg_build_slackdesc && 	
	rm -f /usr/bin/env &&
	rm -f /root/.distlib &&

	# run firefox as spot
	install -m755 /tmp/pkg/firefox-spot /usr/bin &&
	
	# desktop file
	install -m644 /tmp/pkg/firefox.desktop /usr/share/applications
}


