#!/usr/bin/make -f

# Leave commented unless debugging build issues
#export DH_VERBOSE = 1
#export DH_OPTIONS = -v

export SHELL=/bin/bash

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed -Wl,--reduce-memory-overheads -Wl,--no-keep-memory

FFVERSION = 102.14.0
CLVERSION = RELEASE_8_1_0

%:
	mkdir -p output/build output/icecat-$(FFVERSION)
	dh $@ --builddirectory=output/build --sourcedirectory=output/icecat-$(FFVERSION)

override_dh_auto_configure:
	# Accomodate for building outside of OBS.
ifeq ("$(wildcard $(../SOURCES/firefox-$(FFVERSION)esr.source.tar.xz))","")
	cp ../SOURCES/firefox-$(FFVERSION)esr.source.tar.xz output/
endif
ifeq ("$(wildcard $(../SOURCES/firefox-$(FFVERSION)esr.source.tar.xz.asc))","")
	cp ../SOURCES/firefox-$(FFVERSION)esr.source.tar.xz.asc output/
endif
ifeq ("$(wildcard $(../SOURCES/KEY))","")
	cp ../SOURCES/KEY output/
endif
ifeq ("$(wildcard $(../SOURCES/*.zip))","")
	cp ../SOURCES/*.zip output/
endif
ifeq ("$(wildcard $(../SOURCES/$(CLVERSION).zip))","")
	cp ../SOURCES/$(CLVERSION).zip output/compare-locales-$(CLVERSION).zip
endif
	# https://aur.archlinux.org/cgit/aur.git/commit/?h=icecat&id=19c0c6c810d1db9179fde5d9dbaed97ccc1739da
	sed -e 's;find l10n -wholename '\''\*/brand.dtd'\'' | xargs;find l10n -wholename '\''\*/brand.dtd'\'' | xargs -r;g' -i makeicecat
	./makeicecat
	cp data/buildscripts/mozconfig-common output/icecat-$(FFVERSION)/.mozconfig
	cat data/buildscripts/mozconfig-gnulinux >> output/icecat-$(FFVERSION)/.mozconfig
	echo "ac_add_options --prefix=/usr" >> output/icecat-$(FFVERSION)/.mozconfig
ifneq (,$(filter $(DEB_HOST_ARCH),ppc64el arm64))
	# TODO: Look into why this doesn't exist for ppc64el/arm64
	sed -i '/--disable-eme/d' output/icecat-$(FFVERSION)/.mozconfig
endif
ifeq (ppc64el,$(DEB_BUILD_ARCH))
	# Work-around-bz-1775202-to-fix-FTBFS-on-ppc64el.patch
	# Add-missing-webrtc-directory-for-ppc64el-bz-1775202.patch
	sed -i -e '523c\
\t"/third_party/libwebrtc/modules/audio_coding/isac_gn",\
\t"/third_party/libwebrtc/modules/desktop_capture/desktop_capture_generic_gn",\
\t"/third_party/libwebrtc/modules/desktop_capture/desktop_capture_gn",\
\t"/third_party/libwebrtc/modules/desktop_capture/primitives_gn"' \
	output/icecat-$(FFVERSION)/third_party/libwebrtc/moz.build
endif
	sed -i 's/cargo_min_version = rustc_min_version/cargo_min_version = Version("1.46.0")/' \
		output/icecat-$(FFVERSION)/build/moz.configure/rust.configure

override_dh_auto_build:
	cd output/icecat-$(FFVERSION) && MACH_USE_SYSTEM_PYTHON=1 ./mach build
	cd output/build/browser/locales && \
	for locale in $$(ls ../../../l10n/ -1); do \
		make merge-$$locale LOCALE_MERGEDIR=. ; \
		make langpack-$$locale LOCALE_MERGEDIR=. ; \
	done
	cd output/build/dist/linux-*/xpi && \
	for langpack in $$(ls *.xpi); do \
		language=$$(echo $$langpack | awk -F '.' '{print $$(NF-2)}') ; \
		mv $$langpack langpack-$$language@icecat.mozilla.org.xpi ; \
	done

override_dh_auto_test:
