Debian related changes to build PLUS packages with OSS packaging scripts
diff --git a/debian/Makefile b/debian/Makefile
index 04f44ad..8820184 100644
--- a/debian/Makefile
+++ b/debian/Makefile
@@ -2,33 +2,40 @@
 
 CONTRIB?=	../contrib
 CONTRIB:=	$(abspath $(CONTRIB))
+DOCS?=		../docs
 
-include $(CONTRIB)/src/nginx/version
+BASE_TARGET?=	oss
+MODULE_TARGET?=	oss
+
+ifeq ($(BASE_TARGET), plus)
+BASE_SUFFIX=-plus
+endif
+
+include $(CONTRIB)/src/nginx$(BASE_SUFFIX)/version
 
 BASE_VERSION=	$(NGINX_VERSION)
 BASE_RELEASE=	1
 PLUS_VERSION=	$(NGINX_PLUS_VERSION)
-BASE_SRC=	nginx-$(BASE_VERSION).tar.gz
-SRCDIR=		nginx-$(BASE_VERSION)
+PLUS_RELEASE=	1
+BASE_SRC=	nginx$(BASE_SUFFIX)-$(BASE_VERSION).tar.gz
+SRCDIR=		nginx$(BASE_SUFFIX)-$(BASE_VERSION)
 SRCPATH?=	../..
-DOCS?=		../docs
 BUILD_ENV_PATH=	${HOME}/debuild
-MODULE_TARGET?=	oss
 
 ifeq ($(MODULE_TARGET), plus)
-MODULE_CONTROL_TEMPLATE=   nginx-plus-module.control.in
-TARGET_SUFFIX=	-plus
+MODULE_SUFFIX=	-plus
 MODULE_SUMMARY_PREFIX=NGINX Plus
 TARGET_VERSION=$(PLUS_VERSION)
-MODULE_PACKAGE_PREFIX=nginx-plus-module
+BASE_ORIG_NAME=nginx-plus_$(PLUS_VERSION).orig.tar.gz
 else
-MODULE_CONTROL_TEMPLATE=   nginx-module.control.in
 MODULE_SUMMARY_PREFIX=nginx
 TARGET_VERSION=$(BASE_VERSION)
-MODULE_PACKAGE_PREFIX=nginx-module
+BASE_ORIG_NAME=nginx_$(BASE_VERSION).orig.tar.gz
 endif
 
+MODULE_PACKAGE_PREFIX=nginx$(MODULE_SUFFIX)-module
 MODULE_TARGET_PREFIX=$(TARGET_VERSION)+
+MODULE_CONTROL_TEMPLATE=	nginx$(MODULE_SUFFIX)-module.control.in
 
 MODULE_PACKAGE_VENDOR=	Sergey Budnevitch <sb@nginx.com>
 MODULE_PACKAGE_URL=	https://nginx.org/
@@ -86,6 +93,15 @@
 	--with-stream_ssl_module \
 	--with-stream_ssl_preread_module
 
+ifeq ($(BASE_TARGET), plus)
+BASE_CONFIGURE_ARGS+= \
+	--build=nginx-plus-r$(PLUS_VERSION) \
+	--with-http_auth_jwt_module \
+	--with-http_f4f_module \
+	--with-http_hls_module \
+	--with-http_session_log_module
+endif
+
 export CR=\\n
 
 modname = $(shell echo $1 | tr '-' '_')
@@ -118,61 +134,114 @@
 	@echo "===> Building base package" ; \
 	$(SHELL) -c "cd debuild-base/$(SRCDIR) ; export DEB_BUILD_OPTIONS=parallel=$(NPROC) ; time -p debuild -us -uc" && \
 	find debuild-base/ -maxdepth 1 -type f -exec cp {} ../../ \; && \
-	ln -s debuild-base/$(SRCDIR)/debian/nginx/usr/sbin base
+	ln -s debuild-base/$(SRCDIR)/debian/nginx$(BASE_SUFFIX)/usr/sbin base
 
 $(BASE_SRC):
