abinfo "Installing nodejs dependencies ..."
# FIXME: Our nodejs is built with GCC, so `clang=0` in
# /usr/lib/node-22/include/node/config.gypi.node-gyp adds GCC-specific
# compiler flags for ppc64el as the result. Set CC/CXX to GCC for ppc64el only.
#
# Ref: github.com/nodejs/node/blob/v22.x/common.gypi#L546-L548
if ab_match_arch ppc64el; then
	CC=gcc CXX=g++ npm install
else
	npm install
fi

abinfo "Load Surfer CI setup ..."
npm run surfer -- ci --brand release --display-version "$PKGVER"

abinfo "Downloading Firefox source ..."
npm run download && npm run import

MOZCONFIG_PATH="${SRCDIR}/configs/linux/mozconfig"

# Append materialized abs path of API key files
cat << EOF >> "$MOZCONFIG_PATH"
# Google API keys.
ac_add_options --with-google-location-service-api-keyfile="$SRCDIR/autobuild/google-api-key"
ac_add_options --with-google-safebrowsing-api-keyfile="$SRCDIR/autobuild/google-api-key"
EOF

if ! ab_match_arch loongson3; then
	echo "ac_add_options --enable-linker=lld" \
		>> "${MOZCONFIG_PATH}"
	echo "ac_add_options --with-wasi-sysroot=/usr/lib/wasm32-wasi/" \
		>> "${MOZCONFIG_PATH}"
	echo "ac_add_options --enable-jemalloc" \
		>> "${MOZCONFIG_PATH}"
fi

# FIXME: SIGBUS during build.
if ab_match_arch loongarch64; then
	export CARGO_PROFILE_RELEASE_BUILD_OVERRIDE_DEBUG=true
	# FIXME: Huge libxul links can overflow LoongArch R_LARCH_B26 branch range
	# with the default code model; use medium code model to avoid
	# ld.lld: error: relocation R_LARCH_B26 out of range.
	abinfo "Applying medium code model workaround for LoongArch libxul links ..."
	export CFLAGS="${CFLAGS} -mcmodel=medium"
	export CXXFLAGS="${CXXFLAGS} -mcmodel=medium"
fi

# FIXME
# error: linking with `/var/cache/acbs/build/acbs.tdg5m00_/firefox-128.0.2/build/cargo-linker` failed: exit status: 1
#   |
#   = note: LC_ALL="C" PATH="/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/bin:/var/cache/acbs/build/acbs.tdg5m00_/firefox-128.0.2/mozbuild/srcdirs/firefox-128.0.2-cd118c606d13/_virtualenvs/build/bin:/var/cache/acbs/build/acbs.tdg5m00_/firefox-128.0.2/mozbuild/srcdirs/firefox-128.0.2-cd118c606d13/_virtualenvs/mach/bin:/usr/local/bin:/usr/bin:/root/.cargo/bin" VSLANG="1033" "/var/cache/acbs/build/acbs.tdg5m00_/firefox-128.0.2/build/cargo-linker" "/tmp/rustcNfYF5C/symbols.o" "/var/cache/acbs/build/acbs.tdg5m00_/firefox-128.0.2/obj-mips64el-unknown-linux-gnuabi64/release/build/proc-macro2-2e9eccf43e42a20c/build_script_build-2e9eccf43e42a20c.build_script_build.7fc36fe6be68b652-cgu.0.rcgu.o" "/var/cache/acbs/build/acbs.tdg5m00_/firefox-128.0.2/obj-mips64el-unknown-linux-gnuabi64/release/build/proc-macro2-2e9eccf43e42a20c/build_script_build-2e9eccf43e42a20c.build_script_build.7fc36fe6be68b652-cgu.1.rcgu.o" "/var/cache/acbs/build/acbs.tdg5m00_/firefox-128.0.2/obj-mips64el-unknown-linux-gnuabi64/release/build/proc-macro2-2e9eccf43e42a20c/build_script_build-2e9eccf43e42a20c.46klksw3d6aqgnt6.rcgu.o" "-Wl,--as-needed" "-L" "/var/cache/acbs/build/acbs.tdg5m00_/firefox-128.0.2/obj-mips64el-unknown-linux-gnuabi64/release/deps" "-L" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib" "-Wl,-Bstatic" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/libstd-6dcaeb4e3195ef4c.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/libpanic_unwind-3b492db5f861c144.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/libobject-a4d15655b8d59547.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/libmemchr-433eb4217c15c220.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/libaddr2line-3694f367f2cdb9fc.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/libgimli-73fb45a729ed0e30.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/librustc_demangle-41cacb4ff97fa4cf.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/libstd_detect-73d35a69185f39da.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/libhashbrown-f5654efde69fe63a.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/librustc_std_workspace_alloc-d553094a352c860a.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/libminiz_oxide-660efaccb5d68a60.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/libadler-b79bbf5dab20e247.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/libunwind-12f55669c7956981.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/libcfg_if-098c3160b34fea81.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/liblibc-9879917f39f4f518.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/liballoc-0695d1b670b70049.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/librustc_std_workspace_core-b92546fd4bf9da36.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/libcore-306fddc16bfa4696.rlib" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib/libcompiler_builtins-609434335fac640f.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/usr/lib64/rustlib/mips64el-unknown-linux-gnuabi64/lib" "-o" "/var/cache/acbs/build/acbs.tdg5m00_/firefox-128.0.2/obj-mips64el-unknown-linux-gnuabi64/release/build/proc-macro2-2e9eccf43e42a20c/build_script_build-2e9eccf43e42a20c" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-Wl,--strip-debug" "-nodefaultlibs"
#   = note: ld.lld: error: relocation R_MIPS_64 cannot be used against symbol 'DW.ref.rust_eh_personality'; recompile with -fPIC
#           >>> defined in /var/cache/acbs/build/acbs.tdg5m00_/firefox-128.0.2/obj-mips64el-unknown-linux-gnuabi64/release/build/proc-macro2-2e9eccf43e42a20c/build_script_build-2e9eccf43e42a20c.build_script_build.7fc36fe6be68b652-cgu.0.rcgu.o
#           >>> referenced by build_script_build.7fc36fe6be68b652-cgu.0
#           >>>               /var/cache/acbs/build/acbs.tdg5m00_/firefox-128.0.2/obj-mips64el-unknown-linux-gnuabi64/release/build/proc-macro2-2e9eccf43e42a20c/build_script_build-2e9eccf43e42a20c.build_script_build.7fc36fe6be68b652-cgu.0.rcgu.o:(.eh_frame+0x4103)
if ab_match_arch loongson3; then
	echo "ac_add_options --enable-linker=bfd" \
		>> "${MOZCONFIG_PATH}"
	echo "ac_add_options --without-wasm-sandboxed-libraries" \
		>> "${MOZCONFIG_PATH}"
	abinfo "FIXME: sigbus at atomic variables when jemalloc is enabled on loongson3 ..."
	echo "ac_add_options --disable-jemalloc" \
		>> "${MOZCONFIG_PATH}"
	abinfo "FIXME: Disabling broken WebRTC on loongson3 ..."
	echo "ac_add_options --disable-webrtc" \
		>> "${MOZCONFIG_PATH}"
