#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

%:
	dh $@

# Don't split debug info from Hexagon libraries
override_dh_dwz:
	dh_dwz -Xusr/share/hexagon-dsp

override_dh_strip:
	dh_strip -Xusr/share/hexagon-dsp

override_dh_shlibdeps:
	dh_shlibdeps -Xusr/share/hexagon-dsp

# Upstream check.py can't work with Debian's packaging, it requires original
# Git repo. Ignore it, as its main goal is to verify that files under Git
# control match the WHENCE and config files.
override_dh_auto_test:

# Override the target location
override_dh_auto_install:
	$(MAKE) install DESTDIR=debian/tmp DSPDIR=usr/share/hexagon-dsp

REGEN = debian/regen.sh
CONTROLTEMPLATES = debian/control.head.in debian/control.dev.in debian/control.main.in debian/pkgname.lintian-overrides.in

VERSION_BASE := $(shell echo "$(DEB_VERSION)" | sed -e 's,-[^-]*$$,,')
VERSION_UPSTREAM := $(shell echo "$(VERSION_BASE)" | sed -e 's,^0~,,g')
VERSION_BINNMU := $(shell echo "$(DEB_VERSION)" | sed -rne 's,.*\+b([0-9]+)$$,\1,p')
VERSION_SOURCE := $(patsubst %+b$(VERSION_BINNMU),%,$(DEB_VERSION))

DIR_ORIG = ../orig/$(DEB_SOURCE)-$(VERSION_UPSTREAM)
TAR_ORIG_NAME = $(DEB_SOURCE)_$(VERSION_BASE).orig.tar.gz
TAR_ORIG = $(firstword $(wildcard ../$(TAR_ORIG_NAME)) $(wildcard ../orig/$(TAR_ORIG_NAME)))

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

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

# debian/regen.sh 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' $(DEB_SOURCE) $(VERSION_SOURCE)

debian/control: $(REGEN) config.txt $(CONTROLTEMPLATES) debian/build/version-info
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: $(REGEN) config.txt $(CONTROLTEMPLATES) debian/changelog
	$(REGEN) $(VERSION_UPSTREAM)
	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

.PHONY: debian/control-real
