#!/usr/bin/make -f
# -*- makefile -*-
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

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= $(shell dpkg-buildflags --get CFLAGS) -fPIC -O2 $(OPTI_FLAGS_LIBONLY)
export CFLAGS= $(shell dpkg-buildflags --get CFLAGS) -fPIC -O2 $(OPTI_FLAGS_LIBONLY)
CXXFLAGS= $(shell dpkg-buildflags --get CFLAGS) -fPIC -O2 $(OPTI_FLAGS_LIBONLY)
export CXXFLAGS= $(shell dpkg-buildflags --get CFLAGS) -fPIC -O2 $(OPTI_FLAGS_LIBONLY)

endif

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

override_dh_auto_clean:
	dh_auto_clean
	rm -rf html/*

override_dh_auto_build:
	dh_auto_build
	doxygen doc/trio.cfg

override_dh_auto_configure:
	dh_auto_configure -- --prefix=$(CURDIR)/debian/tmp/usr --libdir=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) CFLAGS="$(CFLAGS)"

override_dh_auto_install:
	dh_auto_install
	ln -s libtrio.so.2.0.0 $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libtrio.so.2
	ln -s libtrio.so.2.0.0 $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libtrio.so
	mkdir -p $(CURDIR)/debian/tmp/usr/share/doc/libtrio-dev/html
	install -m 644 $(CURDIR)/html/* $(CURDIR)/debian/tmp/usr/share/doc/libtrio-dev/html/

%:
	dh $@  --with autotools-dev
