#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

SOVERSION	 = 5.3

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

DEB_HOST_MULTIARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

get_package_version	= $(shell dpkg-query -s $1 2>/dev/null | grep ^Version: | cut -d ' ' -f2)

# Upstream does not use CPPFLAGS
CFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS) \
        $(shell dpkg-buildflags --get CFLAGS)
export CFLAGS

%:
	dh $@ --sourcedirectory=src --with autoreconf

.PHONY: override_dh_auto_configure
override_dh_auto_configure: build-generated
	dh_auto_configure --sourcedirectory=src -- \
		--with-static-lib=yes \
		--with-shared-lib=yes \
		--with-shlib \
		--with-perf-events \
		--with-pfm-incdir=/usr/include \
		--with-pfm-libdir=/usr/lib/$(DEB_HOST_MULTIARCH)

.PHONY: override_dh_auto_test
override_dh_auto_test:
	-dh_auto_test

.PHONY: override_dh_auto_install
override_dh_auto_install:
	dh_auto_install --sourcedirectory=src -- install-all

.PHONY: override_dh_shlibdeps
override_dh_shlibdeps:
	find debian/libpapi$(SOVERSION)/usr/lib/$(DEB_HOST_MULTIARCH) -type l -name 'libpapi.so.*' | xargs -r rm -v
	ldconfig -n -v debian/libpapi$(SOVERSION)/usr/lib/$(DEB_HOST_MULTIARCH)
	dh_shlibdeps -a -- --warnings=7

.PHONY: override_dh_install
override_dh_install:
	dh_install --list-missing

.PHONY: override_dh_installchangelogs
override_dh_installchangelogs:
	dh_installchangelogs RELEASENOTES.txt

.PHONY: override_dh_strip
override_dh_strip:
	dh_strip --dbg-package=papi-dbg

.PHONY: override_dh_compress
override_dh_compress:
	dh_compress -X.c -X.h -X.F -XMakefile

.PHONY: override_dh_gencontrol
override_dh_gencontrol:
	dh_gencontrol -- \
		-V'papi:SOVERSION=$(SOVERSION)' \
		-V'libpfm4:MinVersion=$(call get_package_version,libpfm4-dev)'


TEMPLATES	:= $(wildcard debian/libpapiSOVERSION.*)
GENERATED	 = $(subst SOVERSION,$(SOVERSION),$(TEMPLATES))

libpapi$(SOVERSION).%: libpapiSOVERSION.%
	sed 's/@SOVERSION@/$(SOVERSION)/g' $< > $@

build-generated: $(GENERATED) ;
clean-generated:
	$(RM) $(GENERATED)

clean: clean-generated
