#!/usr/bin/make -f

# enable 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 5 ] ; then echo gt ; else echo lt ; fi),gt)
   DEB5=yes
else
   DEB5=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)
    OPTI_FLAGS_LIBONLY += -march=i586 -mtune=generic -mmmx 
else
ifeq ($(DEB_HOST_ARCH),i486)
    OPTI_FLAGS_LIBONLY += -march=i586 -mtune=generic -mmmx 
endif
endif

ifeq ($(DEB_HOST_ARCH),amd64)
    OPTI_FLAGS_LIBONLY += -mmmx -msse -msse2 -mfpmath=sse 
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

%:
	dh $@ --with autoreconf

override_dh_auto_clean:
	rm -rf m4
	dh_auto_clean -- 

override_dh_autoreconf:
	mkdir m4
	dh_autoreconf --

override_dh_auto_test:
	
#	make -j1 check || true
# check https://redmine.lighttpd.net/projects/lighttpd/repository/14/revisions/4a68780e1b1d5388d2d67ab518d1d3c424b792d3/diff due our paches about 200 ok

override_dh_auto_configure:
	dh_auto_configure -- \
                --disable-dependency-tracking \
                --libdir=/usr/lib/lighttpd \
                --libexecdir="/usr/lib/lighttpd" \
                --with-attr \
                --with-fam \
                --with-gdbm \
                --with-krb5 \
                --with-ldap \
                --with-geoip \
                --with-lua=lua5.1 \
                --with-mysql \
                --with-openssl \
                --with-pcre \
                --with-webdav-locks \
                --with-webdav-props \
                CFLAGS_FOR_BUILD="$(LCFLAGSB) $(OPTI_FLAGS_LIBONLY)" \
		CPPFLAGS_FOR_BUILD="$(LCPFLAGSB) $(OPTI_FLAGS_LIBONLY)"

override_dh_install:
	cp NEWS debian/tmp/changelog
	dh_install

override_dh_fixperms:
	dh_fixperms
	test -d debian/lighttpd && \
	    chmod 0750 debian/lighttpd/var/log/lighttpd && \
	    chown www-data:www-data \
	        debian/lighttpd/var/cache/lighttpd/compress \
	        debian/lighttpd/var/cache/lighttpd/uploads \
	        debian/lighttpd/var/log/lighttpd || true

override_dh_installinit:
	dh_installinit --error-handler=start_failed
