#!/usr/bin/make -f

#export DH_VERBOSE=1
include /usr/share/dpkg/vendor.mk
ifeq ($(shell $(call dpkg_vendor_derives_from,ubuntu)),yes)
DISTROREL_MAJOR := $(shell lsb_release -rs | cut -d. -f 1)
ifeq ($(shell test $(DISTROREL_MAJOR) -ge 24; echo $$?), 0)
export DEB_CFLAGS_MAINT_APPEND=-fPIC
else
export DEB_CFLAGS_MAINT_APPEND=-Wp,-D_FORTIFY_SOURCE=2 -fPIC
endif
else
export DEB_CFLAGS_MAINT_APPEND=-Wp,-D_FORTIFY_SOURCE=2 -fPIC
endif
export DEB_BUILD_MAINT_OPTIONS=hardening=+all
export DEB_LDFLAGS_MAINT_APPEND=-Wl,--as-needed
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

BUILDDIR_nginx = $(CURDIR)/debian/build-nginx
BUILDDIR_nginx_debug = $(CURDIR)/debian/build-nginx-debug
INSTALLDIR = $(CURDIR)/debian/nginx-module-otel
BASEDIR = $(CURDIR)

ifeq (--add-dynamic-module=extra/nginx-otel-72d8eed53af4c2cd6f3e30a2efe0e38d66f5e176/,"")
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)
		NUMJOBS = 1
	endif
else
	NUMJOBS = 1
endif



config.env.%:
	dh_testdir
	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_$*)/
	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_$*)/
	if test -d $(CURDIR)/debian/extra ; then  cp -Pa $(CURDIR)/debian/extra $(BUILDDIR_$*)/ ; fi
	touch $@

config.pre.nginx:
	dh_testdir
	set -ex && export PATH=$builddir/prebuilt/bin/:$$PATH && export CMAKE_PREFIX_PATH=$builddir/prebuilt/ && cd $(BUILDDIR_nginx)/extra/abseil-cpp-20211102.0 && mkdir build && cd build && cmake -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_EXTENSIONS=OFF -DCMAKE_CXX_VISIBILITY_PRESET=hidden -DCMAKE_POLICY_DEFAULT_CMP0063=NEW -DCMAKE_PREFIX_PATH=$(BUILDDIR_nginx)/extra/prebuilt/ -DCMAKE_INSTALL_PREFIX:STRING=$(BUILDDIR_nginx)/extra/prebuilt/ -DCMAKE_INSTALL_LIBDIR:STRING=lib -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_TESTING=OFF -DWITH_BENCHMARK=OFF -DCMAKE_BUILD_TYPE=RelWithDebInfo ../ && make -j$(NUMJOBS) install && cd $(BUILDDIR_nginx)/extra/protobuf-3.19.5 && mkdir build && cd build && cmake -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_EXTENSIONS=OFF -DCMAKE_CXX_VISIBILITY_PRESET=hidden -DCMAKE_POLICY_DEFAULT_CMP0063=NEW -DCMAKE_PREFIX_PATH=$(BUILDDIR_nginx)/extra/prebuilt/ -DCMAKE_INSTALL_PREFIX:STRING=$(BUILDDIR_nginx)/extra/prebuilt/ -DCMAKE_INSTALL_LIBDIR:STRING=lib -DCMAKE_POSITION_INDEPENDENT_CODE=ON -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ../cmake/ && make -j$(NUMJOBS) install && cd $(BUILDDIR_nginx)/extra/grpc-1.46.7 && mkdir build && cd build && CXXFLAGS='-Wno-error=format-security -DGRPC_NO_XDS -DGRPC_NO_RLS' cmake -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_EXTENSIONS=OFF -DCMAKE_CXX_VISIBILITY_PRESET=hidden -DCMAKE_POLICY_DEFAULT_CMP0063=NEW -DCMAKE_PREFIX_PATH=$(BUILDDIR_nginx)/extra/prebuilt/ -DCMAKE_INSTALL_PREFIX:STRING=$(BUILDDIR_nginx)/extra/prebuilt/ -DCMAKE_INSTALL_LIBDIR:STRING=lib -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DgRPC_BUILD_GRPC_RUBY_PLUGIN=OFF -DgRPC_BUILD_GRPC_PYTHON_PLUGIN=OFF -DgRPC_BUILD_GRPC_PHP_PLUGIN=OFF -DgRPC_BUILD_GRPC_OBJECTIVE_C_PLUGIN=OFF -DgRPC_BUILD_GRPC_NODE_PLUGIN=OFF -DgRPC_BUILD_GRPC_CSHARP_PLUGIN=OFF -DgRPC_BUILD_CSHARP_EXT=OFF -DgRPC_BUILD_CODEGEN=ON -DgRPC_SSL_PROVIDER=package -DgRPC_ZLIB_PROVIDER=package -DgRPC_CARES_PROVIDER=package -DgRPC_ABSL_PROVIDER=package -DgRPC_PROTOBUF_PROVIDER=package -DgRPC_PROTOBUF_PACKAGE_TYPE=CONFIG -DgRPC_USE_PROTO_LITE=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo ../ && make -j$(NUMJOBS) install && cd $(BUILDDIR_nginx)/extra/opentelemetry-cpp-1.26.0 && mkdir build && cd build && cmake -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_EXTENSIONS=OFF -DCMAKE_CXX_VISIBILITY_PRESET=hidden -DCMAKE_POLICY_DEFAULT_CMP0063=NEW -DCMAKE_PREFIX_PATH=$(BUILDDIR_nginx)/extra/prebuilt/ -DCMAKE_INSTALL_PREFIX:STRING=$(BUILDDIR_nginx)/extra/prebuilt/ -DCMAKE_INSTALL_LIBDIR:STRING=lib -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_TESTING=OFF -DWITH_BENCHMARK=OFF -DWITH_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=RelWithDebInfo ../ && $(MAKE) -j$(NUMJOBS) install || exit 1
	touch $@