-	cd $(CONTRIB) && make .sum-nginx
+	cd $(CONTRIB) && make .sum-nginx$(BASE_SUFFIX)
 	ln -s $(CONTRIB)/tarballs/$(BASE_SRC) $(BASE_SRC)
 
 nginx.deb-changelog:
 	@echo "===> Generating $@"
-	@cd $(DOCS) && DEST=$(CURDIR) VERSIONMASK=1 make $(CURDIR)/nginx.deb-changelog
+	@cd $(DOCS) && DEST=$(CURDIR) VERSIONMASK=1 make $(CURDIR)/nginx$(BASE_SUFFIX).deb-changelog
 
 rules-base: $(BASE_SRC) nginx.deb-changelog
 	@echo "===> Creating $@"
 	@{ \
 	set -e ; \
 	mkdir -p debuild-base ; \
-	cp $(BASE_SRC) debuild-base/nginx_$(BASE_VERSION).orig.tar.gz ; \
-	tar -C debuild-base -xf debuild-base/nginx_$(BASE_VERSION).orig.tar.gz ; \
+	cp $(BASE_SRC) debuild-base/$(BASE_ORIG_NAME); \
+	tar -C debuild-base -xf debuild-base/$(BASE_ORIG_NAME) ; \
 	mkdir -p debuild-base/$(SRCDIR)/debian ; \
 	mkdir -p debuild-base/$(SRCDIR)/debian/source ; \
 	echo "3.0 (quilt)" > debuild-base/$(SRCDIR)/debian/source/format ; \
 	echo "9" > debuild-base/$(SRCDIR)/debian/compat ; \
-	cat nginx.rules.in | sed \
+	for patch in `find $(CONTRIB)/src/nginx$(BASE_SUFFIX)/ -type f -name '*.patch' -printf "%f\n" | sort`; do \
+		mkdir -p debuild-base/$(SRCDIR)/debian/patches ; \
+		echo $$patch >> debuild-base/$(SRCDIR)/debian/patches/series ; \
+		cp -v $(CONTRIB)/src/nginx$(BASE_SUFFIX)/$$patch debuild-base/$(SRCDIR)/debian/patches ; \
+	done ; \
+	cat nginx$(BASE_SUFFIX).rules.in | sed \
 		-e 's#%%CODENAME%%#$(CODENAME)#g' \
 		-e 's#%%BASE_CONFIGURE_ARGS%%#$(BASE_CONFIGURE_ARGS)#g' \
-		-e 's#%%BASE_CONFIGURE_ARGS%%#$(BASE_CONFIGURE_ARGS)#g' \
 		> debuild-base/$(SRCDIR)/debian/rules ; \
-	cat nginx.deb-changelog | sed \
+	cat nginx$(BASE_SUFFIX).deb-changelog | sed \
 		-e 's#%%CODENAME%%#$(CODENAME)#g' \
 		-e 's#%%BASE_VERSION%%#$(BASE_VERSION)#g' \
 		-e 's#%%BASE_RELEASE%%#$(BASE_RELEASE)#g' \
+		-e 's#%%PLUS_VERSION%%#$(PLUS_VERSION)#g' \
+		-e 's#%%PLUS_RELEASE%%#$(PLUS_RELEASE)#g' \
 		> debuild-base/$(SRCDIR)/debian/changelog ; \
-	cat nginx.control.in | sed \
+	cat nginx$(BASE_SUFFIX).control.in | sed \
 		-e 's#%%BASE_VERSION%%#$(BASE_VERSION)#g' \
+		-e 's#%%PLUS_VERSION%%#$(PLUS_VERSION)#g' \
 		> debuild-base/$(SRCDIR)/debian/control ; \
-	cp debuild-base/$(SRCDIR)/CHANGES debuild-base/$(SRCDIR)/CHANGES.ru nginx.* nginx-debug.default nginx-debug.service debuild-base/$(SRCDIR)/debian/ ; \
+	cp nginx$(BASE_SUFFIX).* debuild-base/$(SRCDIR)/debian/ ; \
+	cp nginx$(BASE_SUFFIX).default.conf debuild-base/$(SRCDIR)/debian/default.conf ; \
+	if [ "$(BASE_TARGET)" = "plus" ]; then \
+		cp	$(DOCS)/EULA-TRIAL \
+			$(DOCS)/EULA-DEVELOPER \
+			$(DOCS)/EULA-SUBSCRIPTION \
+			$(DOCS)/CHANGES \
+			$(DOCS)/nginx-modules-reference.pdf \
+			nginx.subscription-check.sh \
+			debuild-base/$(SRCDIR)/debian/ ; \
+		cp -r $(DOCS)/swagger-ui/ debuild-base/$(SRCDIR)/debian/ ; \
+		cp $(CONTRIB)/tarballs/dashboard.html debuild-base/$(SRCDIR)/debian/ ; \
+		mv debuild-base/$(SRCDIR)/debian/nginx-plus.conf debuild-base/$(SRCDIR)/debian/nginx.conf ; \
+		( cd debuild-base/$(SRCDIR) && \
+			find debian/ -type f -exec file --mime {} \; | fgrep 'charset=binary' | cut -d ':' -f 1 > include-binaries && \
+			mv include-binaries debian/source/ \
+		) ; \
+	else \
+		cp	debuild-base/$(SRCDIR)/CHANGES \
+			debuild-base/$(SRCDIR)/CHANGES.ru \
+			nginx-debug.default \
+			nginx-debug.service \
+			debuild-base/$(SRCDIR)/debian/ ; \
+	fi; \
 	cp $(DOCS)/nginx.copyright debuild-base/$(SRCDIR)/debian/copyright ; \
 	chmod +x debuild-base/$(SRCDIR)/debian/rules ; \
 	ln -s debuild-base/$(SRCDIR)/debian/rules rules-base ; \
 	}
 
