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

# source: BLFS 7.5 merged with BLFS 8.2 and Fatdog's own options
TARBALL=seamonkey-$PKGVER.source.tar.xz
MD5SUM=5868179112ff4b2562f0ca22f4091bf0
SRC_URL=https://archive.mozilla.org/pub/mozilla.org/seamonkey/releases/$PKGVER/source/$TARBALL
BUNDLE=

SLACKREQ='alsa-lib gtk2 ffmpeg libicu'
SLACKDESC="seamonkey: seamonkey $PKGVER (Web browser suite)  
seamonkey:  
seamonkey: SeaMonkey is a browser suite, the Open Source sibling of Netscape. 
seamonkey: It includes the browser, composer, mail and news clients, and an 
seamonkey: IRC client. It is the follow-on to the Mozilla browser suite.  
seamonkey: 
seamonkey: 
seamonkey: 
seamonkey: 
seamonkey: 
seamonkey:
"

USE_PREBUILT= # yes
BACKUP_DIR=/mnt/sda5/play

### default pkg_download
### default pkg_prepare
pkg_prepare() {
	pkg_prepare_default &&
	
	###Reverse patch that did away with default profile copying. Bug 1234012.
	cd tmp/seamonkey-*/mozilla/ &&
	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 &&

	# ffmpeg 4.0 patch
	patch -Np1 -i ../../pkg/patch-bug1435212.diff &&
	
	cd .. &&
	if [ $USE_PREBUILT ]; then
		[ -e $BACKUP_DIR/moz-build-dir ] &&
		cp -a $BACKUP_DIR/moz-build-dir .
	else
		return 0
	fi
}

### package
pkg_package() {
	#cp -a tmp/*/moz-build-dir $BACKUP_DIR # backup the build output for USE_PREBUILT
	
	### add fatdog standard profile
	rm -rf usr/$LIBDIR/seamonkey-$PKGVER/defaults/profile &&
	cp -a tmp/pkg/profile usr/$LIBDIR/seamonkey-$PKGVER/defaults &&
	
	### add 'close tab' extension
	mkdir -p usr/$LIBDIR/seamonkey-$PKGVER/extensions/"seatabx2@extensions.moz" &&
	tar --strip-components=1 -xf tmp/pkg/seatabx2.tar.xz \
	    -C usr/$LIBDIR/seamonkey-$PKGVER/extensions/"seatabx2@extensions.moz" &&

	### add 'user agent switcher' extension
	mkdir -p usr/$LIBDIR/seamonkey-$PKGVER/extensions/"{e968fc70-8f95-4ab9-9e79-304de2a71ee1}" &&
	unzip tmp/pkg/user-agent-switcher-0.7.3-modified.xpi \
		-d usr/$LIBDIR/seamonkey-$PKGVER/extensions/"{e968fc70-8f95-4ab9-9e79-304de2a71ee1}"	
}

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

#################### MOZ-BUILD START ###################
if ! [ "$USE_PREBUILT" ]; then

	mkdir moz-build-dir
	ln -s ../moz-build-dir ./mozilla/
	export SHELL=/bin/sh PYTHON=python2.7-$USE_ARCH
	cat > mozconfig << EOF
# If you have a multicore machine, all cores will be used by default.
# If desired, you can reduce the number of cores used, e.g. to 1, by
# uncommenting the next line and setting a valid number of CPU cores.
mk_add_options MOZ_MAKE_FLAGS="$MAKEFLAGS" #-j6 or whatever

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

# If you have installed dbus-glib, and you have installed (or will install)
# wireless-tools, and you wish to use geolocation web services, comment out
# this line
ac_add_options --disable-necko-wifi

# If you have installed libnotify delete this option:
#ac_add_options --disable-libnotify

# Uncomment these lines if you have installed optional dependencies:
ac_add_options --enable-startup-notification
ac_add_options --enable-system-hunspell

# Uncomment the following option if you have not installed PulseAudio
ac_add_options --disable-pulseaudio
# and uncomment this if you installed alsa-lib instead of PulseAudio
ac_add_options --enable-alsa

# Comment out following option if you have gconf installed
ac_add_options --disable-gconf

# Comment out following options if you have not installed
# recommended dependencies:
ac_add_options --enable-system-sqlite
# ac_add_options --with-system-libevent
ac_add_options --with-system-libvpx
#ac_add_options --with-system-nspr # don't use system - these are updated often
#ac_add_options --with-system-nss  # don't use system - these are updated often

