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

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

PKGDIR := debian/puppet-agent
DOCPKGDIR := debian/puppet-doc/usr/share/doc/puppet

%:
	dh $@

execute_after_dh_auto_build:
ifeq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
	# build manpages
	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
	# build html reference docs
	for dir in references references/types; do \
		mkdir -p html/$${dir}; \
		for doc in $${dir}/*.md; do \
			doc_basename=$$(basename -s .md $$doc); \
			pandoc --standalone --from commonmark_x --shift-heading-level-by -1 --to html --toc \
				-o $(CURDIR)/html/$${dir}/$${doc_basename}.html \
				--css ../pandoc.css \
				$${doc}; \
		done; \
	done
	# build core module reference docs
	for ref in modules/*/REFERENCE.md; do \
		modulename=$$(dirname $$ref | grep -oP 'puppetlabs-\K[^\-]+'); \
		mkdir -p html/modules/$${modulename}; \
		pandoc --standalone --from gfm --to html \
			-o $(CURDIR)/html/modules/$${modulename}/reference.html \
			--css ../../pandoc.css \
			$${ref}; \
	done
	# build index page
	pandoc --standalone --from gfm --to html \
		-o $(CURDIR)/html/index.html \
		--css pandoc.css \
		debian/index.md
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:
	ruby debian/gem_helper.rb
	mkdir -p $(PKGDIR)/usr/share/rubygems-integration/all/specifications
	install -m 644 debian/puppet.gemspec \
		$(PKGDIR)/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)/$(DOCPKGDIR)/modules/$${m}; \
		cd $${m} && for f in CHANGELOG.md examples README.md readmes REFERENCE.md; do \
			test -e $${f} && mv $${f} $(CURDIR)/$(DOCPKGDIR)/modules/$${m}; \
		done; \
		if test -e NOTICE; then \
			mkdir -p $(CURDIR)/$(PKGDIR)/usr/share/doc/modules/$${m}; \
			mv NOTICE $(CURDIR)/$(PKGDIR)/usr/share/doc/modules/$${m}; \
		fi; \
		rm -rf acceptance spec; find . -maxdepth 1 -type f -delete; cd ..; \
	done
	# install css for html references
	install -m 644 -D debian/pandoc.css $(DOCPKGDIR)/html
	ln -s ../pandoc.css $(DOCPKGDIR)/html/references
	# exclude markdown manpages from docs package
	rm -rf $(DOCPKGDIR)/references/man
	# replace external urls in html docs
	sed -i 's,https://puppet.com/docs/puppet/latest/\(configuration\|function\|metaparameter\|types\)\.html,\1.html,' \
		$(DOCPKGDIR)/html/references/*.html
	sed -i 's,https://puppet.com/docs/puppet/latest/\(configuration\|function\|metaparameter\|types\)\.html,../\1.html,' \
		$(DOCPKGDIR)/html/references/types/*.html

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 html
	rm -f debian/puppet.gemspec
