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

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

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

MAIN_PKG=node-boom
SUBMODULES=node-hapi-bounce
SUBMODULES_GIT=node-hapi-bounce

%:
	dh $@ --with nodejs

override_dh_auto_clean:
	dh_auto_clean
	rm -rf node_modules

override_dh_auto_configure:
	dh_auto_configure
	mkdir -p node_modules/@hapi/
	ln -s ../../ node_modules/@hapi/boom

#override_dh_auto_build:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
override_dh_auto_test:
	nodejs -e "require('.')"
	cd node-hapi-bounce && nodejs -e "require('.')"
endif

ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
override_dh_installdocs:
	dh_installdocs
	mkdir -p debian/$(MAIN_PKG)/usr/share/doc/node-hapi-boom
	install README.md $(CURDIR)/debian/$(MAIN_PKG)/usr/share/doc/node-hapi-boom
	install API.md $(CURDIR)/debian/$(MAIN_PKG)/usr/share/doc/node-hapi-boom
	cd  debian/$(MAIN_PKG)/usr/share/doc/$(MAIN_PKG) && for file in *; do \
		ln -sf ../$(MAIN_PKG)/$$file $(CURDIR)/debian/$(MAIN_PKG)/usr/share/doc/node-hapi-boom/$$file || exit 1; \
	done
	set -e;for module in $(SUBMODULES); do \
		mkdir -p "debian/$(MAIN_PKG)/usr/share/doc/$$module"; \
		ln -s ../$(MAIN_PKG)/copyright debian/$(MAIN_PKG)/usr/share/doc/$$module/copyright || exit 1; \
	done
	set -e;for module in $(SUBMODULES); do \
		mkdir -p "debian/$(MAIN_PKG)/usr/share/doc/$$module"; \
		find -L "$$module" -maxdepth 1 -iname '*.md' -and -not -iname 'license.md' -and -not -iname 'changelog.md' -exec install {} "debian/$(MAIN_PKG)/usr/share/doc/$$module" \; || exit 1;\
	done
else
override_dh_installdocs:
        @echo '**********************************************************'
        @echo 'Skip doc'
        @echo '**********************************************************'
endif


override_dh_installchangelogs:
	dh_installchangelogs -XCHANGELOG.md
	mkdir -p debian/$(MAIN_PKG)/usr/share/doc/node-hapi-boom
	ln -sf ../$(MAIN_PKG)/changelog.Debian.gz debian/$(MAIN_PKG)/usr/share/doc/node-hapi-boom/changelog.Debian.gz
	set -e;for module in $(SUBMODULES); do \
		mkdir -p "debian/$(MAIN_PKG)/usr/share/doc/$$module"; \
		ln -s ../$(MAIN_PKG)/changelog.Debian.gz debian/$(MAIN_PKG)/usr/share/doc/$$module/changelog.Debian.gz; \
	done
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
	mkdir -p debian/$(MAIN_PKG)/usr/share/doc/node-hapi-boom
	cp CHANGELOG.md debian/$(MAIN_PKG)/usr/share/doc/node-hapi-boom/changelog
	set -e;for module in $(SUBMODULES); do \
		mkdir -p "debian/$(MAIN_PKG)/usr/share/doc/$$module"; \
		find -L "$$module" -maxdepth 1 -iname 'changelog.md' -exec cp {} "debian/$(MAIN_PKG)/usr/share/doc/$$module/changelog" \; ;\
	done
else
       # skip
endif

COMPONENTS_LIST_GIT_DPM=$(foreach component,$(SUBMODULES_GIT),--component ../$(MAIN_PKG)_$(DEB_VERSION_UPSTREAM).orig-$(component).tar.*)

git_dpm_rules:
	git dpm inu --ignore-unclean-branches --pristine-tar $(COMPONENTS_LIST_GIT_DPM) --rebase ../$(MAIN_PKG)_$(DEB_VERSION_UPSTREAM).orig.tar.*
