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

include /usr/share/dpkg/pkg-info.mk

PKGDIR:=debian/puppet-agent

%:
	dh $@

execute_after_dh_auto_build:
	mv man man.orig
	HOME=$$(mktemp -d) LC_ALL=C.UTF-8 rake gen_manpages
	rm -rf man && mv man.orig man

#override_dh_auto_test:
#   # The puppet-agent testsuite is extensive and does not
#   # work at this time as part of the Debian build process.
#	rake spec TEST=spec/unit
#	rake spec TEST=spec/integration

override_dh_auto_install:
	./install.rb --destdir=debian/tmp \
		--codedir=/etc/puppet/code \
		--configdir=/etc/puppet \
		--vardir=/var/cache/puppet \
		--publicdir=/var/lib/puppet/public \
		--rundir=/run/puppet \
		--logdir=/var/log/puppet \
		--localedir=/usr/share/puppet/locale \
		--ruby=/usr/bin/ruby \
		--sitelibdir=/usr/lib/ruby/vendor_ruby

override_dh_install:
	# prepare vendor_modules directory
	mkdir vendor_modules
	cd modules && for m in *; do \
		n=$$(echo "$${m}" | cut -d- -f2); \
		mv "$${m}" "../vendor_modules/$${n}"; \
	done
	# invoke dh_install
	dh_install -Xusr/share/man

execute_after_dh_install:
	mkdir -p $(CURDIR)/debian/puppet-agent/usr/share/rubygems-integration/all/specifications
	sed -e "s#%DEB_VERSION_UPSTREAM%#$(DEB_VERSION_UPSTREAM)#g" < debian/puppet.gemspec.in > debian/puppet.gemspec
	install -m 644 $(CURDIR/)debian/puppet.gemspec \
		$(CURDIR)/debian/puppet-agent/usr/share/rubygems-integration/all/specifications/puppet-$(DEB_VERSION_UPSTREAM).gemspec

execute_after_dh_installdocs:
	# cleanup and move core module docs to /usr/share/doc
	cd $(CURDIR)/$(PKGDIR)/usr/share/puppet/vendor_modules && for m in *; do \
		mkdir -p $(CURDIR)/$(PKGDIR)/usr/share/doc/puppet-agent/modules/$${m}; \
		cd $${m} && for f in CHANGELOG CONTRIBUTING examples HISTORY NOTICE README readmes REFERENCE; do \
			test -e $${f}* && mv $${f}* $(CURDIR)/$(PKGDIR)/usr/share/doc/puppet-agent/modules/$${m}; \
		done; rm -f CODEOWNERS LICENSE*; cd ..; \
	done

override_dh_missing:
	dh_missing --fail-missing -Xusr/share/man

override_dh_installinit:
	install -m 0755 $(CURDIR)/ext/debian/puppet.init $(CURDIR)/debian/puppet-agent.puppet.init
	install -m 0644 $(CURDIR)/ext/debian/puppet.default $(CURDIR)/debian/puppet-agent.puppet.default
	dh_installinit --name=puppet --no-enable

override_dh_installsystemd:
	install -m 0644 $(CURDIR)/ext/systemd/puppet.service $(CURDIR)/debian/puppet-agent.puppet.service
	dh_installsystemd --name=puppet --no-enable
