#!/usr/bin/make -f

DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

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 7 ] ; then echo gt ; else echo lt ; fi),gt)
   DEBVE1=yes
else
   DEBVE1=no
endif

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)

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

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

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= -O3 $(OPTI_FLAGS_LIBONLY)
export CFLAGS= -O3 $(OPTI_FLAGS_LIBONLY)

CPPFLAGS= -O3 $(OPTI_FLAGS_LIBONLY)
export CPPFLAGS= -O3 $(OPTI_FLAGS_LIBONLY)

endif

%:
ifeq ($( DEBVE1),yes)
	dh ${@} --parallel --with autoreconf
else
	dh ${@} --parallel
endif

override_dh_auto_configure:
	dh_auto_configure -- --enable-debug --enable-gtk-doc --enable-udisks --disable-silent-rules --enable-demo

override_dh_auto_install:
	dh_auto_install

	# split extra headers
	mkdir -p debian/libfm-extra-dev/usr/include/libfm-1.0
	for file in fm-extra.h fm-version.h fm-xml-file.h; do \
		mv debian/tmp/usr/include/libfm-1.0/$$file debian/libfm-extra-dev/usr/include/libfm-1.0/; \
	done

	# removing unused files
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/*.a
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/*.la

override_dh_auto_test:
	# disabled: fails

override_dh_install:
	dh_install --fail-missing

override_dh_installchangelogs:
	dh_installchangelogs NEWS

override_dh_makeshlibs:
	# modules are not shared libs so disable processing for them
	dh_makeshlibs -X/modules/

override_dh_auto_clean:
	test -f docs/Makefile && cd docs && $(MAKE) distclean || true
	dh_auto_clean

override_dh_strip:
ifeq ($(DEBVE2),yes)
	dh_strip -plibfm4 -plibfm-extra4 --dbgsym-migration='libfm-dbg (<< 1.2.5-1)'
	dh_strip -plibfm-gtk4 --dbgsym-migration='libfm-gtk-dbg (<< 1.2.5-1)'
	dh_strip -plibfm-modules --dbgsym-migration='libfm-modules-dbg (<< 1.2.5-1)'
else
	dh_strip -plibfm4 -plibfm-extra4 --dbg-package=libfm-dbg
	dh_strip -plibfm-gtk4 --dbg-package=libfm-gtk-dbg
	dh_strip -plibfm-modules --dbg-package=libfm-modules-dbg
endif
	dh_strip -Nlibfm4 -Nlibfm-extra4 -Nlibfm-gtk4 -Nlibfm-modules
