#!/usr/bin/make -f

#export DH_VERBOSE=1
CFLAGS := $(shell dpkg-buildflags --get CFLAGS)
LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)
WITH_HTTP2 := $(shell printf "Source: tengine\nBuild-Depends: libssl-dev (>= 1.0.1)\n" | \
	dpkg-checkbuilddeps - >/dev/null 2>&1 && \
	echo "--with-http_v2_module")

%:
	dh $@ --parallel

override_dh_auto_configure: configure_debug

override_dh_strip:
	dh_strip -Xdebug

override_dh_auto_build:
	dh_auto_build
	mv objs/nginx objs/nginx.debug
	./configure \
		--prefix=/etc/nginx \
		--sbin-path=/usr/sbin/nginx \
		--conf-path=/etc/nginx/nginx.conf \
		--error-log-path=/var/log/nginx/error.log \
		--http-log-path=/var/log/nginx/access.log \
		--pid-path=/var/run/nginx.pid \
		--lock-path=/var/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-http_ssl_module \
		--with-http_realip_module \
		--with-http_addition_module \
		--with-http_sub_module \
		--with-http_dav_module \
		--with-http_flv_module \
		--with-http_mp4_module \
		--with-http_gunzip_module \
		--with-http_gzip_static_module \
		--with-http_random_index_module \
		--with-http_secure_link_module \
		--with-http_stub_status_module \
		--with-mail \
		--with-mail_ssl_module \
		--with-file-aio \
		$(WITH_HTTP2) \
		--with-cc-opt="$(CFLAGS)" \
		--with-ld-opt="$(LDFLAGS)" \
		--with-ipv6
	dh_auto_build

configure_debug:
	./configure \
		--prefix=/etc/nginx \
		--sbin-path=/usr/sbin/nginx \
		--conf-path=/etc/nginx/nginx.conf \
		--error-log-path=/var/log/nginx/error.log \
		--http-log-path=/var/log/nginx/access.log \
		--pid-path=/var/run/nginx.pid \
		--lock-path=/var/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-http_ssl_module \
		--with-http_realip_module \
		--with-http_addition_module \
		--with-http_sub_module \
		--with-http_dav_module \
		--with-http_flv_module \
		--with-http_mp4_module \
		--with-http_gunzip_module \
		--with-http_gzip_static_module \
		--with-http_random_index_module \
		--with-http_secure_link_module \
		--with-http_stub_status_module \
		--with-mail \
		--with-mail_ssl_module \
		--with-file-aio \
		$(WITH_HTTP2) \
		--with-cc-opt="$(CFLAGS)" \
		--with-ld-opt="$(LDFLAGS)" \
		--with-ipv6 \
		--with-debug

override_dh_auto_install:
	dh_auto_install
	/usr/bin/install -m 644 debian/nginx.conf debian/tengine/etc/nginx/
	/usr/bin/install -m 644 conf/win-utf debian/tengine/etc/nginx/
	/usr/bin/install -m 644 conf/koi-utf debian/tengine/etc/nginx/
	/usr/bin/install -m 644 conf/koi-win debian/tengine/etc/nginx/
	/usr/bin/install -m 644 conf/mime.types debian/tengine/etc/nginx/
	/usr/bin/install -m 644 conf/scgi_params debian/tengine/etc/nginx/
	/usr/bin/install -m 644 conf/fastcgi_params debian/tengine/etc/nginx/
	/usr/bin/install -m 644 conf/uwsgi_params debian/tengine/etc/nginx/
	/usr/bin/install -m 644 html/index.html debian/tengine/usr/share/nginx/html/
	/usr/bin/install -m 644 html/50x.html debian/tengine/usr/share/nginx/html/
	/usr/bin/install -m 644 debian/nginx.vh.default.conf debian/tengine/etc/nginx/conf.d/default.conf
	/usr/bin/install -m 644 debian/nginx.vh.example_ssl.conf debian/tengine/etc/nginx/conf.d/example_ssl.conf
	/usr/bin/install -m 755 objs/nginx  debian/tengine/usr/sbin/
