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

# export necessary perl hardenging flags
# see: src/http/modules/perl/Makefile.PL
debian_cflags:=$(shell dpkg-buildflags --get CFLAGS) -fPIC -flto -ffat-lto-objects $(shell dpkg-buildflags --get CPPFLAGS)
debian_ldflags:=$(shell dpkg-buildflags --get LDFLAGS) -fPIC -flto -ffat-lto-objects

DEBIAN_NGINX_PERL_LDFLAGS:= $(shell dpkg-buildflags --get LDFLAGS)
export DEBIAN_NGINX_PERL_LDFLAGS

FLAVOURS := full light extras custom ac

MODULESDIR = $(CURDIR)/debian/modules
BASEDIR = $(CURDIR)
$(foreach flavour,$(FLAVOURS),$(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
common_configure_flags := \
            --with-cc-opt="$(debian_cflags)" \
            --with-ld-opt="$(debian_ldflags)" \
            --prefix=/usr/share/nginx \
            --conf-path=/etc/nginx/nginx.conf \
            --http-log-path=/var/log/nginx/access.log \
            --error-log-path=/var/log/nginx/error.log \
            --lock-path=/var/lock/nginx.lock \
            --pid-path=/run/nginx.pid \
            --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 \
            --build='AgiusCloud Nginx' \
            --with-debug \
            --with-pcre-jit \
            --with-file-aio \
            --with-threads \
            --with-http_ssl_module \
            --with-http_v2_module \
            --with-http_stub_status_module \
            --with-http_realip_module \
            --with-http_auth_request_module \
            --with-openssl-opt="zlib enable-ec_nistp_64_gcc_128 enable-tls1_3 no-ssl3-method -fPIC"

light_configure_flags := \
            $(common_configure_flags) \
            --with-http_gzip_static_module \
            --without-http_browser_module \
            --without-http_geo_module \
            --without-http_limit_req_module \
            --without-http_limit_conn_module \
            --without-http_memcached_module \
            --without-http_referer_module \
            --without-http_scgi_module \
            --without-http_ssi_module \
            --without-http_userid_module \
            --without-http_uwsgi_module \
            --add-module=$(MODULESDIR)/nginx-echo

full_configure_flags := \
            $(common_configure_flags) \
            --with-http_addition_module \
            --with-http_geoip_module \
            --with-http_gzip_static_module \
            --with-http_sub_module \
            --with-http_xslt_module \
            --with-mail_ssl_module \
            --add-module=$(MODULESDIR)/nginx-auth-pam \
            --add-module=$(MODULESDIR)/nginx-echo \
            --add-module=$(MODULESDIR)/nginx-upstream-fair \
            --add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module

extras_configure_flags := \
            $(common_configure_flags) \
            --with-http_addition_module \
            --with-http_dav_module \
            --with-http_flv_module \
            --with-http_geoip_module \
            --with-http_gzip_static_module \
            --with-http_mp4_module \
            --with-http_perl_module \
            --with-http_random_index_module \
            --with-http_secure_link_module \
            --with-http_sub_module \
            --with-http_xslt_module \
            --with-mail \
            --with-mail_ssl_module \
            --add-module=$(MODULESDIR)/headers-more-nginx-module \
            --add-module=$(MODULESDIR)/nginx-auth-pam \
            --add-module=$(MODULESDIR)/nginx-cache-purge \
            --add-module=$(MODULESDIR)/nginx-development-kit \
            --add-module=$(MODULESDIR)/nginx-echo \
            --add-module=$(MODULESDIR)/nginx-upstream-fair \
            --add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module

custom_configure_flags := \
            $(common_configure_flags) \
            --with-http_addition_module \
            --with-http_geoip_module \
            --with-http_gzip_static_module \
            --with-http_mp4_module \
            --with-http_sub_module \
            --with-http_xslt_module \
            --without-http_memcached_module \
            --add-module=$(MODULESDIR)/headers-more-nginx-module \
            --add-module=$(MODULESDIR)/nginx-auth-pam \
            --add-module=$(MODULESDIR)/nginx-cache-purge \
            --add-module=$(MODULESDIR)/nginx-echo \
            --add-module=$(MODULESDIR)/nginx-upstream-fair \
            --add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module \
            --add-module=$(MODULESDIR)/memc-nginx-module \
            --add-module=$(MODULESDIR)/srcache-nginx-module \
            --add-module=$(MODULESDIR)/HttpRedisModule \
            --add-module=$(MODULESDIR)/redis2-nginx-module \
            --add-module=$(MODULESDIR)/ngx_devel_kit \
            --add-module=$(MODULESDIR)/set-misc-nginx-module 

ac_configure_flags := \
            $(custom_configure_flags)

%:
	dh $@ --with systemd

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

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

override_dh_systemd_enable:
	dh_systemd_enable --name=nginx

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

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

strip.arch.%:
	dh_strip --package=nginx-$(*) --dbg-package=nginx-$(*)-dbg

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)

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