#!/usr/bin/make -f

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

CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS   := $(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS  := $(shell dpkg-buildflags --get LDFLAGS)

LDFLAGSSHARED := LDFLAGS

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

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

USRCXXFLAGS      := $(CPPFLAGS) $(CXXFLAGS)
USRLDFLAGSSHARED := $(LDFLAGS)

export USRCXXFLAGS USRLDFLAGSSHARED MAKEFLAGS

# package name
PKGNAME=$(strip $(shell head -n1 debian/changelog | cut -d' ' -f1))

# current package version
DEBVER=$(strip $(shell \
		head -n 1 debian/changelog | cut -f 2 -d ' ' | tr -d '()'))

# current upstream version (remove any epoch and Debian release number)
UPVER=$(shell echo $(DEBVER) | \
		 sed -e 's/-[^-]*$$//' -e 's/^[^:]*://')

../$(PKGNAME)_$(UPVER).orig.tar.gz:
	./debian/get-orig-source --upstream-version $(shell echo $(UPVER) |\
	       sed -e 's/\.//g') $@

get-orig-source: ../$(PKGNAME)_$(UPVER).orig.tar.gz

configure-stamp:
	dh_auto_configure -- --enable-shared --with-hepmc=/usr \
		--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH)
	for f in debian/lib*.install.in; do \
		sed -e 's,@DEB_HOST_MULTIARCH@,$(DEB_HOST_MULTIARCH),' $$f \
			> $${f%.in}; \
	done
	touch $@

override_dh_install:
	dh_install
	-rm -f debian/libpythia8-dev/usr/include/pythia8/HepMCInterface.h

build-arch: configure-stamp
	dh_testdir
	$(MAKE) $(MAKEFLAGS)

build-indep: configure-stamp
	dh_testdir
	cd xmldoc; xelatex worksheet; xelatex worksheet
	#cd xmldoc; xelatex pythia8100; xelatex pythia8100

build: build-arch

binary-arch: build-arch
	dh $@

binary-indep: build-indep
	dh $@

binary: binary-arch binary-indep

%:
	dh $@ 

.PHONY: build build-arch build-indep binary binary-arch binary-indep get-orig-source
