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

<% if ['1', 'true', 'on', 'yes'].include?(ENV['USE_CCACHE']) -%>
export USE_CCACHE=1
NGINX_CONFIGURE_OPTIONS = CC=/usr/lib/ccache/cc CXX=/usr/lib/ccache/c++
<% else -%>
NGINX_CONFIGURE_OPTIONS =
<% end -%>

# Speed up ccache (reduce I/O) by lightly compressing things.
# Always set these variables because pbuilder uses ccache transparently.
CCACHE_COMPRESS=1
export CCACHE_COMPRESS
CCACHE_COMPRESS_LEVEL=3
export CCACHE_COMPRESS_LEVEL

%:
	dh $@

override_dh_auto_configure:
	# Do nothing

override_dh_auto_build:
<% if is_distribution?("<= saucy") || is_distribution?("<= wheezy") -%>
	/usr/bin/ruby1.8 /usr/bin/rake fakeroot
	mv pkg/fakeroot pkg/fakeroot1.8
	/usr/bin/ruby1.9.1 /usr/bin/rake fakeroot
	mv pkg/fakeroot pkg/fakeroot1.9.1
<% else -%>
	/usr/bin/ruby1.9.1 /usr/bin/rake fakeroot
	mv pkg/fakeroot pkg/fakeroot1.9.1
	/usr/bin/ruby2.0 /usr/bin/rake fakeroot
	mv pkg/fakeroot pkg/fakeroot2.0
<% end -%>
	cd nginx-<%= PhusionPassenger::PREFERRED_NGINX_VERSION %> && \
		env $(NGINX_CONFIGURE_OPTIONS) ./configure --prefix=/tmp \
		<%= PhusionPassenger::STANDALONE_NGINX_CONFIGURE_OPTIONS %> \
		--add-module=`pwd`/../ext/nginx && \
		make && \
		mv objs/nginx objs/nginx-<%= PhusionPassenger::PREFERRED_NGINX_VERSION %>

override_dh_auto_install:
	mkdir debian/tmp/
	# Merge Ruby 1.8, 1.9 and 2.0 files into a single directory.
<% if is_distribution?("<= saucy") || is_distribution?("<= wheezy") -%>
	cp -a pkg/fakeroot1.8/* debian/tmp/
	cp -a pkg/fakeroot1.9.1/* debian/tmp/
<% else -%>
	cp -a pkg/fakeroot1.9.1/* debian/tmp/
	cp -a pkg/fakeroot2.0/* debian/tmp/
<% end -%>
	./dev/install_scripts_bootstrap_code.rb --ruby /usr/lib/ruby/vendor_ruby debian/tmp/usr/bin/* debian/tmp/usr/sbin/*
	./dev/install_scripts_bootstrap_code.rb --nginx-module-config /usr/bin debian/tmp/usr/share/<%= PhusionPassenger::GLOBAL_NAMESPACE_DIRNAME %>/ngx_http_passenger_module/config
	touch debian/tmp/usr/share/<%= PhusionPassenger::GLOBAL_NAMESPACE_DIRNAME %>/release.txt

override_dh_auto_clean:
	/usr/bin/rake clean CLEAN_DOCS=false
	rm -rf pkg/fakeroot1.8
	rm -rf pkg/fakeroot1.9.1
	rm -rf pkg/fakeroot2.0
	cd nginx-<%= PhusionPassenger::PREFERRED_NGINX_VERSION %> && if test -f Makefile; then make clean; fi
	# Hack to prevent HTML files from being renegerated
	touch doc/*.html

# Because we include Ruby extensions, the Debian package depends on libruby.
# Since Phusion Passenger works fine without the Ruby extensions, we don't want
# to depend on libruby.
# https://github.com/phusion/passenger_apt_automation/issues/3
override_dh_shlibdeps:
	dh_shlibdeps
	sed -i -E 's/libruby.*?, //g' debian/*.substvars
