#!/usr/bin/make -f

# NOTE: debian/rules build (which runs tests) must be run outside of fakeroot
# since the tests open sockets, which fakeroot stubs out. In other words, you
# must run `debian/rules build` separately from `fakeroot debian/rules binary`.
# (The standard build tools such as dpkg-buildpackage already do this.)

%:
	dh $@ --with autoreconf,python2

DH_common = -S python_distutils
DH_client = -S makefile -- -f Makefile.client prefix=/usr sysconfdir=/etc
DH_facilitator = -S autoconf -D facilitator --
DH_proxy = -S makefile -D proxy --
DH_nodeproxy = -S makefile -D proxy/modules/nodejs --

# TODO(infinity0): handle setup-common.py better; currently we symlink it as a
# hack. Ideally, we would fix dh_python to support custom setup.py filenames.

override_dh_install:
	dh_install -XChangeLog -XLICENSE -XMakefile

override_dh_fixperms:
	dh_fixperms
	chmod 600 debian/flashproxy-facilitator/etc/flashproxy/reg-email.pass

override_dh_auto_configure:
	ln -sf setup-common.py setup.py
	dh_auto_configure $(DH_facilitator) --enable-initscripts cgibindir=/usr/lib/cgi-bin

override_dh_auto_build:
	dh_auto_build $(DH_common)
	dh_auto_build $(DH_client)
	dh_auto_build $(DH_facilitator) PYTHONPATH=..
	dh_auto_build $(DH_nodeproxy)

override_dh_auto_test:
	dh_auto_test $(DH_common)
	dh_auto_test $(DH_client)
	dh_auto_test $(DH_facilitator) PYTHONPATH=.. VERBOSE=1
	dh_auto_test $(DH_proxy)

override_dh_auto_install:
	dh_auto_install $(DH_common)
	dh_auto_install $(DH_client)
	dh_auto_install $(DH_facilitator)
	for name in fp-facilitator fp-reg-decryptd fp-registrar-email; do \
	  dh_installinit -p flashproxy-facilitator -o --name=$${name}; \
	done

override_dh_auto_clean:
	ln -sf setup-common.py setup.py
	dh_auto_clean $(DH_common)
	rm -rf flashproxy_common.egg-info
	dh_auto_clean $(DH_client)
	dh_auto_clean $(DH_facilitator)
	dh_auto_clean $(DH_nodeproxy)
	rm -f setup.py

e2e-check:
	dpkg -s libparse-debcontrol-perl >/dev/null
	CHANGES="../flashproxy_$$(dpkg-parsechangelog -SVersion)_$$(dpkg --print-architecture).changes"; \
	if [ -f "$$CHANGES" ]; then \
		debian/flashproxy-end-to-end-test.sh "$$CHANGES"; \
	else \
		echo >&2 "did you built the package yet? file not found: $$CHANGES"; \
		false; \
	fi