-module-%: rules-module-%
-	@echo "===> Building nginx-$@ package" ; \
-	$(SHELL) -c "cd debuild-$@/$(SRCDIR) ; export DEB_BUILD_OPTIONS=parallel=$(NPROC) ; time -p debuild -us -uc" && \
-	find debuild-$@/ -maxdepth 1 -type f -exec cp {} ../../ \; && \
-	ln -s debuild-$@/$(SRCDIR)/debian/$(MODULE_PACKAGE_PREFIX)-$*/usr/lib/nginx/modules $@
+module-%: rules-module-% check-compat-%
+	@if [ -e nginx-$@.skip ]; then \
+	    echo "---> Skipping nginx-$@ package (`cat nginx-$@.skip`)" ; \
+	else \
+	    echo "===> Building nginx-$@ package" ; \
+	    $(SHELL) -c "cd debuild-$@/$(SRCDIR) ; export DEB_BUILD_OPTIONS=parallel=$(NPROC) ; time -p debuild -us -uc" && \
+	    find debuild-$@/ -maxdepth 1 -type f -exec cp {} ../../ \; && \
+	    ln -s debuild-$@/$(SRCDIR)/debian/$(MODULE_PACKAGE_PREFIX)-$*/usr/lib/nginx/modules $@ ; \
+	fi;
+
+check-compat-%:
+	@module=`echo $@ | cut -d '-' -f 3- | tr '-' '_'` ; \
+	set -e ; \
+	case "$${module}" in \
+	    brotli) \
+	    if [ "$(CODENAME)" = "stretch" ]; then \
+	        echo "brotli can not be built on Debian 9" > \
+	        nginx-module-$${module}.skip ; \
+	    fi ; \
+	    ;; \
+	    lua) \
+	    if [ "$(ARCH)" = "ppc64le" ]; then \
+	        echo "Lua can not be built on $(ARCH) due to issues with LuaJIT" > \
+	        nginx-module-$${module}.skip ; \
+	    fi ; \
+	    ;; \
+	    modsecurity) \
+	    if [ "$(ARCH)" = "ppc64le" ]; then \
+	        echo "ModSecurity can not be built on $(ARCH)" > \
+	        nginx-module-$${module}.skip ; \
+	    fi ; \
+	    ;; \
+	esac
 
 nginx-module-%.deb-changelog:
 	@echo "===> Generating $@"
 	@cd $(DOCS) && DEST=$(CURDIR) VERSIONMASK=1 make $(CURDIR)/nginx-module-$*.deb-changelog
