#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
DH_VERBOSE = 1


# Disable the following tests as they require network access
BAD_TESTS=10http1client 50access-log 50reverse-proxy-added-headers 50reverse-proxy-drop-headers 50reverse-proxy-https 50reverse-proxy-session-resumption 80invalid-h2-chars-in-headers 40server-push 50mruby-acl 80issues595 40max-connections 40session-ticket

DEBVE=no
DEBVE2=no
devi := $(shell lsb_release -r -s | cut -d '.'  -f1)
ifeq ($(shell if [ $(devi) -gt 7 ] ; then echo gt ; else echo lt ; fi),gt)
   DEBVE=yes
# mayor than wheeze is jessie detected or superior
endif

ifeq ($(shell if [ $(devi) -gt 8 ] ; then echo gt ; else echo lt ; fi),gt)
   DEBVE2=yes
# mayor than jessie is stretch detected or superior
endif

# Enable only working hardening flags
export PERL5LIB=$(CURDIR)
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

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    CFLAGS += -O0
else

DEB_HOST_ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)
OPTI_FLAGS_LIBONLY := -fomit-frame-pointer
ifeq ($(DEB_HOST_ARCH),i386)
    OPTI_FLAGS_LIBONLY += -march=i586 -mtune=generic -mmmx 
endif
ifeq ($(DEB_HOST_ARCH),i486)
    OPTI_FLAGS_LIBONLY += -march=i586 -mtune=generic -mmmx 
endif
ifeq ($(DEB_HOST_ARCH),amd64)
    OPTI_FLAGS_LIBONLY += -mmmx -msse -msse2
endif

CFLAGS= -O2 $(OPTI_FLAGS_LIBONLY)
export CFLAGS= -O2 $(OPTI_FLAGS_LIBONLY)
CPPFLAGS= -O2 $(OPTI_FLAGS_LIBONLY)
export CPPFLAGS= -O2 $(OPTI_FLAGS_LIBONLY)
VNXCFLAGS= -O2 $(OPTI_FLAGS_LIBONLY)
endif


%:
	dh $@

override_dh_auto_configure:
ifeq ($(DEBVE2),yes)
	dh_auto_configure -- -DCMAKE_INSTALL_SYSCONFDIR=/etc/h2o -DWITH_BUNDLED_SSL=off -DWITH_MRUBY=on -DBUILD_SHARED_LIBS=on -DCMAKE_C_FLAGS="$(VNXCFLAGS)"
else
	dh_auto_configure -- -DCMAKE_INSTALL_SYSCONFDIR=/etc/h2o -DWITH_BUNDLED_SSL=on -DWITH_MRUBY=on -DBUILD_SHARED_LIBS=on -DCMAKE_C_FLAGS="$(VNXCFLAGS)"
endif

override_dh_auto_clean:
	rm -f include/h2o.h.gch
	make -C deps/mruby clean
	rm -f deps/mruby/bin/mruby-config
	rm -f deps/mruby/mkmf.log
	dh_auto_clean

override_dh_auto_test:
ifeq ($(DEBVE2),yes)
	
else
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	for test in $(BAD_TESTS); do mv $(CURDIR)/t/$$test.t $(CURDIR)/t/$$test.disabled; done
	dh_auto_test || true
	for test in $(BAD_TESTS); do mv $(CURDIR)/t/$$test.disabled $(CURDIR)/t/$$test.t; done
endif
endif

ifeq ($(DEBVE2),yes)
override_dh_installinit:
	# Do not do a full restart, just a graceful reload
	dh_installinit --no-restart-after-upgrade --no-stop-on-upgrade
endif

override_dh_installdocs:
	dh_installdocs
	find $(CURDIR)/debian/h2o-doc/usr/share/ -type f -print0 | xargs --no-run-if-empty -0 sed -i 's/jquery-1\.9\.1/jquery/g'

ifeq ($(DEBVE),yes)
override_dh_missing:
	dh_missing --fail-missing
endif

