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

### BLFS 11.3 updated to 102.10.0
TARBALL=firefox-102.10.0esr.source.tar.xz
MD5SUM=1442929705ee299acd2a65b711946cee
SRC_URL=https://archive.mozilla.org/pub/firefox/releases/102.10.0esr/source/$TARBALL
BUNDLE=

SLACKREQ='gtk3 libicu libevent libwebp libnspr libnss alsa-lib
libstartup-notification liboauth libnotify'
# clang rustc autoconf213 cbindgen python unzip yasm zip build-time only
SLACKDESC="firefox: firefox $PKGVER (Web browser)  
firefox:  
firefox: Firefox is a stand-alone browser based on the Mozilla codebase.  
firefox: 
firefox: This browser is configured to use /home/spot,
firefox: if you're still using /root/spot you may need to adjust
firefox: as needed.
firefox: 
firefox: 
firefox: 
firefox:  
"


### default pkg_download
### default pkg_prepare

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

	cat > mozconfig << EOF &&
# If you have a multicore machine, all cores will be used by default.
mk_add_options MOZ_MAKE_FLAGS="-j4" # don't fry my machine

# If 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

# API Keys for geolocation APIs - necko-wifi (above) is required for MLS
# Uncomment the following line if you wish to use Mozilla Location Service
#ac_add_options --with-mozilla-api-keyfile=$PWD/mozilla-key

# Uncomment the following line if you wish to use Google's geolocation API
# (needed for use with saved maps with Google Maps)
#ac_add_options --with-google-location-service-api-keyfile=$PWD/google-key

# startup-notification is required since firefox-78

# Uncomment the following option if you have not installed PulseAudio and
# want to use alsa instead
ac_add_options --enable-audio-backends=alsa

# Comment out following options if you have not installed
# recommended dependencies:
ac_add_options --with-system-icu
ac_add_options --with-system-libevent
ac_add_options --with-system-libvpx
ac_add_options --with-system-nspr
ac_add_options --with-system-nss
ac_add_options --with-system-webp

# Unlike with thunderbird, although using the gold linker can
# save four megabytes in the installed file it does not make
# the build faster.
ac_add_options --enable-linker=gold

# libdavid (av1 decoder) requires nasm. Uncomment this if nasm
# has not been installed. Do not uncomment this if you have
# ffmpeg-5 installed.
#ac_add_options --disable-av1

# You cannot distribute the binary if you do this
#ac_add_options --enable-official-branding

# Stripping is now enabled by default.
# Uncomment these lines if you need to run a debugger:
#ac_add_options --disable-strip
#ac_add_options --disable-install-strip

# Disabling debug symbols makes the build much smaller and a little
# faster. Comment this if you need to run a debugger. Note: This is
# required for compilation on i686.
ac_add_options --disable-debug-symbols

# The elf-hack is reported to cause failed installs (after successful builds)
# on some machines. It is supposed to improve startup time and it shrinks
# libxul.so by a few MB - comment this if you know your machine is not affected.
#ac_add_options --disable-elf-hack

# The BLFS editors recommend not changing anything below this line:
ac_add_options --prefix=/usr
ac_add_options --enable-application=browser
ac_add_options --disable-crashreporter
ac_add_options --disable-updater
# enabling the tests will use a lot more space and significantly
# increase the build time, for no obvious benefit.
ac_add_options --disable-tests

# The default level of optimization again produces a working build with gcc.
ac_add_options --enable-optimize

ac_add_options --enable-system-ffi
ac_add_options --enable-system-pixman

ac_add_options --with-system-jpeg
ac_add_options --with-system-png
ac_add_options --with-system-zlib

# Using sandboxed wasm libraries has been moved to all builds instead
# of only mozilla automation builds. It requires extra llvm packages
# and was reported to seriously slow the build. Disable it.
ac_add_options --without-wasm-sandboxed-libraries

# The following option unsets Telemetry Reporting. With the Addons Fiasco,
# Mozilla was found to be collecting user's data, including saved passwords and
# web form data, without users consent. Mozilla was also found shipping updates
# to systems without the user's knowledge or permission.
# As a result of this, use the following command to permanently disable
# telemetry reporting in Firefox.
unset MOZ_TELEMETRY_REPORTING

mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/firefox-build-dir

### Fatdog settings
ac_add_options --disable-dbus
ac_add_options --libdir=/usr/$LIBDIR
#export LDFLAGS="-Wl,--no-keep-memory"
EOF

	# prepare
	rm -rf /usr/$LIBDIR/firefox /usr/$LIBDIR/firefox-$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?

	# load rust compiler
	. /etc/profile.d/rustc.sh &&                 

	# BLFS 11.3 fix to support cbindgen-0.24.2 changes
	sed -i '/ROOT_CLIP_CHAIN/d' gfx/webrender_bindings/webrender_ffi.h &&
	
	# BLFS 11.3 notes
	export SHELL=/bin/sh && # PYTHON=python2.7 &&
	mount -t tmpfs devshm /dev/shm &&

	# BLFS 11.3 build
	export CC=gcc CXX=g++ && # try building with gcc
	#unset CC CXX CPP CFLAGS CXXFLAGS CPPFLAGS          &&
	export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=none &&
	export MOZBUILD_STATE_PATH=${PWD}/mozbuild          &&
	./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 firefox took a long time to build).

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

	# relocate
	#mkdir -p ./usr/$LIBDIR # only needed if it lives in ./usr/lib
	#mv ./usr/lib/firefox ./usr/$LIBDIR/firefox
	mv ./usr/$LIBDIR/firefox ./usr/$LIBDIR/firefox-$PKGVER
	ln -s firefox-$PKGVER ./usr/$LIBDIR/firefox
	ln -s /usr/$LIBDIR/firefox/firefox ./usr/bin
	
	# run firefox as spot
	#install -m755 ./tmp/pkg/firefox-spot ./usr/bin
	ln -s program-spot ./usr/bin/firefox-spot

	# desktop files
	mkdir -p ./usr/share/applications
	install -m644 ./tmp/pkg/firefox.desktop ./usr/share/applications

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

	# set profile - doesn't work yet, we will settle later
	# add fatdog standard profile
	#rm -rf ./usr/$LIBDIR/firefox-$PKGVER/defaults/profile
	#cp -a ./tmp/pkg/profile ./usr/$LIBDIR/firefox-$PKGVER/defaults
}
