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

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

devi := $(shell lsb_release -r -s | cut -d '.'  -f1)

ifeq ($(shell if [ $(devi) -gt 7 ] ; then echo gt ; else echo lt ; fi),gt)
export DEB_BUILD_MAINT_OPTIONS = hardening=+fortify,+stackprotector,+relro,+bindnow,-pie
else
export DEB_BUILD_MAINT_OPTIONS = hardening=-fortify,-stackprotector,-relro,-bindnow,-pie
endif

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

endif
#liblzo2_source := $(shell dpkg-query -W -f='$${source:Package} (= $${source:Version})\n' liblzo2-dev | head -n 1)


%:
	dh $@ --with autoreconf

#override_dh_auto_build:
#	# Update MiniLZO
#	cp src/compress/lzoconf.h src/compress/lzoconf.h.orig
#	cp src/compress/lzodefs.h src/compress/lzodefs.h.orig
#	cp src/compress/minilzo.c src/compress/minilzo.c.orig
#	cp src/compress/minilzo.h src/compress/minilzo.h.orig
#	cp /usr/share/lzo/minilzo/*.[ch] src/compress
#	dh_auto_build

#override_dh_auto_clean:
#	# Restore MiniLZO
#	[ ! -f src/compress/lzoconf.h.orig ] || mv src/compress/lzoconf.h.orig src/compress/lzoconf.h
#	[ ! -f src/compress/lzodefs.h.orig ] || mv src/compress/lzodefs.h.orig src/compress/lzodefs.h
#	[ ! -f src/compress/minilzo.c.orig ] || mv src/compress/minilzo.c.orig src/compress/minilzo.c
#	[ ! -f src/compress/minilzo.h.orig ] || mv src/compress/minilzo.h.orig src/compress/minilzo.h
#	dh_auto_clean

override_dh_auto_configure:
#	dh_auto_configure -- --program-suffix=09 --bindir=/usr/bin --enable-psx --with-external-mpcdec --with-external-tremor --with-external-trio --with-external-lzo
	dh_auto_configure -- --program-suffix=09 --bindir=/usr/bin --enable-psx --disable-nls 

override_dh_install:
	dh_install -- 
	

#override_dh_gencontrol-arch:
#	dh_gencontrol -a -- '-Vbu:liblzo2=$(liblzo2_source)'
