#!/usr/bin/make -f
# -*- makefile -*-

# extract the architecture for setting the library path suffix
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# extract the last section of the changelog version for extra info
DEB_VERSION ?= $(shell dpkg-parsechangelog -S Version)
DEB_VERSION_EXTRA := $(lastword $(subst -, ,$(DEB_VERSION)))
VERSION_MAJOR := $(word 1,$(subst ., ,$(DEB_VERSION)))
VERSION_MINOR := $(word 2,$(subst ., ,$(DEB_VERSION)))

#create install scripts with dynamic names based on the release
#we could manually re-name them, but this automates everything but control/changelog
RELEASE_NUM = $(VERSION_MAJOR).$(VERSION_MINOR)
GENERATED_INSTALL_FILES := \
	debian/limesuite-images$(RELEASE_NUM).dirs \
	debian/limesuite-images$(RELEASE_NUM).postinst \
	debian/limesuite-images$(RELEASE_NUM).prerm

debian/limesuite-images$(RELEASE_NUM)%: debian/limesuite-images%.in
	sed 's/@RELEASE_NUM@/$(RELEASE_NUM)/g' $< > $@

# This has to be exported to make some magic below work.
export DH_OPTIONS

%:
	dh $@ --buildsystem=cmake --parallel

override_dh_auto_configure: $(GENERATED_INSTALL_FILES)
	dh_auto_configure -- \
		-DBUILD_SHARED_LIBS=ON \
		-DCMAKE_AUTOSET_INSTALL_RPATH=FALSE \
		-DUDEV_RULES_PATH=/lib/udev/rules.d \
		-DLIB_SUFFIX="/$(DEB_HOST_MULTIARCH)" \
		-DLIME_SUITE_EXTVER="$(DEB_VERSION_EXTRA)"

override_dh_installchangelogs:
	dh_installchangelogs Changelog.txt