-	@{ \
-	if [ "$(MODULE_TARGET)" = "plus" ]; then \
-		sed -i -e 's,nginx-module-,nginx-plus-module-,g' $(CURDIR)/nginx-module-$*.deb-changelog ; \
-	fi ; \
-	}
-
+	@sed -i -e 's,nginx-module-,nginx$(MODULE_SUFFIX)-module-,g' $(CURDIR)/$@
 
 rules-module-%: $(BASE_SRC) nginx-module-%.deb-changelog .deps-module-%
 	@echo "===> Creating $@"
@@ -182,13 +251,8 @@
 	module_asis=`echo $@ | cut -d '-' -f 3-` ; \
 	builddir=`echo debuild-module-$${module_asis}` ; \
 	mkdir -p $${builddir} ; \
-	if [ "$(MODULE_TARGET)" = "oss" ]; then \
-		module_version="$(MODULE_VERSION_PREFIX_$(call modname, $*))$(MODULE_VERSION_$(call modname, $*))" ; \
-		orig_name="nginx-module-$${module_asis}_$${module_version}.orig.tar.gz" ; \
-	else \
-		module_version="$(MODULE_VERSION_PREFIX_$(call modname, $*))$(MODULE_VERSION_$(call modname, $*))" ; \
-		orig_name="nginx-plus-module-$${module_asis}_$${module_version}.orig.tar.gz" ; \
-	fi ; \
+	module_version="$(MODULE_VERSION_PREFIX_$(call modname, $*))$(MODULE_VERSION_$(call modname, $*))" ; \
+	orig_name="nginx$(MODULE_SUFFIX)-module-$${module_asis}_$${module_version}.orig.tar.gz" ; \
 	cp $(BASE_SRC) $${builddir}/$${orig_name} ; \
 	tar -C $${builddir} -xf $${builddir}/$${orig_name} ; \
 	mkdir -p $${builddir}/$(SRCDIR)/debian/extra ; \
@@ -250,7 +314,7 @@
 		-e "s#%%CODENAME%%#$(CODENAME)#g" \
 		-e "s#%%BASE_VERSION%%#$(BASE_VERSION)#g" \
 		-e "s#%%BASE_RELEASE%%#$(BASE_RELEASE)#g" \
-		-e "s#%%TARGET_SUFFIX%%#$(TARGET_SUFFIX)#g" \
+		-e "s#%%MODULE_SUFFIX%%#$(MODULE_SUFFIX)#g" \
 		-e "s#%%BASE_CONFIGURE_ARGS%%#$(BASE_CONFIGURE_ARGS)#g" \
 		-e "s#%%MODULE_CONFIGURE_ARGS%%#$(MODULE_CONFARGS_$(call modname, $*))#g" \
 		-e "s#%%MODULE_DEFINITIONS%%#$${definitions}#g" \
@@ -264,7 +328,7 @@
 	> $${builddir}/$(SRCDIR)/debian/rules ; \
 	cat nginx-module.postinst.in | sed \
 		-e "s#%%MODULE_POST%%#$$post#g" \
