#!/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

DEB_VERSION = $(shell dpkg-parsechangelog | awk '/^Version:/ { print $$2 }')
UPSTREAM_VERSION = $(word 1, $(subst -, ,$(DEB_VERSION)))
MAJOR = $(word 1, $(subst ., ,$(UPSTREAM_VERSION)))
MINOR = $(word 2, $(subst ., ,$(UPSTREAM_VERSION)))
PATCHLEVEL = $(word 3, $(subst ., ,$(UPSTREAM_VERSION)))
PATCHLEVEL_RSPEC = $(word 1, $(subst c, ,$(PATCHLEVEL)))
PATCHLEVEL_RSPEC_CORE = $(word 2, $(subst e, ,$(subst c, ,$(PATCHLEVEL))))
PATCHLEVEL_RSPEC_EXPECTATIONS = $(word 2, $(subst m, ,$(subst e, ,$(PATCHLEVEL))))
PATCHLEVEL_RSPEC_MOCKS = $(word 2, $(subst s, ,$(subst m, ,$(PATCHLEVEL))))
PATCHLEVEL_RSPEC_SUPPORT = $(word 2, $(subst s, ,$(PATCHLEVEL)))
RSPEC_VERSION = $(MAJOR).$(MINOR).$(PATCHLEVEL_RSPEC)
RSPEC_CORE_VERSION = $(MAJOR).$(MINOR).$(PATCHLEVEL_RSPEC_CORE)
RSPEC_EXPECTATIONS_VERSION = $(MAJOR).$(MINOR).$(PATCHLEVEL_RSPEC_EXPECTATIONS)
RSPEC_MOCKS_VERSION = $(MAJOR).$(MINOR).$(PATCHLEVEL_RSPEC_MOCKS)
RSPEC_SUPPORT_VERSION = $(MAJOR).$(MINOR).$(PATCHLEVEL_RSPEC_SUPPORT)

test:
	@echo $(RSPEC_VERSION)
	@echo $(RSPEC_CORE_VERSION)
	@echo $(RSPEC_EXPECTATIONS_VERSION)
	@echo $(RSPEC_MOCKS_VERSION)
	@echo $(RSPEC_SUPPORT_VERSION)

report:
	@dh_testdir
	@set -e && \
	(echo rspec $(RSPEC_VERSION); echo rspec-core $(RSPEC_CORE_VERSION); echo rspec-expectations $(RSPEC_EXPECTATIONS_VERSION); echo rspec-mocks $(RSPEC_MOCKS_VERSION); echo rspec-support $(RSPEC_SUPPORT_VERSION)) | { while read gem version; do \
		new_version=$$(uscan --verbose --report --watch debian/$$gem.watch --upstream-version $$version | sed -n -e 's/^Newest version on remote site is \([^,]*\), .*$$/\1/p'); \
		if [ "$$new_major_minor" ]; then \
			[ "$$new_major_minor" = "$${new_version%.*}" ] || { echo 'Major/minor version mismatch between gems!' >&2; exit 1; }; \
		fi; \
		new_major_minor="$${new_version%.*}"; \
		new_patchlevel="$${new_version##*.}"; \
		new_patchlevels="$${new_patchlevels:+$$new_patchlevels+}$$new_patchlevel"; \
	done; \
	new_upstream_version="$$new_major_minor.$$new_patchlevels"; \
	echo "Newest version on remote site is $$new_upstream_version, local version is $(UPSTREAM_VERSION)"; \
	if [ "$$new_upstream_version" = "$(UPSTREAM_VERSION)" ]; then \
		echo " => Package is up to date"; \
	else \
		echo " => Newer versions available. Update debian/changelog and run 'debian/rules get-orig-sources'"; \
	fi; \
	}

get-orig-sources:
	dh_testdir
	#git submodule init
	#git submodule update --checkout
	wget -O../ruby-rspec_$(UPSTREAM_VERSION).orig-rspec.tar.gz \
		https://github.com/rspec/rspec/archive/v$(RSPEC_VERSION).tar.gz
	#cd rspec && git fetch origin && git checkout v$(RSPEC_VERSION)
	wget -O../ruby-rspec_$(UPSTREAM_VERSION).orig-rspec-core.tar.gz \
		https://github.com/rspec/rspec-core/archive/v$(RSPEC_CORE_VERSION).tar.gz
	#cd rspec-core && git fetch origin && git checkout v$(RSPEC_CORE_VERSION)
	wget -O../ruby-rspec_$(UPSTREAM_VERSION).orig-rspec-expectations.tar.gz \
		https://github.com/rspec/rspec-expectations/archive/v$(RSPEC_EXPECTATIONS_VERSION).tar.gz
	#cd rspec-expectations && git fetch origin && git checkout v$(RSPEC_EXPECTATIONS_VERSION)
	wget -O../ruby-rspec_$(UPSTREAM_VERSION).orig-rspec-mocks.tar.gz \
		https://github.com/rspec/rspec-mocks/archive/v$(RSPEC_MOCKS_VERSION).tar.gz
	#cd rspec-mocks && git fetch origin && git checkout v$(RSPEC_MOCKS_VERSION)
	wget -O../ruby-rspec_$(UPSTREAM_VERSION).orig-rspec-support.tar.gz \
		https://github.com/rspec/rspec-support/archive/v$(RSPEC_SUPPORT_VERSION).tar.gz
	#cd rspec-support && git fetch origin && git checkout v$(RSPEC_SUPPORT_VERSION)

gen-gemspecs:
	# Let's create a static version of the gemspec as we don't build with git
	ruby -rrubygems -e ' \
		%w{rspec rspec-core rspec-expectations rspec-mocks rspec-support}.each do |gem| \
			Dir.chdir(gem) do \
				spec = Gem::Specification.load("#{gem}.gemspec"); \
				File.write("../debian/#{gem}.gemspec", spec.to_ruby); \
			end; \
		end \
		'

unpack-sources:
	for x in rspec rspec-core rspec-expectations rspec-mocks rspec-support; do \
		tar -C $$x -xzf ../ruby-rspec_$(UPSTREAM_VERSION).orig-$$x.tar.gz --strip-components=1; \
		done




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

override_dh_installman:
	mkdir man
	pod2man --center "" --release "" --name RSPEC --utf8 debian/rspec.1.pod man/rspec.1
	dh_installman

override_dh_auto_install:
	# need to have rspec in bin/ to be correctly autoinstalled
	dh_install -pruby-rspec-core rspec-core/exe/rspec usr/bin
	dh_install -pruby-rspec-core rspec-core/lib/rspec/core/project_initializer/.rspec usr/lib/ruby/vendor_ruby/rspec/core/project_initializer
	# create rspec-mocks/tmp used in tests
	mkdir -p rspec-mocks/tmp
	dh_auto_install

override_dh_auto_clean:
	rm -rf rspec-core/tmp/
	rm -rf rspec-expectations/coverage/
	rm -rf rspec-mocks/coverage/
	rm -rf rspec-mocks/tmp
	rm -rf man/
	dh_auto_clean

override_dh_installchangelogs:
	for x in rspec rspec-core rspec-expectations rspec-mocks rspec-support; do \
		dh_installchangelogs -pruby-$$x $$x/Changelog.md; \
	done
