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

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

include /usr/share/dpkg/pkg-info.mk
MANPAGES_TARGET := $(subst .1.xml,.1, $(wildcard debian/man/*.*.xml))
NODE_SUBPACKAGE=$(shell find .  -maxdepth 1 -name 'node-*' -type d -print | sed s,./,,g)
COMPONENTS=$(NODE_SUBPACKAGE)
ACORN_COMPONENTS=acorn-walk acorn-loose
PACKAGES_DOCS_DIR=$(COMPONENTS) node-acorn-walk node-acorn-loose
ACORN_MAIN_PACKAGES=acorn $(ACORN_COMPONENTS)
ACORN_ALL_COMPONENTS=node-acorn-walk node-acorn-loose $(COMPONENTS)
MAIN_PACKAGE=node-acorn

%:
	dh $@ --with nodejs

override_dh_auto_build:
	# back link
	set -e; for module in $(ACORN_COMPONENTS); do \
		ln -s $$module node-$$module ;\
	done
	dh_auto_build --buildsystem=nodejs
	@for dir in acorn acorn-walk acorn-loose; do \
		echo "# Build $$dir"; \
		echo "rollup -c $$dir/rollup.config.js"; \
		rollup -c $$dir/rollup.config.js; \
	done

# MANPAGES & DOC
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
override_dh_installman: $(MANPAGES_TARGET)
	dh_installman
else
override_dh_installman:
	@echo '**********************************************************'
	@echo 'Skip man page create                                      '
	@echo '**********************************************************'
endif

%.1: %.1.xml
	# Create man page from DocBook XML
	cd $(dir $@) && docbook2x-man --encoding=utf-8 --string-param header-3="$(SOURCE_DATE_EPOCH)" $(notdir $@).xml


override_dh_installdocs:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
	dh_installdocs --link-doc=node-acorn
	dh_nodejs_autodocs auto_dispatch
else
override_dh_installdocs:
	@echo '**********************************************************'
	@echo 'Skip dh_installdocs                                       '
	@echo '**********************************************************'
endif

override_dh_installchangelogs:
	dh_installchangelogs acorn/CHANGELOG.md

override_dh_auto_clean:
	set -e; for module in $(ACORN_COMPONENTS); do \
		rm -f node-$$module ; \
	done
	rm -rf node_modules
	rm -rf node-acorn-node/node_modules
	dh_auto_clean -- --with nodejs