-	> $${builddir}/$(SRCDIR)/debian/nginx$(TARGET_SUFFIX)-module-$${module_asis}.postinst ; \
+	> $${builddir}/$(SRCDIR)/debian/nginx$(MODULE_SUFFIX)-module-$${module_asis}.postinst ; \
 	chmod +x $${builddir}/$(SRCDIR)/debian/rules ; \
 	ln -s $${builddir}/$(SRCDIR)/debian/rules $@ ; \
 	}
@@ -293,17 +357,17 @@
 	for so in `find module-*/ -maxdepth 1 -type f -name "*module.so" | sort -t / -k 2`; do \
 		globals="$$globals load_module $$pwd/$$so;" ; \
 	done ; \
-	if [ -d $$pwd/debuild-module-perl ]; then \
-		globals="$$globals env PERL5LIB=$$pwd/debuild-module-perl/$(SRCDIR)/debian/build-nginx/objs/src/http/modules/perl:$$pwd/debuild-module-perl/nginx-$(BASE_VERSION)/debian/build-nginx/objs/src/http/modules/perl/blib/arch;" ; \
+	if [ -d $$pwd/module-perl ]; then \
+		globals="$$globals env PERL5LIB=$$pwd/debuild-module-perl/$(SRCDIR)/debian/build-nginx/objs/src/http/modules/perl:$$pwd/debuild-module-perl/$(SRCDIR)/debian/build-nginx/objs/src/http/modules/perl/blib/arch;" ; \
 	fi ; \
-	if [ -d $$pwd/debuild-module-modsecurity ]; then \
+	if [ -d $$pwd/module-modsecurity ]; then \
 		export LD_LIBRARY_PATH=$$pwd/debuild-module-modsecurity/$(SRCDIR)/debian/build-nginx/extra/modsecurity-v$(MODSECURITY_VERSION)/local/lib ; \
 	fi ; \
-	if [ -d $$pwd/debuild-module-opentracing ]; then \
+	if [ -d $$pwd/module-opentracing ]; then \
 		export LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$$pwd/debuild-module-opentracing/$(SRCDIR)/debian/build-nginx/extra/opentracing-cpp-$(OPENTRACING_CPP_VERSION)/usr/local/lib ; \
 	fi ; \
-	if [ -d $$pwd/debuild-module-lua ]; then \
-		globals_http="lua_package_path '$$pwd/debuild-module-lua/nginx-$(BASE_VERSION)/debian/extra/lua-resty-core-$(LUA_RESTY_CORE_VERSION)/lib/?.lua;$$pwd/debuild-module-lua/nginx-$(BASE_VERSION)/debian/extra/lua-resty-lrucache-$(LUA_RESTY_LRUCACHE_VERSION)/lib/?.lua;;';" ; \
+	if [ -d $$pwd/module-lua ]; then \
+		globals_http="lua_package_path '$$pwd/debuild-module-lua/$(SRCDIR)/debian/extra/lua-resty-core-$(LUA_RESTY_CORE_VERSION)/lib/?.lua;$$pwd/debuild-module-lua/$(SRCDIR)/debian/extra/lua-resty-lrucache-$(LUA_RESTY_LRUCACHE_VERSION)/lib/?.lua;;';" ; \
 		globals_stream="$$globals_http" ; \
 	fi ; \
 	cd nginx-tests && TEST_NGINX_BINARY=$$pwd/base/nginx TEST_NGINX_GLOBALS="$$globals" TEST_NGINX_GLOBALS_HTTP="$$globals_http" TEST_NGINX_GLOBALS_STREAM="$$globals_stream" prove $$PROVE_ARGS . ; \
@@ -319,17 +383,17 @@
 	for so in `find module-*/ -maxdepth 1 -type f -name "*module-debug.so" | sort -t / -k 2`; do \
 		globals="$$globals load_module $$pwd/$$so;" ; \
 	done ; \
