#!/usr/bin/make -f

export GEM2DEB_TEST_RUNNER = --check-dependencies

%:
	dh $@ --buildsystem=ruby --with ruby


override_dh_auto_install:
# The tests are performed here, and during them it's necessary to
# start rabbitmq-server with a customized configuration file (see
# spec/config/rabbitmq.conf).  However, when we install
# rabbitmq-server its systemd service is automatically started (except
# when inside a chroot), which means that we have to start *another*
# rabbitmq-server instance listening at a different port.
#
# For the reason above, it's necessary to adjust all of the test files
# inside the spec/ directory and make them explicitly connect to our
# rabbitmq-server instance.  This is what we do here, before we
# actually invoke the tests.
	grep 'Bunny\.new' spec/ -rl \
		| sort -u \
		| xargs sed -i 's@Bunny\.new(@Bunny.new(host: "127.0.0.1", port: 16688, @'
# Adjust the ports that the rabbitmq-server will listen to.
	sed -i \
		-e 's@^listeners\.tcp\.1 = .*@listeners.tcp.1 = 127.0.0.1:16688@' \
		-e 's@^listeners\.ssl\.default = .*@listeners.ssl.default = 16689@' \
		spec/config/rabbitmq.conf
# Adjust d/ruby-tests.rake to reflect the change of ports.
	sed -i \
		-e "s@^rabbitmq_env = {@rabbitmq_env = { \
  'RABBITMQ_NODE_PORT' => '16688', \
  'BUNNY_RABBITMQ_HOSTNAME' => '127.0.0.1:16688', \
  'RABBITMQ_PORT' => '16688', \
  'RABBITMQ_URL' => 'amqp://127.0.0.1:16688/bunny_testbed', \
  'ERL_EPMD_PORT' => '62222', \
@" debian/ruby-tests.rake

	dh_auto_install -O--buildsystem=ruby
	rm -rf debian/ruby-bunny/usr/bin
