#!/usr/bin/make -f

ifneq (,$(findstring amd64,$(DEB_BUILD_ARCH)))
export DEB_BUILD_MAINT_OPTIONS = hardening=+fortify,-stackprotector,+relro,+bindnow,-pie
else
export DEB_BUILD_MAINT_OPTIONS = hardening=+fortify,-stackprotector,+relro,+bindnow,+pie
endif

devi := $(shell lsb_release -r -s | cut -d '.'  -f1)
ifeq ($(shell if [ $(devi) -gt 8 ] ; then echo gt ; else echo lt ; fi),gt)
   DEBVE2=yes
else
   DEBVE2=no
endif

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

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    CFLAGS += -O0
else

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

DEB_HOST_ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)
OPTI_FLAGS_LIBONLY := -funswitch-loops -fpredictive-commoning -fgcse-after-reload -ftree-vectorize -fomit-frame-pointer
### PICCORO optimizer flags for i386 x86 machines
ifeq ($(DEB_HOST_ARCH),i386)
    OPTI_FLAGS_LIBONLY += -march=i586 -mtune=generic -mmmx -fexcess-precision=fast 
else
ifeq ($(DEB_HOST_ARCH),i486)
    OPTI_FLAGS_LIBONLY += -march=i586 -mtune=generic -mmmx -fexcess-precision=fast 
endif
endif

ifeq ($(DEB_HOST_ARCH),amd64)
    OPTI_FLAGS_LIBONLY += -mmmx -msse -msse2 -mfpmath=sse -fexcess-precision=fast 
endif

CFLAGS= -O2 $(OPTI_FLAGS_LIBONLY)
export CFLAGS= -O2 $(OPTI_FLAGS_LIBONLY)

CXXFLAGS= -O2 $(OPTI_FLAGS_LIBONLY)
export CXXFLAGS= -O2 $(OPTI_FLAGS_LIBONLY)

endif

LDFLAGS=$(shell dpkg-buildflags --get LDFLAGS)

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

%:
	dh ${@}

override_dh_auto_configure:
	dh_auto_configure -- --enable-gtk-doc --libexecdir=/usr/lib

override_dh_auto_install:
	dh_auto_install
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/*.la

override_dh_install:
	dh_install --fail-missing

override_dh_installchangelogs:
	dh_installchangelogs NEWS

ifeq ($(DEBVE2),yes)
override_dh_strip:
	dh_strip -plibmenu-cache3 --dbgsym-migration='libmenu-cache-dbg (<< 1.1.0-1)'
	dh_strip -plibmenu-cache-bin --dbgsym-migration='libmenu-cache-bin-dbg (<< 1.1.0-1)'
else
	dh_strip -plibmenu-cache3 --dbg-package=libmenu-cache-dbg
	dh_strip -plibmenu-cache-bin --dbg-package=libmenu-cache-bin-dbg
endif

override_dh_autoreconf_clean:
	dh_autoreconf_clean
	rm -f docs/reference/libmenu-cache/libmenu-cache-docs.sgml
	rm -f docs/reference/libmenu-cache/libmenu-cache-sections.txt