-	if [ -d $$pwd/debuild-module-perl ]; then \
-		globals="$$globals env PERL5LIB=$$pwd/debuild-module-perl/$(SRCDIR)/debian/build-nginx-debug/objs/src/http/modules/perl:$$pwd/debuild-module-perl/nginx-$(BASE_VERSION)/debian/build-nginx/objs/src/http/modules/perl/blib/arch;" ; \
+	if [ -d $$pwd/module-perl ]; then \
+		globals="$$globals env PERL5LIB=$$pwd/debuild-module-perl/$(SRCDIR)/debian/build-nginx-debug/objs/src/http/modules/perl:$$pwd/debuild-module-perl/$(SRCDIR)/debian/build-nginx/objs/src/http/modules/perl/blib/arch;" ; \
 	fi ; \
-	if [ -d $$pwd/debuild-module-modsecurity ]; then \
+	if [ -d $$pwd/module-modsecurity ]; then \
 		export LD_LIBRARY_PATH=$$pwd/debuild-module-modsecurity/$(SRCDIR)/debian/build-nginx-debug/extra/modsecurity-v$(MODSECURITY_VERSION)/local/lib ; \
 	fi ; \
-	if [ -d $$pwd/debuild-module-opentracing ]; then \
+	if [ -d $$pwd/module-opentracing ]; then \
 		export LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$$pwd/debuild-module-opentracing/$(SRCDIR)/debian/build-nginx-debug/extra/opentracing-cpp-$(OPENTRACING_CPP_VERSION)/usr/local/lib ; \
 	fi ; \
-	if [ -d $$pwd/debuild-module-lua ]; then \
-		globals_http="lua_package_path '$$pwd/debuild-module-lua/nginx-$(BASE_VERSION)/debian/extra/lua-resty-core-$(LUA_RESTY_CORE_VERSION)/lib/?.lua;$$pwd/debuild-module-lua/nginx-$(BASE_VERSION)/debian/extra/lua-resty-lrucache-$(LUA_RESTY_LRUCACHE_VERSION)/lib/?.lua;;';" ; \
+	if [ -d $$pwd/module-lua ]; then \
+		globals_http="lua_package_path '$$pwd/debuild-module-lua/$(SRCDIR)/debian/extra/lua-resty-core-$(LUA_RESTY_CORE_VERSION)/lib/?.lua;$$pwd/debuild-module-lua/$(SRCDIR)/debian/extra/lua-resty-lrucache-$(LUA_RESTY_LRUCACHE_VERSION)/lib/?.lua;;';" ; \
 		globals_stream="$$globals_http" ; \
 	fi ; \
 	cd nginx-tests && TEST_NGINX_BINARY=$$pwd/base/nginx-debug TEST_NGINX_GLOBALS="$$globals" TEST_NGINX_GLOBALS_HTTP="$$globals_http" TEST_NGINX_GLOBALS_STREAM="$$globals_stream" prove $$PROVE_ARGS . ; \
diff --git a/debian/Makefile.module-njs b/debian/Makefile.module-njs
index 2e1109d..50fdaaa 100644
--- a/debian/Makefile.module-njs
+++ b/debian/Makefile.module-njs
@@ -25,8 +25,8 @@
 export MODULE_PREBUILD_njs
 
 define MODULE_PREINSTALL_njs
-	mkdir -p $$(INSTALLDIR)/usr/share/doc/nginx-module-njs
-	install -m644 $$(BUILDDIR_nginx)/extra/njs-$(NJS_VERSION)/CHANGES $$(INSTALLDIR)/usr/share/doc/nginx-module-njs/
+	mkdir -p $$(INSTALLDIR)/usr/share/doc/nginx$(MODULE_SUFFIX)-module-njs
+	install -m644 $$(BUILDDIR_nginx)/extra/njs-$(NJS_VERSION)/CHANGES $$(INSTALLDIR)/usr/share/doc/nginx$(MODULE_SUFFIX)-module-njs/
 	mkdir -p $$(INSTALLDIR)/usr/bin
 	install -m755 $$(CURDIR)/debian/extra/njs-$(NJS_VERSION)/build/njs $$(INSTALLDIR)/usr/bin/
 endef
