#!/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 7 ] ; 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)
DEB_HOST_ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)

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

OPTI_FLAGS_LIBONLY := -funswitch-loops -fpredictive-commoning -fgcse-after-reload -ftree-vectorize -fomit-frame-pointer
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= -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)

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

override_dh_auto_configure:
	dh_auto_configure -- \
	--without-downloader --without-fftw3

override_dh_auto_install:
	rm -f debian/tmp/usr/bin/aacplusenc
	dh_auto_install --
	sed -i "/dependency_libs/ s/'.*'/''/" `find debian/ -name '*.la'`