config.pre.nginx_debug: config.pre.nginx
	dh_testdir
	rm -rf $(BUILDDIR_nginx_debug)/extra && cp -Pa $(BUILDDIR_nginx)/extra $(BUILDDIR_nginx_debug)/
	touch $@

config.status.nginx: config.env.nginx config.pre.nginx
ifeq ($(META_MODULE), 0)
	cd $(BUILDDIR_nginx) && \
	CFLAGS="" PATH=$(BUILDDIR_nginx)/extra/prebuilt/bin/:$$PATH NGX_OTEL_PROTO_DIR=$(BUILDDIR_nginx)/extra/opentelemetry-proto-1.10.0 CMAKE_PREFIX_PATH=$(BUILDDIR_nginx)/extra/prebuilt/ NGX_OTEL_CMAKE_OPTS="-DNGX_OTEL_GRPC=package -DNGX_OTEL_SDK=package -DNGX_OTEL_PROTO_DIR=$$NGX_OTEL_PROTO_DIR" ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/run/nginx.pid --lock-path=/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_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_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_v3_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-dynamic-module=extra/nginx-otel-72d8eed53af4c2cd6f3e30a2efe0e38d66f5e176/ --with-cc-opt="$(CFLAGS) " --with-ld-opt="$(LDFLAGS) "
endif
	touch $@

config.status.nginx_debug: config.env.nginx_debug config.pre.nginx_debug
ifeq ($(META_MODULE), 0)
	cd $(BUILDDIR_nginx_debug) && \
	CFLAGS="" PATH=$(BUILDDIR_nginx)/extra/prebuilt/bin/:$$PATH NGX_OTEL_PROTO_DIR=$(BUILDDIR_nginx)/extra/opentelemetry-proto-1.10.0 CMAKE_PREFIX_PATH=$(BUILDDIR_nginx)/extra/prebuilt/ NGX_OTEL_CMAKE_OPTS="-DNGX_OTEL_GRPC=package -DNGX_OTEL_SDK=package -DNGX_OTEL_PROTO_DIR=$$NGX_OTEL_PROTO_DIR" ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/run/nginx.pid --lock-path=/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_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_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_v3_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-dynamic-module=extra/nginx-otel-72d8eed53af4c2cd6f3e30a2efe0e38d66f5e176/ --with-cc-opt="$(CFLAGS) " --with-ld-opt="$(LDFLAGS) " --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
	touch $@

build-dbg.%: install
	dh_testdir
ifeq ($(META_MODULE), 0)
	dh_strip --dbg-package=nginx-module-otel-dbg
endif

build-dbg: build-dbg.nginx
	dh_testdir
	touch $@

build-indep:
	dh_testdir
	touch $@

build: build-arch build-indep
	dh_testdir
	touch $@

clean:
	dh_testdir
	dh_testroot
	dh_clean
	rm -rf $(CURDIR)/debian/build-*
	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
	dh_testroot
	dh_prep
	dh_installdirs
	dh_install

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/ ; \
	done
	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

binary-arch: install build-dbg
	dh_testdir
	dh_testroot
	dh_installchangelogs -a
	dh_installdocs -a
	dh_lintian -a
	dh_link -aA
	dh_compress -a
	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

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary install