diff --git a/debian/Makefile.module-opentracing b/debian/Makefile.module-opentracing
index 2a2bf2e..720889a 100644
--- a/debian/Makefile.module-opentracing
+++ b/debian/Makefile.module-opentracing
@@ -72,6 +72,7 @@
 		OpenTracing_DIR=$$(BUILDDIR_nginx)/extra/opentracing-cpp-$(OPENTRACING_CPP_VERSION)/usr/local/lib/cmake/OpenTracing \
 		thrift_DIR=$$(BUILDDIR_nginx)/extra/thrift-$(THRIFT_VERSION)/usr/local/lib/cmake/thrift \
 		cmake \
+			-DJAEGERTRACING_BUILD_EXAMPLES=OFF \
 			-DBUILD_TESTING=OFF \
 			-DJAEGERTRACING_PLUGIN=ON \
 			-DCMAKE_BUILD_TYPE=Release \
diff --git a/debian/nginx-module.rules.in b/debian/nginx-module.rules.in
index e7c8c42..05908bb 100755
--- a/debian/nginx-module.rules.in
+++ b/debian/nginx-module.rules.in
@@ -8,9 +8,15 @@
 
 BUILDDIR_nginx = $(CURDIR)/debian/build-nginx
 BUILDDIR_nginx_debug = $(CURDIR)/debian/build-nginx-debug
-INSTALLDIR = $(CURDIR)/debian/nginx%%TARGET_SUFFIX%%-module-%%MODULE%%
+INSTALLDIR = $(CURDIR)/debian/nginx%%MODULE_SUFFIX%%-module-%%MODULE%%
 BASEDIR = $(CURDIR)
 
+ifeq (%%MODULE_CONFIGURE_ARGS%%,"")
+META_MODULE=1
+else
+META_MODULE=0
+endif
+
 ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
 	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
 	ifeq (${NUMJOBS}, 0)
@@ -27,9 +33,11 @@
 	mkdir -p $(BUILDDIR_$*)
 	cp -Pa $(CURDIR)/auto $(BUILDDIR_$*)/
 	cp -Pa $(CURDIR)/conf $(BUILDDIR_$*)/
+	if ! test -e $(CURDIR)/configure ; then ln -s $(CURDIR)/auto/configure $(CURDIR)/configure ; fi
 	cp -Pa $(CURDIR)/configure $(BUILDDIR_$*)/
 	cp -Pa $(CURDIR)/contrib $(BUILDDIR_$*)/
-	cp -Pa $(CURDIR)/man $(BUILDDIR_$*)/
+	if test -e $(CURDIR)/man ; then  cp -Pa $(CURDIR)/man $(BUILDDIR_$*)/ ; fi
+	if test -e $(CURDIR)/docs ; then cp -Pa $(CURDIR)/docs $(BUILDDIR_$*)/ ; fi
 	cp -Pa $(CURDIR)/src $(BUILDDIR_$*)/
 	test -d $(CURDIR)/debian/extra && cp -Pa $(CURDIR)/debian/extra $(BUILDDIR_$*)/
 	touch $@
@@ -45,19 +53,25 @@
 	touch $@
 
 config.status.nginx: config.env.nginx config.pre.nginx
+ifeq ($(META_MODULE), 0)
 	cd $(BUILDDIR_nginx) && \
 	CFLAGS="" %%MODULE_ENV%% ./configure %%BASE_CONFIGURE_ARGS%% %%MODULE_CONFIGURE_ARGS%% --with-cc-opt="$(CFLAGS) %%MODULE_CC_OPT%%" --with-ld-opt="$(LDFLAGS) %%MODULE_LD_OPT%%"
+endif
 	touch $@
 
 config.status.nginx_debug: config.env.nginx_debug config.pre.nginx_debug
