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

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

liblzo2_source := $(shell dpkg-query -W -f='$${source:Package} (= $${source:Version})' liblzo2-dev)

DEB_HOST_ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)
OPTI_FLAGS_LIBONLY := -falign-functions=0 -falign-jumps=0 -falign-loops=0 -funswitch-loops -fpredictive-commoning -fgcse-after-reload -ftree-vectorize -fomit-frame-pointer

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)

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

%:
	dh $@ --with autoreconf

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
# commented, currently system tremot causes troubles, use embebed
#	# Restore bundled source code
#	[ ! -d src/tremor.orig ] || mv src/tremor.orig src/tremor
	dh_auto_clean

override_dh_auto_configure:
	dh_auto_configure -- --program-suffix=08 --bindir=/usr/bin --with-x --x-includes=/usr/include
#	dh_auto_configure -- --program-suffix=08 --bindir=/usr/bin --disable-nls 


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
# commented, currently system tremot causes troubles, use embebed
#	# Move bundled source code for external packages out of the way
#	mv src/tremor src/tremor.orig
	dh_auto_build -- INCLUDES='-I/usr/include/GL'
