#!/usr/bin/make -f
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export DEB_HOST_MULTIARCH
DEB_DEBIAN_DIR=$(dir $(firstword $(MAKEFILE_LIST)))
VER=$(shell dpkg-parsechangelog -l$(DEB_DEBIAN_DIR)/changelog \
              | sed -rne 's,^Version: ([^-]+).*,\1,p')
VER_MAJOR=$(shell echo $(VER) | sed -rne 's,^([0-9]+)(\.[0-9]+){3}(\+.*)?$$,\1,p')
VER_API=$(shell echo $(VER) | sed -rne 's,^[0-9]+\.([0-9]+)(\.[0-9]+){2}(\+.*)?$$,\1,p')
VER_ABI=$(shell echo $(VER) | sed -rne 's,^([0-9]+\.){2}([0-9]+)\.[0-9]+(\+.*)?$$,\2,p')
VER_PATCH=$(shell echo $(VER) | sed -rne 's,^([0-9]+\.){3}([0-9]+)$$,\2,p')

GITREV=$(shell echo $(VER) | sed -rne 's,^[0-9]+\.[0-9]+.*\+[0-9]+\+g([0-f]{8})$$,\1,p' -e 's,^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$$,v\1,p')
GITCOUNT=$(shell echo $(VER) | sed -rne 's,^[0-9]+\.[0-9]+.*\+([0-9]+)\+g[0-f]{8}$$,\1,p')

%:
	dh $@ --with python3 --parallel

override_dh_auto_configure:
	dh_auto_configure -- -DLIB_SUFFIX="/$(DEB_HOST_MULTIARCH)" \
		-DGR_GIT_COUNT="$(GITCOUNT)" \
		-DGR_GIT_HASH="$(GITREV)" \
		-DVERSION_MAJOR="$(VER_MAJOR)" \
		-DVERSION_API="$(VER_API)" \
		-DVERSION_ABI="$(VER_ABI)" \
		-DVERSION_PATCH="$(VER_PATCH)"

version-to-get:
	echo $(VER) is $(GITREV)

get-orig-source: version-to-get
	git clone https://gitlab.com/librespacefoundation/gr-soapy.git .gr-soapy
	cd .gr-soapy && git archive --format=tar --prefix=gr-soapy-$(VER)/ $(GITREV) | xz > ../gr-soapy_$(VER).orig.tar.xz
	rm -rf .gr-soapy