+ifeq ($(META_MODULE), 0)
 	cd $(BUILDDIR_nginx_debug) && \
 	CFLAGS="" %%MODULE_ENV%% ./configure %%BASE_CONFIGURE_ARGS%% %%MODULE_CONFIGURE_ARGS%% --with-cc-opt="$(CFLAGS) %%MODULE_CC_OPT_DEBUG%%" --with-ld-opt="$(LDFLAGS) %%MODULE_LD_OPT_DEBUG%%" --with-debug
+endif
 	touch $@
 
 build-arch.%: config.status.%
 	dh_testdir
 	dh_prep
+ifeq ($(META_MODULE), 0)
 	$(MAKE) -j$(NUMJOBS) -C $(BUILDDIR_$*) modules
+endif
 
 build-arch: build-arch.nginx build-arch.nginx_debug
 	dh_testdir
@@ -65,7 +79,9 @@
 
 build-dbg.%: install
 	dh_testdir
-	dh_strip --dbg-package=nginx%%TARGET_SUFFIX%%-module-%%MODULE%%-dbg
+ifeq ($(META_MODULE), 0)
+	dh_strip --dbg-package=nginx%%MODULE_SUFFIX%%-module-%%MODULE%%-dbg
+endif
 
 build-dbg: build-dbg.nginx
 	dh_testdir
@@ -87,10 +103,12 @@
 	find $(CURDIR) -maxdepth 1 -size 0 -delete
 
 post-build:
+ifeq ($(META_MODULE), 0)
 	for so in `find $(BUILDDIR_nginx_debug)/objs/ -maxdepth 1 -type f -name "*.so"`; do \
 	    debugso=`echo $${so} | sed -e "s|.so|-debug.so|"` ; \
 	    mv $${so} $${debugso} ; \
 	done
+endif
 
 install:
 	dh_testdir
@@ -99,6 +117,7 @@
 	dh_installdirs
 	dh_install
 %%MODULE_PREINSTALL%%
+ifeq ($(META_MODULE), 0)
 	mkdir -p $(INSTALLDIR)/usr/lib/nginx/modules
 	for so in `find $(BUILDDIR_nginx_debug)/objs/ -maxdepth 1 -type f -name "*.so"`; do \
 	    install -m755 $${so} $(INSTALLDIR)/usr/lib/nginx/modules/ ; \
@@ -106,6 +125,7 @@
 	for so in `find $(BUILDDIR_nginx)/objs/ -maxdepth 1 -type f -name "*.so"`; do \
 	    install -m755 $${so} $(INSTALLDIR)/usr/lib/nginx/modules/ ; \
 	done
+endif
 
 binary-indep: build post-build install
 
@@ -120,7 +140,9 @@
 	dh_perl -a
 	dh_fixperms -a
 	dh_installdeb -a
+ifeq ($(META_MODULE), 0)
 	dh_shlibdeps -a
+endif
 	dh_gencontrol -a
 	dh_md5sums -a
 	dh_builddeb -a
diff --git a/debian/nginx.vh.default.conf b/debian/nginx.default.conf
similarity index 100%
rename from debian/nginx.vh.default.conf
rename to debian/nginx.default.conf
diff --git a/debian/nginx.rules.in b/debian/nginx.rules.in
index 16f6737..7d963c3 100755
--- a/debian/nginx.rules.in
+++ b/debian/nginx.rules.in
@@ -94,7 +94,7 @@
 	mkdir -p $(INSTALLDIR)/usr/lib/nginx/modules
 	mkdir -p $(INSTALLDIR)/usr/share/doc/nginx
 	install -m 644 debian/CHANGES $(INSTALLDIR)/usr/share/doc/nginx/changelog
-	install -m 644 debian/nginx.vh.default.conf $(INSTALLDIR)/etc/nginx/conf.d/default.conf
+	install -m 644 debian/nginx.default.conf $(INSTALLDIR)/etc/nginx/conf.d/default.conf
 	ln -s /usr/lib/nginx/modules $(INSTALLDIR)/etc/nginx/modules
 
 binary-indep: build post-build install