#!/usr/bin/make -f
export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS=hardening=+all
debian_cflags:=$(shell dpkg-buildflags --get CFLAGS) -fPIC $(shell dpkg-buildflags --get CPPFLAGS)
debian_ldflags:=$(shell dpkg-buildflags --get LDFLAGS) -fPIC

FLAVOURS := bin
DYN_MODS := \
	http-geoip \
	http-image-filter \
	http-perl \
	http-xslt-filter \
	mail \
	stream \
	stream-geoip

BASEDIR = $(CURDIR)
$(foreach flavour,$(FLAVOURS) src ,$(eval BUILDDIR_$(flavour) = $(CURDIR)/debian/build-$(flavour)))

DEB_BUILD_ARCH ?=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
ifeq ($(DEB_BUILD_ARCH),sparc)
	debian_cflags += -m32 -mcpu=ultrasparc
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

# configure flags
basic_configure_flags := \
			--prefix=/usr/share/nginx \
			--conf-path=/etc/nginx/nginx.conf \
			--http-log-path=/var/log/nginx/access.log \
			--error-log-path=stderr \
			--lock-path=/var/lock/nginx.lock \
			--pid-path=/run/nginx.pid \
			--modules-path=/usr/lib/nginx/modules \
			--http-client-body-temp-path=/var/lib/nginx/body \
			--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
			--http-proxy-temp-path=/var/lib/nginx/proxy \
			--http-scgi-temp-path=/var/lib/nginx/scgi \
			--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
			--with-compat \
			--with-debug \
			--with-pcre-jit \
			--with-http_ssl_module \
			--with-http_stub_status_module \
			--with-http_realip_module \
			--with-http_auth_request_module \
			--with-http_v2_module \
			--with-http_dav_module \
			--with-http_slice_module \
			--with-threads

common_configure_flags := \
			--with-cc-opt="$(debian_cflags)" \
			--with-ld-opt="$(debian_ldflags)" \
			$(basic_configure_flags)

bin_configure_flags := \
			$(common_configure_flags) \
			--with-http_addition_module \
			--with-http_flv_module \
			--with-http_gunzip_module \
			--with-http_gzip_static_module \
			--with-http_mp4_module \
			--with-http_random_index_module \
			--with-http_secure_link_module \
			--with-http_sub_module \
			--with-mail_ssl_module \
			--with-stream_ssl_module \
			--with-stream_ssl_preread_module \
			--with-stream_realip_module \
			--with-http_geoip_module=dynamic \
			--with-http_image_filter_module=dynamic \
			--with-http_perl_module=dynamic \
			--with-http_xslt_module=dynamic \
			--with-mail=dynamic \
			--with-stream=dynamic \
			--with-stream_geoip_module=dynamic

%:
	dh $@ --without autoreconf

override_dh_auto_configure: $(foreach flavour,$(FLAVOURS),config.arch.$(flavour)) config.src
override_dh_auto_build:     $(foreach flavour,$(FLAVOURS),build.arch.$(flavour)) build.src
override_dh_strip:          $(foreach flavour,$(FLAVOURS),strip.arch.$(flavour)) $(foreach mod,$(DYN_MODS),strip.mods.$(mod))
override_dh_clean:          $(foreach flavour,$(FLAVOURS),clean.$(flavour)) clean.src
	dh_clean

override_dh_install:
	dh_install
	DH_AUTOSCRIPTDIR=$(CURDIR)/debian/autoscripts debian/debhelper/dh_nginx --in-nginx-tree

override_dh_installinit:
	dh_installinit --no-stop-on-upgrade --no-start --name=nginx

override_dh_installsystemd:
	dh_installsystemd --name=nginx

override_dh_installlogrotate:
	dh_installlogrotate --package nginx-common --name=nginx

override_dh_gencontrol:
	dh_gencontrol -- -Tdebian/substvars -Tdebian/libnginx-mod.abisubstvars

build.arch.%:
	$(MAKE) -C $(BUILDDIR_$*) build

build.src:
	cp -Pa $(CURDIR)/auto $(BUILDDIR_src)/
	sed -i '/^# create Makefile/,/^END$$/d' $(BUILDDIR_src)/auto/make $(BUILDDIR_src)/auto/init $(BUILDDIR_src)/auto/install
	find $(CURDIR)/src -type f -name '*.h' -printf 'src/%P\0' | tar -C $(CURDIR) --null --files-from - -c | tar -C $(BUILDDIR_src)/ -x
	if [ -e $(CURDIR)/configure ]; then cp $(CURDIR)/configure $(BUILDDIR_src)/; fi
	echo "NGX_CONF_FLAGS=(" $(basic_configure_flags) ")" > $(BUILDDIR_src)/conf_flags
	pod2man debian/debhelper/dh_nginx > $(BUILDDIR_src)/dh_nginx.1

strip.arch.%:
	#dh_strip --package=nginx-$(*) -O--dbgsym-migration='nginx-$(*)-dbg (<< 1.10.1-3~)'
	dh_strip --package=nginx -O--dbgsym-migration='nginx-dbg (<< 1.10.1-3~)'

strip.mods.%:
	dh_strip --package=libnginx-mod-$(*) -O--automatic-dbgsym

config.arch.%:
	dh_testdir
	mkdir -p $(BUILDDIR_$*)
	cp -Pa $(CURDIR)/auto $(BUILDDIR_$*)/
	cp -Pa $(CURDIR)/conf $(BUILDDIR_$*)/
	cp -Pa $(CURDIR)/configure $(BUILDDIR_$*)/
	cp -Pa $(CURDIR)/contrib $(BUILDDIR_$*)/
	cp -Pa $(CURDIR)/src $(BUILDDIR_$*)/
	cp -Pa $(CURDIR)/man $(BUILDDIR_$*)/
	cd $(BUILDDIR_$*) && ./configure $($*_configure_flags)

config.src:
	dh_testdir
	mkdir -p $(BUILDDIR_src)

clean.%:
	rm -rf $(BUILDDIR_$*)
