#!/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

export DEB_CFLAGS_MAINT_STRIP = -O2 -O3 -g
export DEB_CXXFLAGS_MAINT_STRIP = -O2 -O3 -g
export DEB_CFLAGS_MAINT_APPEND = -Ofast -DNGX_HTTP_HEADERS -DNGX_ZLIB_NG=1 -w -pipe -fomit-frame-pointer -Wno-error=stringop-overflow
export DEB_CXXFLAGS_MAINT_APPEND = -Ofast -DNGX_HTTP_HEADERS -DNGX_ZLIB -flto -fomit-frame-pointer -pipe

export LUAJIT_LIB=/usr/lib/x86_64-linux-gnu/
export LUAJIT_INC=/usr/include/luajit-2.1

export MODULESDIR=$(CURDIR)/debian/modules
MODULESPATCHDIR=$(CURDIR)/debian/modules/patches
modules_with_patches := $(notdir $(wildcard $(CURDIR)/debian/modules/patches/*))

FLAVOURS := bin minimal modules modules1

DYN_MODS := \
        http-auth-ldap \
        http-auth-pam \
        http-auth-spnego \
        http-access-plus \
        http-brotli \
        http-cache-purge \
	http-captcha \
        http-concat \
        http-dav-ext \
        http-doh \
	http-dynamic-etag \
        http-dynamic-limit-req \
        http-early-hints \
        http-echo \
        http-encrypted-session \
        http-eval \
        http-fancyindex \
        http-flv-live \
        http-geoip \
        http-geoip2 \
        http-headers-more-filter \
        http-hmac-secure-link \
        http-image-filter \
	http-immutable \
        http-ipset-blacklist \
        http-js-challenge \
        http-length-hiding-filter \
        http-lua \
        http-modsecurity \
        http-naxsi \
        http-ndk \
        http-njs \
        pagespeed \
        http-perl \
        http-redis2 \
        http-security-headers \
        http-set-misc \
        http-srcache-filter \
        http-subs-filter \
        http-sysguard \
        http-testcookie-access \
        http-trim-filter \
        http-uploadprogress \
        http-upstream-fair \
        http-user-agent \
        http-xslt-filter \
        http-xss-filter \
        http-vhost-traffic-status \
        http-waf \
        http-zstd \
	ipscrub \
        mail \
        nchan \
	rtmp \
        stream \
        stream-geoip \
        stream-geoip2 \
        stream-lua 
#        stream-ssl-ct \
#        http-ssl-ct \
#        ssl-ct 

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 for all packages
###
basic_configure_flags := \
			--conf-path=/etc/nginx/nginx.conf \
			--error-log-path=stderr \
			--http-client-body-temp-path=/var/lib/nginx/body \
			--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
			--http-log-path=/var/log/nginx/access.log \
			--http-proxy-temp-path=/var/lib/nginx/proxy \
			--http-scgi-temp-path=/var/lib/nginx/scgi \
			--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
			--lock-path=/var/lock/nginx.lock \
			--modules-path=/usr/lib/nginx/modules \
			--pid-path=/run/nginx.pid \
			--prefix=/usr/share/nginx \
			--with-compat \
			--with-file-aio \
			--with-threads \
		        --with-cc-opt="$(debian_cflags) -Werror=discarded-qualifiers" \
			--with-ld-opt="$(debian_ldflags)" \
			--with-pcre-jit	

###
# Dependencies needed for 3rd party modules_configure
###
common_configure_flags := \
			--with-http_dav_module \
			--with-http_ssl_module \
			--with-http_v2_module \
			--with-http_v3_module \
                        --with-stream=dynamic \
                        --with-stream_realip_module \
                        --with-stream_ssl_module \
                        --with-stream_ssl_preread_module

###
# For the angie-minimal-package, not part of the debian package but requested by users
###
minimal_configure_flags := \
                        $(basic_configure_flags) \
                        --with-http_ssl_module \
                        --with-http_v2_module \
                        --with-http_v3_module \
			--with-http_realip_module \
                        --with-http_sub_module \
			--with-http_stub_status_module \
                        --without-http_browser_module \
                        --without-http_empty_gif_module \
                        --without-http_geo_module \
                        --without-http_grpc_module \
                        --without-http_memcached_module \
                        --without-http_mirror_module \
                        --without-http_referer_module \
                        --without-http_scgi_module \
                        --without-http_split_clients_module \
			--without-http_ssi_module \
                        --without-http_userid_module \
                        --without-http_uwsgi_module \
                        --build="the minimal edition" \

###
# The main package without 3rd party modules
###
bin_configure_flags := \
			$(basic_configure_flags) \
			$(common_configure_flags) \
                        --with-http_addition_module \
                        --with-http_auth_request_module \
                        --with-http_degradation_module \
                        --with-http_flv_module \
                        --with-http_geoip_module=dynamic \
                        --with-http_gunzip_module \
                        --with-http_gzip_static_module \
                        --with-http_image_filter_module=dynamic \
                        --with-http_mp4_module \
                        --with-http_perl_module=dynamic \
                        --with-http_random_index_module \
                        --with-http_realip_module \
                        --with-http_secure_link_module \
                        --with-http_slice_module \
                        --with-http_stub_status_module \
                        --with-http_sub_module \
                        --with-http_xslt_module=dynamic \
                        --with-mail=dynamic \
                        --with-mail_ssl_module \
                        --with-stream_geoip_module=dynamic \

###
# only 3rd party modules
# http-ndk needs to put first
###
modules_configure_flags := \
			$(basic_configure_flags) \
			$(common_configure_flags) \
			--add-dynamic-module=$(MODULESDIR)/http-ndk \
                        --add-dynamic-module=$(MODULESDIR)/http-access-plus \
                        --add-dynamic-module=$(MODULESDIR)/http-auth-ldap \
                        --add-dynamic-module=$(MODULESDIR)/http-auth-pam \
                        --add-dynamic-module=$(MODULESDIR)/http-auth-spnego \
                        --add-dynamic-module=$(MODULESDIR)/http-brotli \
                        --add-dynamic-module=$(MODULESDIR)/http-cache-purge \
			--add-dynamic-module=$(MODULESDIR)/http-captcha \
                        --add-dynamic-module=$(MODULESDIR)/http-concat \
                        --add-dynamic-module=$(MODULESDIR)/http-dav-ext \
                        --add-dynamic-module=$(MODULESDIR)/http-doh \
			--add-dynamic-module=$(MODULESDIR)/http-dynamic-etag \
			--add-dynamic-module=$(MODULESDIR)/http-dynamic-limit-req \
                        --add-dynamic-module=$(MODULESDIR)/http-early_hints \
                        --add-dynamic-module=$(MODULESDIR)/http-echo \
                        --add-dynamic-module=$(MODULESDIR)/http-encrypted-session \
                        --add-dynamic-module=$(MODULESDIR)/http-eval \
                        --add-dynamic-module=$(MODULESDIR)/http-fancyindex \
                        --add-dynamic-module=$(MODULESDIR)/http-flv-live \
                        --add-dynamic-module=$(MODULESDIR)/http-geoip2 \
                        --add-dynamic-module=$(MODULESDIR)/http-headers-more-filter \
                        --add-dynamic-module=$(MODULESDIR)/http-hmac-secure-link \
                        --add-dynamic-module=$(MODULESDIR)/http-immutable \
                        --add-dynamic-module=$(MODULESDIR)/http-ipset-blacklist \
                        --add-dynamic-module=$(MODULESDIR)/http-js-challenge \
			--add-dynamic-module=$(MODULESDIR)/http-length-hiding-filter \
                        --add-dynamic-module=$(MODULESDIR)/http-lua \
                        --add-dynamic-module=$(MODULESDIR)/http-modsecurity \
                        --add-dynamic-module=$(MODULESDIR)/http-naxsi/naxsi_src \
                        --add-dynamic-module=$(MODULESDIR)/http-redis2 \
                        --add-dynamic-module=$(MODULESDIR)/http-security-headers \
                        --add-dynamic-module=$(MODULESDIR)/http-set-misc \
                        --add-dynamic-module=$(MODULESDIR)/http-srcache-filter \
                        --add-dynamic-module=$(MODULESDIR)/http-subs-filter \
                        --add-dynamic-module=$(MODULESDIR)/http-sysguard \
                        --add-dynamic-module=$(MODULESDIR)/http-testcookie-access \
                        --add-dynamic-module=$(MODULESDIR)/http-trim-filter \
                        --add-dynamic-module=$(MODULESDIR)/http-uploadprogress \
                        --add-dynamic-module=$(MODULESDIR)/http-upstream-fair \
                        --add-dynamic-module=$(MODULESDIR)/http-user-agent \
                        --add-dynamic-module=$(MODULESDIR)/http-vhost-traffic-status \
                        --add-dynamic-module=$(MODULESDIR)/http-waf \
                        --add-dynamic-module=$(MODULESDIR)/http-xss-filter \
                        --add-dynamic-module=$(MODULESDIR)/http-zstd \
			--add-dynamic-module=$(MODULESDIR)/ipscrub/ipscrub \
                        --add-dynamic-module=$(MODULESDIR)/nchan \
                        --add-dynamic-module=$(MODULESDIR)/njs/nginx \
                        --add-dynamic-module=$(MODULESDIR)/stream-lua \
                        --add-dynamic-module=$(MODULESDIR)/http-pagespeed 
#                        --add-dynamic-module=$(MODULESDIR)/ssl-ct \


###
# Modules under this configure won't play nice with other modules when compiled at the same time
###
modules1_configure_flags := \
                        $(basic_configure_flags) \
                        --add-dynamic-module=$(MODULESDIR)/rtmp \


%:
	dh $@ --without autoreconf

override_dh_auto_configure: unpack_psol config_patch_modules $(foreach flavour,$(FLAVOURS),config.arch.$(flavour)) config.src

override_dh_auto_build:     $(foreach flavour,$(FLAVOURS),build.arch.$(flavour)) build.src
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

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) || echo

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

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


# functions not in the default debian package:

override_dh_installdocs:

override_dh_strip: $(foreach mod,$(DYN_MODS),strip.mods.$(mod))
	dh_strip --no-automatic-dbgsym

strip.arch.%:
	dh_strip --package=nginx-$(*) --no-automatic-dbgsym

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

config_patch_modules:	$(foreach mod,$(modules_with_patches),config.patch.$(mod))

config.patch.%:
	cd $(MODULESDIR)/$* && QUILT_PATCHES=$(MODULESPATCHDIR)/$* quilt push -a

unpack_psol: 
	tar --overwrite -xf $(CURDIR)/debian/modules/http-pagespeed/psol.tar.xz -C $(CURDIR)/debian/modules/http-pagespeed/ || echo

clean_patch_modules: $(foreach mod,$(modules_with_patches),clean.patch.$(mod))
clean.patch.%:
	if [ -s $(MODULESDIR)/$*/.pc/applied-patches ]; then \
		cd $(MODULESDIR)/$* && QUILT_PATCHES=$(MODULESPATCHDIR)/$* quilt pop -q -a; \
		rm -rf $(MODULESDIR)/$*/.pc; \
	fi

.PHONY: config_patch_modules clean_patch_modules

