#!/usr/bin/make -f

export MOZ_NOSPAM=1

%:
	dh $@

override_dh_auto_configure:
	# 1. Find and extract the patches tarball
	@PATCHES_TAR=$$(ls -1 /usr/src/packages/SOURCES/thunderbird-patches-static-*.tar.xz 2>/dev/null | head -1); \
	if [ -z "$$PATCHES_TAR" ]; then \
		PATCHES_TAR=$$(ls -1 "$(CURDIR)/../thunderbird-patches-static-*.tar.xz" 2>/dev/null | head -1); \
	fi; \
	if [ -z "$$PATCHES_TAR" ]; then echo "ERROR: Patches tarball not found"; exit 1; fi; \
	echo "FOUND patches: $$PATCHES_TAR"; \
	export PATCHDIR=$$(mktemp -d); \
	tar xf "$$PATCHES_TAR" -C "$$PATCHDIR" 2>/dev/null; \
	export PATCHDIR=$$(find "$$PATCHDIR" -maxdepth 2 -type d -name "thunderbird-patches*" | head -1); \
	if [ -z "$$PATCHDIR" ]; then echo "ERROR: Patches not found in tarball"; exit 1; fi; \
	echo "PATCHDIR=$$PATCHDIR"; \
	\
	# 2. Apply Mozilla-Central patches
	echo "Applying Mozilla-Central patches..."; \
	cat "$$PATCHDIR/140/series-M-C" | while read p; do \
		[ -z "$$p" ] && continue; \
		case "$$p" in \#*) continue;; esac; \
		echo "Applying: $$p"; \
		patch -p1 < "$$PATCHDIR/140/$$p"; \
	done; \
	\
	# 3. Apply Betterbird Comm patches
	echo "Applying Betterbird Comm patches..."; \
	cd comm && cat "$$PATCHDIR/140/series" | while read p; do \
		[ -z "$$p" ] && continue; \
		case "$$p" in \#*) continue;; esac; \
		echo "Applying: $$p"; \
		patch -p1 < "$$PATCHDIR/140/$$p"; \
	done; \
	cd ..; \
	\
	# 3.5. Fix vendored .cargo-checksum.json files (Bug 1998307)
	echo 'import json, os                                               ' > /tmp/fix_checksum.py; \
	echo 'for root, dirs, files in os.walk(os.getcwd()):               ' >> /tmp/fix_checksum.py; \
	echo '    if ".cargo-checksum.json" in files:                      ' >> /tmp/fix_checksum.py; \
	echo '        path = os.path.join(root, ".cargo-checksum.json")    ' >> /tmp/fix_checksum.py; \
	echo '        with open(path) as f:                                ' >> /tmp/fix_checksum.py; \
	echo '            data = json.load(f)                              ' >> /tmp/fix_checksum.py; \
	echo '        removal_keys = [k for k in data.get("files", {})     ' >> /tmp/fix_checksum.py; \
	echo '            if k.endswith(".orig") or any(p.startswith(".") for p in k.split("/"))]     ' >> /tmp/fix_checksum.py; \
	echo '        if removal_keys:                                     ' >> /tmp/fix_checksum.py; \
	echo '            for k in removal_keys:                           ' >> /tmp/fix_checksum.py; \
	echo '                del data["files"][k]                         ' >> /tmp/fix_checksum.py; \
	echo '            with open(path, "w") as f:                       ' >> /tmp/fix_checksum.py; \
	echo '                json.dump(data, f, indent=2)                 ' >> /tmp/fix_checksum.py; \
	python3 /tmp/fix_checksum.py; \
	\
	# 4. Handle mozconfig
	echo "Installing mozconfig..."; \
	if [ -f ../debian.mozconfig ]; then cp ../debian.mozconfig .mozconfig; \
	elif [ -f debian/mozconfig ]; then cp debian/mozconfig .mozconfig; \
	elif [ -f ../mozconfig ]; then cp ../mozconfig .mozconfig; \
	elif [ -f mozconfig ]; then cp mozconfig .mozconfig; \
	else echo "ERROR: mozconfig not found"; exit 1; fi; \
	echo "Build configuration ready."

override_dh_auto_build:
	# Install pip packages from local sources (offline build — OBS workers have no network)
	pip install --no-index --find-links=/usr/src/packages/SOURCES/ pyyaml glean-sdk 2>/dev/null || true
	# MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=none: skip system python compat check
	# (Debian 13 ships Python 3.13, too new for mach's system check)
	MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=none ./mach build

override_dh_auto_install:
	# 1. Let mach install under debian/betterbird
	DESTDIR=$(CURDIR)/debian/betterbird ./mach install

	# 2. Create target dirs
	mkdir -p $(CURDIR)/debian/betterbird/usr/lib
	mkdir -p $(CURDIR)/debian/betterbird/usr/bin
	mkdir -p $(CURDIR)/debian/betterbird/usr/share/applications

	# 3. Detect and Move
	APPROOT="$(CURDIR)/debian/betterbird/usr/local/lib"; \
	if [ -d "$$APPROOT" ]; then \
	  cand=$$(find "$$APPROOT" -mindepth 1 -maxdepth 1 -type d | head -n1); \
	  if [ -n "$$cand" ]; then \
	    mv "$$cand" "$(CURDIR)/debian/betterbird/usr/lib/betterbird"; \
	  else \
	    exit 1; \
	  fi; \
	fi

	# 4. Rename binary (thunderbird -> betterbird)
	if [ -f "$(CURDIR)/debian/betterbird/usr/lib/betterbird/thunderbird" ]; then \
	    mv "$(CURDIR)/debian/betterbird/usr/lib/betterbird/thunderbird" "$(CURDIR)/debian/betterbird/usr/lib/betterbird/betterbird"; \
	fi

	# 5. Symlink
	ln -sf ../lib/betterbird/betterbird $(CURDIR)/debian/betterbird/usr/bin/betterbird

	# 6. Install desktop file (if present)
	if [ -f ../betterbird.desktop ]; then \
	    cp ../betterbird.desktop $(CURDIR)/debian/betterbird/usr/share/applications/; \
	fi

	# 7. Cleanup
	rm -rf $(CURDIR)/debian/betterbird/usr/local
