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

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

#export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie

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

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CUFLAGS += -O0
else
ifneq (,$(findstring i386,$(DEB_BUILD_ARCH)))
CUFLAGS += -O2 -fomit-frame-pointer -march=i686 -mmmx -mno-sse4 -mno-sse3 -msse 
else
ifneq (,$(findstring i486,$(DEB_BUILD_ARCH)))
CUFLAGS += -O2 -fomit-frame-pointer -march=i686 -mmmx -mno-sse4 -mno-sse3 -msse 
else
CUFLAGS += -O2 -fomit-frame-pointer -mno-sse4 -mno-sse3 -mno-sse2 
endif
endif
endif

ifneq (,$(findstring amd64,$(DEB_BUILD_ARCH)))
CUFLAGS += -O2 -fomit-frame-pointer -msse -msse2 -mno-sse4 -mno-sse3 
endif

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)

devi := $(shell lsb_release -r -s | cut -d '.'  -f1)
ifeq ($(shell if [ $(devi) -gt 7 ] ; then echo gt ; else echo lt ; fi),gt)
   CNFLAGS=$(CUFLAGS)
export DEB_BUILD_MAINT_OPTIONS = hardening=+fortify,+stackprotector,+relro,+bindnow,-pie
else
CNFLAGS = -Wall -fomit-frame-pointer -fwrapv
ifneq (,$(findstring amd64,$(DEB_BUILD_ARCH)))
CNFLAGS = -fomit-frame-pointer -msse -mno-sse4 -mno-sse3 -fwrapv 
endif
ifneq (,$(findstring i386,$(DEB_BUILD_ARCH)))
CNFLAGS= -march=i686 -mmmx -fomit-frame-pointer -msse -mno-sse2 -mno-sse4 -mno-sse3 -fwrapv 
endif
export DEB_BUILD_MAINT_OPTIONS = hardening=-fortify,-stackprotector,-relro,-bindnow,-pie
endif

CFLAGS=$(CNFLAGS)
export CFLAGS=$(CNFLAGS)
CXXFLAGS=$(CNFLAGS)
export CXXFLAGS=$(CNFLAGS)

#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)'
