#!/usr/bin/make -f
SHELL := sh -e
SOURCE := $(shell dpkg-parsechangelog -SSource)
VERSION := $(shell dpkg-parsechangelog -SVersion)
VERSION_UPSTREAM := $(shell echo "$(VERSION)" | sed -e 's,-[^-]*$$,,')
VERSION_BINNMU := $(shell echo "$(VERSION)" | sed -rne 's,.*\+b([0-9]+)$$,\1,p')
VERSION_SOURCE := $(patsubst %+b$(VERSION_BINNMU),%,$(VERSION))

GENCONTROL = debian/bin/gencontrol.py

binary binary-arch binary-indep build build-arch build-indep clean: debian/control
	dh $@

override_dh_auto_test:
	:

CLEAN_PATTERNS := debian/build debian/lib/python/__pycache__ debian/lib/python/debian_linux/__pycache__ $$(find debian -maxdepth 1 -type d -name 'firmware-*') debian/*.substvars

override_dh_auto_clean:
	rm -rf $(CLEAN_PATTERNS)

override_dh_auto_install:
	./copy-firmware.sh -v debian/build/install
	cd debian/build && ../../dedup-firmware.sh install

override_dh_installdocs:
	dh_installdocs -XTODO

CONTROL_FILES = debian/build/version-info $(wildcard debian/templates/*.in)
CONTROL_FILES += debian/bin/gencontrol.py debian/config/defines $(wildcard debian/config/*/defines) debian/modinfo.json

# debian/bin/gencontrol.py uses debian/changelog as input, but the
# output only depends on the source name and version.  To avoid
# frequent changes to debian/control.md5sum, include only those fields
# in the checksum.
debian/build/version-info: debian/changelog
	mkdir -p $(@D)
	printf >$@ 'Source: %s\nVersion: %s\n' $(SOURCE) $(VERSION_SOURCE)

debian/control: $(GENCONTROL) $(CONTROL_FILES)
ifeq ($(wildcard debian/control.md5sum),)
	$(MAKE) -f debian/rules debian/control-real
else
	md5sum --check debian/control.md5sum --status || \
		$(MAKE) -f debian/rules debian/control-real
endif

debian/control-real: $(GENCONTROL) $(CONTROL_FILES)
# We currently need to run {copy,dedup}-firmware.sh to get a complete
# list of symlinks to include in package descriptions.
	./copy-firmware.sh debian/build/install
	cd debian/build && ../../dedup-firmware.sh install
	$(GENCONTROL)
	md5sum $^ > debian/control.md5sum
	@echo
	@echo This target is made to fail intentionally, to make sure
	@echo that it is NEVER run during the automated build. Please
	@echo ignore the following error, the debian/control file has
	@echo been generated SUCCESSFULLY.
	@echo
	exit 1

DIR_ORIG = ../orig/$(SOURCE)-$(VERSION_UPSTREAM)
TAR_ORIG_NAME = $(SOURCE)_$(VERSION_UPSTREAM).orig.tar.xz
TAR_ORIG = $(firstword $(wildcard ../$(TAR_ORIG_NAME)) $(wildcard ../orig/$(TAR_ORIG_NAME)))

orig: $(DIR_ORIG)
	rsync --delete --exclude /debian --exclude /.git --link-dest=$(DIR_ORIG)/ -a $(DIR_ORIG)/ .
	QUILT_PATCHES='$(CURDIR)/debian/patches' QUILT_PC=.pc quilt push --quiltrc - -a -q --fuzz=0

$(DIR_ORIG):
ifeq ($(TAR_ORIG),)
	$(error Cannot find orig tarball $(TAR_ORIG_NAME))
else
	mkdir -p ../orig
	tar -C ../orig -xaf $(TAR_ORIG)
endif

clean-generated:
	rm -rf $(CLEAN_PATTERNS)
# We cannot use dh_clean here because it requires debian/control to exist
	rm -rf debian/.debhelper debian/*.debhelper* debian/debhelper-build-stamp debian/files debian/generated.*
	rm -f \
		debian/control \
		debian/control.md5sum \
		debian/*.bug-presubj \
		debian/*.install \
		debian/*.metainfo.xml \
		debian/*.postinst \
		debian/*.postrm \
		debian/*.preinst \
		debian/*.prerm \
		debian/*.templates

maintainerclean: clean-generated
	rm -rf $(filter-out debian .git, $(wildcard * .[^.]*))

.PHONY: clean build-indep build-arch build binary-indep binary-arch binary \
	clean-generated maintainerclean
