#!/usr/bin/make -f
export DH_OPTIONS

export REPACK_SH=$(CURDIR)/debian/repack.sh
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifeq ($(DEB_HOST_ARCH),armhf)
	confflags += --disable-optimize
endif

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 := -fomit-frame-pointer
### PICCORO optimizer flags for i386 x86 machines
ifeq ($(DEB_HOST_ARCH),i386)
ifeq ($(DEBVE2),yes)
    OPTI_FLAGS_LIBONLY += -march=i686 -mtune=generic -mmmx -msse -mfpmath=sse -fexcess-precision=fast 
else
    OPTI_FLAGS_LIBONLY += -march=i586 -mtune=generic -mmmx -fexcess-precision=fast 
endif
else
ifeq ($(DEB_HOST_ARCH),i486)
ifeq ($(DEBVE2),yes)
    OPTI_FLAGS_LIBONLY += -march=i686 -mtune=generic -mmmx -msse -mfpmath=sse -fexcess-precision=fast 
else
    OPTI_FLAGS_LIBONLY += -march=i586 -mtune=generic -mmmx -fexcess-precision=fast 
endif
endif
endif

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

CFLAGS= -Wno-narrowing -O2 $(OPTI_FLAGS_LIBONLY) -fpermissive
export CFLAGS= -Wno-narrowing -O2 $(OPTI_FLAGS_LIBONLY) -fpermissive

CXXFLAGS= -Wno-narrowing -O2 $(OPTI_FLAGS_LIBONLY) -fpermissive
export CXXFLAGS= -Wno-narrowing -O2 $(OPTI_FLAGS_LIBONLY) -fpermissive

endif

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

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

export DEB_CFLAGS_MAINT_APPEND = -Wno-narrowing -O2 $(OPTI_FLAGS_LIBONLY) -fpermissive
export DEB_CXXFLAGS_MAINT_APPEND = -Wno-narrowing -O2 $(OPTI_FLAGS_LIBONLY) -fpermissive

%:
	dh $@ --parallel --with autoreconf

override_dh_auto_configure:
	dh_auto_configure -- $(confflags)
#	dh_auto_configure -- --enable-static=no $(confflags)

override_dh_auto_install:
	dh_auto_install -- 
	sed -i "/dependency_libs/ s/'.*'/''/" `find debian/ -name '*.la'`

override_dh_auto_test:
	dh_auto_test || true
	
override_dh_strip:
	dh_strip --dbg-package=mp4v2-dbg

override_dh_installchangelogs:
	dh_installchangelogs -plibmp4v2-2 doc/ReleaseNotes.txt
	dh_installchangelogs --remaining-packages

get-orig-source:
	uscan --force-download