# The BLFS editors recommend not changing anything below this line:
mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/moz-build-dir
ac_add_options --prefix=/usr
ac_add_options --enable-application=suite

ac_add_options --disable-crashreporter
ac_add_options --disable-tests
ac_add_options --disable-updater

ac_add_options --enable-optimize=-Os # SM 2.48 above requires -O2, doesn't work with -Os anymore
ac_add_options --enable-strip
ac_add_options --enable-install-strip

#ac_add_options --enable-gio
#ac_add_options --enable-official-branding
#ac_add_options --enable-safe-browsing
#ac_add_options --enable-url-classifier

# From firefox-40 (and the corresponding version of seamonkey),
# using system cairo caused seamonkey to crash
# frequently when it was doing background rendering in a tab.
# This appears to again work in seamonkey-2.49.2
#ac_add_options --enable-system-cairo # Note: does not compile. Requires cairo-tee.
ac_add_options --enable-system-ffi
ac_add_options --enable-system-pixman

ac_add_options --with-pthreads

ac_add_options --with-system-bz2
ac_add_options --with-system-jpeg
ac_add_options --with-system-png
ac_add_options --with-system-zlib
ac_add_options --with-system-icu

### Fatdog settings
ac_add_options --libdir=/usr/$LIBDIR
#ac_add_options --enable-calendar # BUG: does not build in SM 2.49 onwards
ac_add_options --enable-default-toolkit=cairo-gtk2
#export LDFLAGS="-Wl,--no-keep-memory -Wl,--reduce-memory-overheads" # doesn't work with ld.gold
export LDFLAGS="-Wl,--no-keep-memory"
EOF

	CFLAGS_HOLD=$CFLAGS           &&
	CXXFLAGS_HOLD=$CXXFLAGS       &&
	EXTRA_FLAGS=" -fno-delete-null-pointer-checks -fno-lifetime-dse -fno-schedule-insns2" &&
	export CFLAGS+=$EXTRA_FLAGS   &&
	export CXXFLAGS+=$EXTRA_FLAGS &&
	unset EXTRA_FLAGS             &&
	export ICU_DATA=/usr/share/icu/current/ && # so ICU knows where its data is

	make -f client.mk &&
	
	# old style-install
	make -C moz-build-dir/suite/installer

	# new-style install
	#make  -f client.mk install INSTALL_SDK= &&
	#chown -R 0:0 /usr/lib/seamonkey-$PKGVER &&
	#cp -v $(find -name seamonkey.1 | head -n1) /usr/share/man/man1

