#!/usr/bin/make -f

# Disable tests which are not working
# for cross builds at the moment
# Although we override dh_auto_test we
# set this ENV to silence some warnings
export DEB_BUILD_OPTIONS=nocheck

#export DH_VERBOSE=1
export LDFLAGS=

# Extract kernel releases dynamically from debian/control.
# Every "Package: realtek-r8126-driver-<release>" stanza defines a release.
# Adding a new binary package (+ matching Build-Depends) is all that is
# needed to build against an additional kernel.
KERNEL_RELEASES := $(shell grep -oP '^Package:\s+realtek-r8126-driver-\K.*' debian/control)

UPSTREAM_VERSION := $(shell dpkg-parsechangelog --show-field Version | cut -d"-" -f 1)

# Construct some paths used in the build process.
BUILD_DIR := $(PWD)/src
DEBIAN_DIR := $(PWD)/debian
OUTPUT_DIR := $(DEBIAN_DIR)/output

# For each kernel release:
# compile the module and stage the resulting .ko in a per-release subdirectory
# and clean the build dir before the next iteration so object files don't leak across kernels.
#
# Note about the file staging workaround:
# With Kernel versions > 6.12 we can use MO= to build external modules in a separate directory.
# This will in future avoid the need of staging and cleaning the tree.
override_dh_auto_build:
	for release in $(KERNEL_RELEASES); do \
		kerneldir=$$(find /usr/src/ -type d -name "linux-balluff-$${release}*" | head -1); \
		cp $(DEBIAN_DIR)/Kbuild $(BUILD_DIR); \
		mkdir -p $(OUTPUT_DIR)/$$release; \
		ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- $(MAKE) -C $$kerneldir M=$(BUILD_DIR) -j $(shell nproc) modules; \
		cp $(BUILD_DIR)/r8126.ko $(OUTPUT_DIR)/$$release/r8126.ko; \
		ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- $(MAKE) -C $$kerneldir M=$(BUILD_DIR) clean; \
	done

# Install the per-release kernel module into the matching binary package
# using dh_install -p to target the correct package.
override_dh_auto_install:
	for release in $(KERNEL_RELEASES); do \
		kerneldir=$$(find /usr/src/ -type d -name "linux-balluff-$${release}*" | head -1); \
		kernelversion=$$(ARCH=arm64 $(MAKE) --no-print-directory -C $$kerneldir kernelrelease); \
		dh_install -p realtek-r8126-driver-$$release --sourcedir=$(OUTPUT_DIR)/$$release r8126.ko /lib/modules/$$kernelversion/extra; \
		dh_install -p realtek-r8126-driver-$$release --sourcedir=$(DEBIAN_DIR) r8126.conf /usr/lib/modprobe.d/; \
	done

override_dh_auto_test:

# Generate per-package maintainer scripts from the shared templates.
# Otherwise dh_installmodules will create a postinst/postrm step
# with a wrong Kernelrelease version string.
execute_before_dh_installdeb:
	for release in $(KERNEL_RELEASES); do \
		cp $(DEBIAN_DIR)/postinst $(DEBIAN_DIR)/realtek-r8126-driver-$$release.postinst; \
		cp $(DEBIAN_DIR)/postrm $(DEBIAN_DIR)/realtek-r8126-driver-$$release.postrm; \
	done

# Clean up generated per-package scripts and staged build artifacts.
override_dh_auto_clean:
	for release in $(KERNEL_RELEASES); do \
		rm -f $(DEBIAN_DIR)/realtek-r8126-driver-$$release.postinst; \
		rm -f $(DEBIAN_DIR)/realtek-r8126-driver-$$release.postrm; \
		rm -rf $(OUTPUT_DIR)/$$release; \
	done
	dh_clean

# Override the default dh_builddeb target to use xz compression explicitly
override_dh_builddeb:
	dh_builddeb -- -Zxz

%:
	dh $@
