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

# source: firefox-gtk2 recipe modified for thunderbird, mixed with BLFS 8.2 2018-07-09
TARBALL=thunderbird-$PKGVER.source.tar.xz
MD5SUM=dfef11976fd4284e438a42da55129ecd
SRC_URL=https://archive.mozilla.org/pub/thunderbird/releases/$PKGVER/source/$TARBALL
BUNDLE=

SLACKREQ='alsa-lib gtk2 libicu' # build-time: autoconf213 
SLACKDESC="thunderbird-gtk2: thunderbird-gtk2 $PKGVER (Email client)
thunderbird-gtk2: 
thunderbird-gtk2: Thunderbird is a stand-alone mail/news client based on the Mozilla 
thunderbird-gtk2: codebase. It uses the Gecko rendering engine to enable it to display 
thunderbird-gtk2: and compose HTML emails.
thunderbird-gtk2: 
thunderbird-gtk2: 
thunderbird-gtk2: 
thunderbird-gtk2: 
thunderbird-gtk2: 
thunderbird-gtk2: 
"

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/thunderbird-*/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 &&
	cd .. &&
	
	if [ $USE_PREBUILT ]; then
		[ -e $BACKUP_DIR/tb-build-dir ] &&
		cp -a $BACKUP_DIR/tb-build-dir .
	else
		return 0
	fi
}

### package
pkg_package() {
	#cp -a tmp/thunderbird-*/tb-build-dir $BACKUP_DIR # backup the build output for USE_PREBUILT

	# I don't know if the profile works, this is inherited from Firefox, unchanged ...
	rm -rf usr/$LIBDIR/thunderbird-$PKGVER/defaults/profile &&
	cp -a tmp/pkg/profile usr/$LIBDIR/thunderbird-$PKGVER/defaults
}

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

#################### MOZ-BUILD START ###################	
	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, the build may be faster if using parallel
# jobs. The build system automatically adds -jN to the "make" flags, where N
# is the number of CPU cores. The option below is therefore useless, unless
# you want to use a smaller number of jobs:
mk_add_options MOZ_MAKE_FLAGS="$MAKEFLAGS" 

# 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

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

# Comment out following option if you have PulseAudio installed
ac_add_options --disable-pulseaudio # required from FF 50
ac_add_options --enable-alsa        # required from FF 52

# Comment out following option if you have gconf installed
ac_add_options --disable-gconf      # tested with FF 50 onwards

# If you want to compile the Mozilla Calendar, uncomment this line:
ac_add_options --enable-calendar

# 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 # changes often - use internal instead
#ac_add_options --with-system-nss  # changes often - use internal instead

# The BLFS editors recommend not changing anything below this line:
ac_add_options --prefix=/usr
ac_add_options --enable-application=mail

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

ac_add_options --enable-optimize=-O2 # FF51 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

# Using system cairo has been reported to make thunderbird execution unstable.
# This appears to again work in thunderbird-52
#ac_add_options --enable-system-cairo # requires cairo-tee which we don't build
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
mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/tb-build-dir
ac_add_options --libdir=/usr/$LIBDIR
ac_add_options --disable-debug-symbols
ac_add_options --enable-default-toolkit=cairo-gtk2 # required from FF 50
ac_add_options --disable-verify-mar # tested with FF 52
#ac_add_options --disable-elf-hack
#ac_add_options --with-arm-kuser
#export LDFLAGS="-Wl,--no-keep-memory -Wl,--reduce-memory-overheads" # failed on ld.gold
export LDFLAGS="-Wl,--no-keep-memory"
EOF

	export ICU_DATA=/usr/share/icu/current/ && # so ICU knows where its data is
	if ! [ "$USE_PREBUILT" ]; then
		make -f client.mk &&		
		make -C tb-build-dir/mail/installer || return 1            # old-style install
	fi  &&

	rm -rf /usr/$LIBDIR/thunderbird /usr/$LIBDIR/thunderbird-$PKGVER &&	
	cp -a tb-build-dir/dist/thunderbird /usr/$LIBDIR/thunderbird-$PKGVER &&   # old-style install
	#make -f client.mk install INSTALL_SDK= &&                                # new-style install
	#chown -R 0:0 /usr/lib/thunderbird-$PKGVER                                # new-style install

#################### MOZ-BUILD END ###################

	# post-install
	ln -sf thunderbird-$PKGVER /usr/$LIBDIR/thunderbird &&
	ln -sf /usr/$LIBDIR/thunderbird/thunderbird /usr/bin/thunderbird &&
	ln -sfv /usr/$LIBDIR/thunderbird-$PKGVER/chrome/icons/default/default256.png \
        /usr/share/pixmaps/thunderbird.png &&
    
	# plugins dir
	mkdir -pv /usr/$LIBDIR/mozilla/plugins                       &&
	ln -sfv ../mozilla/plugins /usr/$LIBDIR/thunderbird-$PKGVER  &&
	ln -sfv ../plugins /usr/$LIBDIR/thunderbird-$PKGVER/browser  &&

	# install additional components - gdata-provider
	echo "installing gdata provider" &&
	mkdir -p /usr/$LIBDIR/thunderbird/extensions/"{a62ef8ec-5fdc-40c2-873c-223b8a6925cc}" &&
	unzip tb-build-dir/dist/xpi-stage/gdata*xpi -d \
	/usr/$LIBDIR/thunderbird/extensions/"{a62ef8ec-5fdc-40c2-873c-223b8a6925cc}" > /dev/null &&
	
	# "fast" thunderbird by uncompressing omni.ja - thanks to BarryK
	# enable this only when thunderbird is built-in into basesfs
<< "FAST"
	echo "unpacking omni.ja" &&
	cd /usr/$LIBDIR/thunderbird &&
	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 thunderbird as spot
	ln -s /usr/bin/program-spot /usr/bin/thunderbird-spot &&
	
	# desktop file
	install -m644 /tmp/pkg/thunderbird.desktop /usr/share/applications
}


