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

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

AGENTPKGDIR := debian/openvox-agent
DOCPKGDIR := debian/openvox-doc/usr/share/doc/openvox-doc

export DH_RUBY = --gem-install
export DH_RUBY_GEM_INSTALL_EXCLUDE = *
export DH_RUBY_GEM_INSTALL_INCLUDE = lib/* locales/*
export GEM2DEB_TEST_RUNNER = --check-dependencies

%:
	dh $@ --buildsystem=ruby

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
	# 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/doc/index.md
endif

execute_after_override_dh_auto_install:
	# remove binary from ruby-openvox package
	rm -rf debian/ruby-openvox//usr/share/rubygems-integration/all/gems/openvox-*/bin

execute_before_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

execute_after_dh_installdocs:
ifeq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
	# cleanup and move core module docs to openvox-doc pkg
	cd $(CURDIR)/$(AGENTPKGDIR)/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)/$(AGENTPKGDIR)/usr/share/doc/openvox-agent/modules/$${m}; \
			mv NOTICE $(CURDIR)/$(AGENTPKGDIR)/usr/share/doc/openvox-agent/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/doc/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
	cd $(DOCPKGDIR)/.. && ln -s openvox-doc puppet
endif

execute_after_dh_installman:
ifeq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
	rm -rf man
	mv man.orig man
endif

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

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