fi &&
#################### MOZ-BUILD END ###################

	# install
	rm -rf /usr/$LIBDIR/seamonkey /usr/$LIBDIR/seamonkey-$PKGVER &&
	cp -a moz-build-dir/dist/seamonkey /usr/$LIBDIR/seamonkey-$PKGVER &&
	ln -sf seamonkey-$PKGVER /usr/$LIBDIR/seamonkey &&
	ln -sf /usr/$LIBDIR/seamonkey/seamonkey /usr/bin/seamonkey &&
	
	# plugins dir
	mkdir -pv /usr/$LIBDIR/mozilla/plugins                   &&
	ln -sfv ../mozilla/plugins /usr/$LIBDIR/seamonkey-$PKGVER &&
	
	# manpage
	cp -v moz-build-dir/dist/man/man1/seamonkey.1 /usr/share/man/man1 &&

	# clear extensions/* and distribution/extensions/* (modern themes and whatnot)
	rm -rf /usr/$LIBDIR/seamonkey-$PKGVER/extensions/*  &&
	rm -rf /usr/$LIBDIR/seamonkey-$PKGVER/distribution/extensions/* && 

<< "DONT EXIST IN SM 2.49" &&
	# install our components - calendar, lightning, inspector, chatzilla to extensions
	echo "installing calendar" &&
	mkdir -p /usr/$LIBDIR/seamonkey/extensions/"{e2fda1a4-762b-4020-b5ad-a41df1933103}" &&
	unzip moz-build-dir/dist/xpi-stage/lightning*xpi -d  \
	/usr/$LIBDIR/seamonkey/extensions/"{e2fda1a4-762b-4020-b5ad-a41df1933103}" > /dev/null &&

	echo "installing calendar timezone" &&
	mkdir -p /usr/$LIBDIR/seamonkey/extensions/"calendar-timezones@mozilla.org" &&
	unzip moz-build-dir/dist/xpi-stage/calendar-timezones*xpi -d  \
	/usr/$LIBDIR/seamonkey/extensions/"calendar-timezones@mozilla.org" > /dev/null &&

	echo "installing gdata provider" &&
	mkdir -p /usr/$LIBDIR/seamonkey/extensions/"{a62ef8ec-5fdc-40c2-873c-223b8a6925cc}" &&
	unzip moz-build-dir/dist/xpi-stage/gdata*xpi -d \
	/usr/$LIBDIR/seamonkey/extensions/"{a62ef8ec-5fdc-40c2-873c-223b8a6925cc}" > /dev/null &&
DONT EXIST IN SM 2.49

<< "DONT EXIST IN SM 2.48" &&
	echo "installing DOM inspector" &&
	mkdir -p /usr/$LIBDIR/seamonkey/extensions/"inspector@mozilla.org" &&
	unzip moz-build-dir/dist/xpi-stage/inspector*xpi -d \
	/usr/$LIBDIR/seamonkey/extensions/"inspector@mozilla.org" > /dev/null &&

	echo "installing chatzilla" &&
	mkdir -p /usr/$LIBDIR/seamonkey/extensions/"{59c81df5-4b7a-477b-912d-4e0fdf64e5f2}" &&
	unzip moz-build-dir/dist/xpi-stage/chatzilla*xpi -d \
	/usr/$LIBDIR/seamonkey/extensions/"{59c81df5-4b7a-477b-912d-4e0fdf64e5f2}" > /dev/null &&
	
	# quitter exist, but doesn't seem to work anyway
	echo "installing Quitter Module" &&
	mkdir -p /usr/$LIBDIR/seamonkey/extensions/"quitter@mozilla.org" &&
	unzip moz-build-dir/dist/xpi-stage/quitter*xpi -d \
	/usr/$LIBDIR/seamonkey/extensions/"quitter@mozilla.org" > /dev/null &&	
DONT EXIST IN SM 2.48

	# Because we cannot get the calendar stuff to to work, we use the pre-packaged ones.
	echo "installing pre-built calendar" &&
	mkdir -p /usr/$LIBDIR/seamonkey/extensions/"{e2fda1a4-762b-4020-b5ad-a41df1933103}" &&
	unzip /tmp/pkg/lightning*xpi -d  \
	/usr/$LIBDIR/seamonkey/extensions/"{e2fda1a4-762b-4020-b5ad-a41df1933103}" > /dev/null &&

	echo "installing pre-built gdata provider" &&
	mkdir -p /usr/$LIBDIR/seamonkey/extensions/"{a62ef8ec-5fdc-40c2-873c-223b8a6925cc}" &&
	unzip /tmp/pkg/provider_for_google_calendar*xpi -d \
	/usr/$LIBDIR/seamonkey/extensions/"{a62ef8ec-5fdc-40c2-873c-223b8a6925cc}" > /dev/null &&

	echo "installing pre-built chatzilla" &&
	mkdir -p /usr/$LIBDIR/seamonkey/extensions/"{59c81df5-4b7a-477b-912d-4e0fdf64e5f2}" &&
	unzip /tmp/pkg/chatzilla*xpi -d \
	/usr/$LIBDIR/seamonkey/extensions/"{59c81df5-4b7a-477b-912d-4e0fdf64e5f2}" > /dev/null &&
	
	# "fast" seamonkey by uncompressing omni.ja - thanks to BarryK
	echo "unpacking omni.ja" &&
	cd /usr/$LIBDIR/seamonkey &&
	mkdir xxx && cd xxx && 
	{ unzip ../omni.ja; rm ../omni.ja; zip -r0 ../omni.ja *; } > /dev/null &&
	cd .. && rm -rf xxx &&
	
	pkg_build_slackdesc && 	
	rm -f /usr/bin/env &&
    
	# run seamonkey as spot
	#install -m755 /tmp/pkg/seamonkey-spot /usr/bin &&
	ln -s program-spot /usr/bin/seamonkey-spot &&

	# desktop files
	install -m644 /tmp/pkg/seamonkey.desktop /usr/share/applications &&	
	install -m644 /tmp/pkg/seamonkey-mail.desktop /usr/share/applications &&
	install -m644 /tmp/pkg/seamonkey-composer-html-editor.desktop /usr/share/applications &&
	install -m644 /tmp/pkg/seamonkey-addressbook.desktop /usr/share/applications

}
 

