#!/usr/bin/make -f
#export DH_VERBOSE=1
#
# Uncomment to ignore all test failures (but the tests will run anyway)
#export DH_RUBY_IGNORE_TESTS=all
#
# Uncomment to ignore some test failures (but the tests will run anyway).
# Valid values:
#export DH_RUBY_IGNORE_TESTS=ruby1.8 ruby1.9.1 require-rubygems
#
# If you need to specify the .gemspec (eg there is more than one)
#export DH_RUBY_GEMSPEC=gem.gemspec

RAILS_VERSION=$(shell sed -n "s/^  version: //p" metadata.yml)

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

override_dh_testdir: debian/control
	dh_testdir

debian/control: debian/control.in metadata.yml
	sed -e "s/@RAILS_VERSION@/$(RAILS_VERSION)/" >$@ <$<

override_dh_install:
	# Use dh_install to install files not handled by gem2deb
	dh_install --sourcedir=$(CURDIR)
	# Move /usr/bin/* to railties/bin/
	mv $(CURDIR)/debian/ruby-rails-2.3/usr/bin \
	   $(CURDIR)/debian/ruby-rails-2.3/usr/lib/ruby/vendor_ruby/railties/
	# Move commands directory to railties/lib
	mv $(CURDIR)/debian/ruby-rails-2.3/usr/lib/ruby/vendor_ruby/commands \
	   $(CURDIR)/debian/ruby-rails-2.3/usr/lib/ruby/vendor_ruby/railties/lib/
	# Move tasks directory to railties/lib
	mv $(CURDIR)/debian/ruby-rails-2.3/usr/lib/ruby/vendor_ruby/tasks \
	   $(CURDIR)/debian/ruby-rails-2.3/usr/lib/ruby/vendor_ruby/railties/lib/
	# Move all *.rb files from vendor_ruby to railties/lib/
	mv $(CURDIR)/debian/ruby-rails-2.3/usr/lib/ruby/vendor_ruby/*.rb \
	   $(CURDIR)/debian/ruby-rails-2.3/usr/lib/ruby/vendor_ruby/railties/lib/
	# Install environments
	cp -r $(CURDIR)/environments \
	      $(CURDIR)/debian/ruby-rails-2.3/usr/lib/ruby/vendor_ruby/railties/
	# Remove executable bit from fresh_rakefile
	chmod -x $(CURDIR)/debian/ruby-rails-2.3/usr/lib/ruby/vendor_ruby/railties/fresh_rakefile

override_dh_link:
	rm $(CURDIR)/debian/ruby-rails-2.3/usr/lib/ruby/vendor_ruby/railties/html/javascripts/prototype.js
	for f in controls.js dragdrop.js effects.js; do \
	  rm $(CURDIR)/debian/ruby-rails-2.3/usr/lib/ruby/vendor_ruby/railties/html/javascripts/$$f; \
	done
	dh_link

override_dh_compress:
	dh_compress -XREADME