fi

if ab_match_arch ppc64el; then
	abinfo "FIXME: Disabling broken WebRTC on ppc64el ..."
	echo "ac_add_options --disable-webrtc" \
		>> "${MOZCONFIG_PATH}"
fi

if ab_match_arch powerpc || ab_match_arch ppc64; then
	abinfo "Swapping out little-endian ICU data for PowerPC ..."
	"${SRCDIR}/engine/mach" python "${SRCDIR}/engine/intl/icu_sources_data.py" .
	rm -v config/external/icu/data/icudt*l.dat
fi

if ab_match_arch armv7hf; then
	abinfo "Applying flag fixes for ARMv7 ..."
	export CFLAGS="${CFLAGS/-march=armv7-a -mtune=cortex-a7 -mfloat-abi=hard -mfpu=neon/}"
	export CXXFLAGS="${CXXFLAGS/-march=armv7-a -mtune=cortex-a7 -mfloat-abi=hard -mfpu=neon/}"
fi

# FIXME: --disable-elf-hack is not available on these architectures.
if ! ab_match_arch loongarch64 && \
   ! ab_match_arch loongson3 && \
   ! ab_match_arch riscv64 && \
   ! ab_match_arch ppc64el; then
	abinfo "Setting --disable-elf-hack ..."
	echo "ac_add_options --disable-elf-hack" \
		>> "${MOZCONFIG_PATH}"
fi

# Enable WASM SIMD on amd64 and arm64 only
if ab_match_arch amd64 || ab_match_arch arm64; then
	abinfo "Setting --enable-wasm-simd"
	echo "ac_add_options --enable-wasm-simd" \
		>> "${MOZCONFIG_PATH}"
fi

# Enable Widevine on amd64
if ab_match_arch amd64; then
	abinfo "Setting --enable-eme=widevine"
	echo "ac_add_options --enable-eme=widevine" \
		>> "${MOZCONFIG_PATH}"
fi

# Enable --enable-replace-malloc on most architectures
if ! ab_match_arch loongson3; then
	abinfo "Setting --enable-replace-malloc"
	echo "ac_add_options --enable-replace-malloc" \
		>> "${MOZCONFIG_PATH}"
fi

abinfo 'Making sure $SHELL is set ...'
export SHELL=/bin/bash

abinfo "Setting MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=pip for installing Python dependencies ..."
export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=pip

# Prepare localization data
abinfo "Copying Zen-specific localization data ..."
python3 "${SRCDIR}/scripts/update_en_US_packs.py"

abinfo "Downloading localization data ..."
L10N_COMMIT="$(cat "${SRCDIR}/build/firefox-cache/l10n-last-commit-hash")"
git clone --depth=1 --revision "${L10N_COMMIT}" \
	"https://github.com/mozilla-l10n/firefox-l10n" \
	"${SRCDIR}/firefox-l10n-source"
mkdir -p "${SRCDIR}/firefox-l10n"
for LANG in $(cat "${SRCDIR}/locales/supported-languages"); do
	LANG_CODE="$(grep "^${LANG}:" "${SRCDIR}/locales/language-maps" | cut -d':' -f2)"
	if [ -z "${LANG_CODE}" ]; then
		LANG_CODE="$LANG"
	fi
	abinfo "Copying localization data for ${LANG_CODE}"
	cp -Rv "${SRCDIR}/firefox-l10n-source/${LANG_CODE}" \
		"${SRCDIR}/firefox-l10n/${LANG_CODE}"
	cp -Rv "${SRCDIR}/locales/${LANG}/browser" \
		"${SRCDIR}/firefox-l10n/${LANG_CODE}/"
	# Record language codes for building lang packs
	echo "${LANG_CODE}" >> "${SRCDIR}/lang-codes"
done
echo "ac_add_options --with-l10n-base=\"${SRCDIR}/firefox-l10n\"" \
	>> "${MOZCONFIG_PATH}"
