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

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

PKGDIR:=debian/puppet-agent

%:
	dh $@

execute_after_dh_auto_build:
ifeq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
	mv man man.orig
	HOME=$$(mktemp -d) LC_ALL=C.UTF-8 rake -I ./lib gen_manpages
	mv man man.tmp
	mv man.orig man
endif

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
	install -m 644 $(CURDIR)/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.md examples NOTICE README.md readmes REFERENCE.md; do \
		test -e $${f} && mv $${f} $(CURDIR)/$(PKGDIR)/usr/share/doc/puppet-agent/modules/$${m}; \
		done; rm -rf acceptance spec; find . -maxdepth 1 -type f -delete; 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

execute_after_dh_clean:
	rm -rf man.tmp
