#!/usr/bin/make -f
# -*- makefile -*-

# We want to run the tests and they run fine,
# except they fail very randomly. :(  TODO, test to see if this is still needed.
#export DH_RUBY_IGNORE_TESTS=all

%:
	dh $@ --with ruby

override_dh_auto_clean:
	# tests
	# db/schema.rb is being removed again as this
	# remains if build time tests fail  TODO, test to see if this is still needed.
	# Also, test to see if this could become an execute_after.
	rm -rf instances Gemfile.local tmp Gemfile.lock db/schema.rb
	dh_auto_clean

# TODO, test to see if this is still needed.
override_dh_auto_configure:
	./debian/check-locales
	bundle install --local --quiet
	rm -f Gemfile.lock

# TODO, test to see if this is still the best way to handle this.
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	mkdir -p instances/default/config/
	cp debian/Gemfile.test.local Gemfile.local
	cp debian/database.test.yml instances/default/config/database.yml
	bin/rake db:migrate RAILS_ENV=test
	bin/rake test RAILS_ENV=test
	rm db/schema.rb
endif

execute_after_dh_install:
	# Clean up the "extra" license files with typos :)  TODO, test to see if it is still needed.
	find debian/redmine/usr/share/redmine -name "*LICEN*E*" -exec rm -f '{}' \;
	# ...and other various files.  TODO, test to see if it is still needed.
	find debian/redmine -name '.gitignore' -type f | xargs rm -f

	# Remove example config files
	rm debian/redmine/usr/share/redmine/config/database.yml.example

	# Remove upstream changelog generator script
	rm debian/redmine/usr/share/redmine/bin/changelog.rb

	# replace config/email.yml by /etc/redmine/<instance>/email.yml in all locales (Closes: #590846).  TODO, test to see if it is still needed.
	sed -i -e 's/config\/configuration\.yml/\/etc\/redmine\/\&lt\;instance\&gt\;\/configuration\.yml/g' debian/redmine/usr/share/redmine/config/locales/*.yml

	# remove shebang from Rakefile.  TODO, test to see if it is still needed.
	sed -i -e '1 { /^#!/ d }' debian/redmine/usr/share/redmine/Rakefile

execute_after_dh_fixperms:
	# fix permissions.  TODO, test to see if it is stil needed.
	chmod +x debian/redmine/usr/share/redmine/extra/mail_handler/rdm-mailhandler.rb
	chmod -x debian/redmine/usr/share/redmine/app/models/mail_handler.rb
	chmod -x debian/redmine/usr/share/redmine/Rakefile

	# FIXME make redmine-instances executable. This can be dropped when the patch is accepted upstream
	chmod +x debian/redmine/usr/share/redmine/bin/redmine-instances
