#!/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))
COPYRIGHT_COMPONENTS_FILES=$(call COMPONENTS_FILES,copyright)
DOCS_COMPONENTS_FILES=$(call COMPONENTS_FILES,docs)
INSTALL_COMPONENTS_FILES=$(call COMPONENTS_FILES,install)
NODE_SUBPACKAGE=$(COMPONENTS)
ACORN_COMPONENTS=acorn-walk acorn-loose
PACKAGES_DOCS_DIR=$(COMPONENTS) node-acorn node-acorn-walk node-acorn-loose
ACORN_MAIN_PACKAGES=acorn $(ACORN_COMPONENTS)
ACORN_SUBPACKAGE=$(foreach component,$(COMPONENTS),$(shell echo $(component) | sed 's/^/node-/g'))
MAIN_PACKAGE=node-debbundle-acorn

%:
	dh $@

override_dh_auto_build:
	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
	@echo "# Build binary"
	rollup -c acorn/rollup.config.bin.js

# 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:
	#module-acorn-node/README.acorn-node.md
	# manual building of copyright
	mkdir -p debian/${MAIN_PACKAGE}/usr/share/doc/${MAIN_PACKAGE}
	if test "X$(PACKAGES_DOCS_DIR)" != X ; then \
		for module in $(PACKAGES_DOCS_DIR); do \
			mkdir -p "debian/${MAIN_PACKAGE}/usr/share/doc/$$module"; \
			ln -s "../${MAIN_PACKAGE}/copyright" "debian/${MAIN_PACKAGE}/usr/share/doc/$$module/copyright" ; \
		done; \
	else \
		true; \
	fi
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
	dh_installdocs --link-doc=${MAIN_PACKAGE}
	# install doc in subdir and link subdir to pseudo package
	ln -s ../node-acorn-walk debian/${MAIN_PACKAGE}/usr/share/doc/node-acorn/acorn-walk
	ln -s ../node-acorn-loose debian/${MAIN_PACKAGE}/usr/share/doc/node-acorn/acorn-loose
	# install
	mkdir -p "debian/${MAIN_PACKAGE}/usr/share/doc/node-acorn";
	install README.md debian/${MAIN_PACKAGE}/usr/share/doc/node-acorn
	if test "X$(NODE_SUBPACKAGE)" != X ; then \
		for module in $(NODE_SUBPACKAGE); do \
			mkdir -p "debian/$(MAIN_PACKAGE)/usr/share/doc/$$module"; \
			find "$$module" -maxdepth 1 -iname '*.md' -and -not -iname 'license.md' -exec install {} "debian/$(MAIN_PACKAGE)/usr/share/doc/$$module" \; ; \
		done; \
	else \
		true; \
	fi
	install acorn/*.md debian/$(MAIN_PACKAGE)/usr/share/doc/node-acorn/
	mkdir -p debian/$(MAIN_PACKAGE)/usr/share/doc/node-acorn-walk
	install acorn-walk/*.md debian/$(MAIN_PACKAGE)/usr/share/doc/node-acorn-walk
	mkdir -p debian/$(MAIN_PACKAGE)/usr/share/doc/node-acorn-loose
	install acorn-loose/*.md debian/$(MAIN_PACKAGE)/usr/share/doc/node-acorn-loose
else
override_dh_installdocs:
	@echo '**********************************************************'
	@echo 'Skip dh_installdocs                                       '
	@echo '**********************************************************'
endif

override_dh_installchangelogs:
	# create changelog
	if test "X$(PACKAGES_DOCS_DIR)" != X ; then \
		for module in $(PACKAGES_DOCS_DIR); do \
			mkdir -p "debian/$(MAIN_PACKAGE)/usr/share/doc/$$module"; \
			ln -s "../$(MAIN_PACKAGE)/changelog.Debian.gz" "debian/$(MAIN_PACKAGE)/usr/share/doc/$$module/changelog.Debian.gz" ; \
		done; \
	else \
		true; \
	fi
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
	dh_installchangelogs
#	mkdir -p debian/node-acorn/usr/share/doc/node-acorn
#install module-acorn-node/CHANGELOG.md debian/node-acorn/usr/share/doc/node-acorn/changelog.acorn-node
else
	dh_installchangelogs
endif

# maint rules
debian/copyright: debian/components/copyright $(COPYRIGHT_COMPONENTS_FILES) debian/components/copyright.license
	cp -f debian/components/copyright debian/copyright.in
	echo '' >> debian/copyright.in
	set -e; \
	for c in  $(COMPONENTS); do \
	    cat debian/components/$$c/copyright >> debian/copyright.in ; \
	    echo '' >> debian/copyright.in; \
	done
	cat debian/components/copyright.license >> debian/copyright.in
	cp -f debian/copyright.in debian/copyright

maint_rule: debian/copyright
	$(info $(components))
	$(info $(copyright_components_files))
	$(info $(copyright_components_docs))
	$(info run maint rules)

components_list_git_dpm=$(foreach component,$(components),--component ../acorn_$(deb_version_upstream).orig-$(component).tar.*)

git_dpm_rules:
	git dpm inu --ignore-unclean-branches --pristine-tar $(components_list_git_dpm) --rebase ../acorn_$(deb_version_upstream).orig.tar.*
