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

# Get version from changelog as well as lsb_release
# to compute unique build version (useful for repo mirroring systems doing file pooling)
PKG_VERSION  := $(shell dpkg-parsechangelog | grep -E '^Version:' | cut -d' ' -f2)
DIST_NAME    := $(shell lsb_release -si)
DIST_RELEASE := $(shell lsb_release -sc)
DIST_VERSION := $(shell lsb_release -sr)
PKG_VERSION_TAG := $(PKG_VERSION)+$(DIST_NAME).$(DIST_RELEASE).$(DIST_VERSION)

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)

CXXFLAGS += -fPIC

ifeq ($(DEB_HOST_ARCH),hppa)
CXXFLAGS += -mlong-calls
else ifeq ($(DEB_BUILD_ARCH), mips)
CXXFLAGS += -mxgot -g1
else ifeq ($(DEB_BUILD_ARCH), mips64el)
CXXFLAGS += -mxgot
else ifeq ($(DEB_BUILD_ARCH), mipsel)
CXXFLAGS += -mxgot -g1
endif

export CXXFLAGS

%:
	dh $@ --parallel

override_dh_auto_configure:
	dh_auto_configure --buildsystem=cmake -- -Dgmock_build_tests=ON -Dgtest_build_tests=ON

override_dh_autoreconf:
	autoreconf -vif

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	cd obj-* && ctest || (cat Testing/Temporary/LastTest.log; exit 1)
endif

override_dh_install:
	dh_install
	find debian/googletest/usr/src/googletest -iname autom4te.cache -type d | xargs rm -rf
	find debian/googletest/usr/src/googletest -iname LICENSE -o -iname .gitignore -o -iname '*.pyc' | xargs rm
	find debian/googletest/usr/src/googletest -iname '*.py' | xargs chmod -x
	rm -rf debian/googletest/usr/src/googletest/*/msvc
	for f in $$(ls debian/googletest-tools/usr/bin/*.py); \
	  do mv $$f $$(echo $$f | sed s/\.py//); \
	done
	chmod a-x -R debian/googletest-tools/usr/share/googletest-tools/generator/cpp/*

override_dh_clean:
	dh_clean
	rm -f test/*.pyc

override_dh_gencontrol:
	dh_gencontrol -- -v"$(PKG_VERSION_TAG)"
