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

# source: https://developer.palemoon.org/build/linux/
TARBALL=palemoon-$PKGVER-source.tar.xz
MD5SUM=a6ae69031d07f57031752eb2ed8ba820
SRC_URL=http://distro.ibiblio.org/fatdog/source/900/$TARBALL
BUNDLE=

SLACKREQ='alsa-lib gtk3 ffmpeg' # autoconf213 build-time dependency
SLACKDESC="palemoon: palemoon $PKGVER (Web browser)
palemoon: 
palemoon: Pale Moon is an Open Source web browser focusing on efficiency and 
palemoon: customization. Pale Moon offers a browsing experience completely 
palemoon: built from its own, independently developed source that was forked 
palemoon: off from Firefox a number of years ago, with carefully selected 
palemoon: features and optimizations.
palemoon: This is the gtk3 version.
palemoon: 
palemoon: https://palemoon.org
palemoon: 
"
SLACKCON="palemoon-gtk2"

### default pkg_download
### default pkg_prepare

### build
pkg_build() {
	cd /tmp/palemoon* || return 1

	cat > mozconfig << "EOF" &&
# Clear this if not a 64bit build
_BUILD_64=1

# Set GTK Version to 2 or 3
_GTK_VERSION=3

# Standard build options for Pale Moon
ac_add_options --enable-application=palemoon
ac_add_options --enable-optimize="-O2 -w"
ac_add_options --enable-default-toolkit=cairo-gtk$_GTK_VERSION
ac_add_options --enable-jemalloc
ac_add_options --enable-strip
ac_add_options --enable-devtools
ac_add_options --enable-av1
ac_add_options --enable-jxl
ac_add_options --disable-webrtc
ac_add_options --disable-gamepad
ac_add_options --disable-tests
ac_add_options --disable-debug
ac_add_options --disable-necko-wifi
ac_add_options --disable-updater
ac_add_options --with-pthreads

# Please see https://www.palemoon.org/redist.shtml for restrictions when using the official branding.
#ac_add_options --enable-official-branding
#export MOZILLA_OFFICIAL=1

# Processor architecture specific build options
if [ -n "$_BUILD_64" ]; then
  ac_add_options --x-libraries=/usr/lib64
else
  ac_add_options --x-libraries=/usr/lib
fi

export MOZ_PKG_SPECIAL=gtk$_GTK_VERSION

### Fatdog settings
mk_add_options MOZ_MAKE_FLAGS="-j4"
ac_add_options --libdir=/usr/lib64
ac_add_options --disable-dbus
ac_add_options --disable-gconf
ac_add_options --disable-pulseaudio
ac_add_options --enable-alsa

# disable sync services: https://github.com/MoonchildProductions/UXP/commit/e5e21699ca95369651670a302fde26dc6c19b4bd
ac_add_options --disable-sync

# Uncomment these if you have installed them:
ac_add_options --enable-startup-notification
# ac_add_options --enable-system-hunspell # no longer builds with system hunspell?
# ac_add_options --enable-system-sqlite # changes often - use internal instead
# ac_add_options --with-system-libevent
# ac_add_options --with-system-libvpx   # no longer applicable for 28.9.3 onwards
# ac_add_options --with-system-nspr     # changes often - use internal instead
# ac_add_options --with-system-nss      # changes often - use internal instead

ac_add_options --enable-system-ffi
ac_add_options --with-system-jpeg
# ac_add_options --with-system-png    # no longer applicable for 28.9.3 onwards
ac_add_options --with-system-zlib
ac_add_options --with-system-bz2
# ac_add_options --with-system-icu    # no longer applicable for 28.9.3 onwards

#ac_add_options --disable-elf-hack
#ac_add_options --with-arm-kuser

#export LDFLAGS="-Wl,--no-keep-memory"
EOF

	# prepare
	rm -rf /usr/$LIBDIR/palemoon /usr/$LIBDIR/palemoon-$PKGVER   # cleans state
	#export ICU_DATA=/usr/share/icu/current/                     # so ICU knows where its data is
	! [ -e /usr/bin/env ] && ln -s /tools/bin/env /usr/bin       # /usr/bin/env need to exist
	# echo nameserver 8.8.8.8 > /etc/resolv.conf # not needed?

	# BLFS 11.3 notes
	export SHELL=/bin/sh && PYTHON=python2.7 &&
	#mount -t tmpfs devshm /dev/shm &&

	./mach configure &&
	./mach build     &&
	./mach install   &&

	# cleanup
	#umount /dev/shm &&
	rm -f /usr/bin/env &&
	rm -f /etc/resolv.conf &&

	pkg_build_slackdesc
}


### package
pkg_package() {
	# we run without && to make sure that even if some of the operations fail
	# we still get a package (since palemoon took a long time to build).

	# clean up
	rm -rf ./usr/$LIBDIR/python* ./root
	rm -f  ./usr/bin/palemoon
	rmdir ./etc

	# relocate
	#mkdir -p ./usr/$LIBDIR # only needed if it lives in ./usr/lib
	#mv ./usr/lib/palemoon ./usr/$LIBDIR/palemoon
	mv ./usr/$LIBDIR/palemoon ./usr/$LIBDIR/palemoon-$PKGVER
	ln -s palemoon-$PKGVER ./usr/$LIBDIR/palemoon
	ln -s /usr/$LIBDIR/palemoon/palemoon ./usr/bin

	# remove other garbage
	rm -rf ./usr/local ./usr/$LIBDIR/palemoon-devel*

	# plugins dir
	mkdir -pv ./usr/$LIBDIR/mozilla/plugins                   
	ln -sfv ../mozilla/plugins ./usr/$LIBDIR/palemoon-$PKGVER

	# run palemoon as spot
	#install -m755 ./tmp/pkg/palemoon-spot /usr/bin
	ln -s program-spot ./usr/bin/palemoon-spot

	# desktop file
	mkdir -p ./usr/share/applications
	install -m644 ./tmp/pkg/palemoon.desktop ./usr/share/applications
	mkdir -p ./usr/share/icons/hicolor/128x128/apps
	cp ./usr/$LIBDIR/palemoon-$PKGVER/browser/icons/mozicon128.png \
        ./usr/share/icons/hicolor/128x128/apps/palemoon.png 

	# "fast" palemoon by uncompressing omni.ja
<< "EOF" # only do this when installed in base
	(
	cd ./usr/$LIBDIR/palemoon
	mkdir xxx && cd xxx
	{ unzip ../omni.ja; rm ../omni.ja; zip -r0 ../omni.ja *; } > /dev/null
	cd .. && rm -rf xxx
	)
EOF

}
