#!/usr/bin/make -f

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

MANPAGES_TARGET := $(subst .1.xml,.1, $(wildcard debian/man/*.*.xml))

TESTSUITEDIR=debian/testsuite
BUILDDIR=build

%:
	dh $@


define newline =


endef
define space =
endef

COMPONENTS=$(subst $(newline),$(space),$(shell set -e;cd debian/components && (find . -type d -printf "%f\n" | sed '/^[.]$$/d' )))
COMPONENTS_FILES_C=$(foreach component,$(COMPONENTS),./debian/components/$(component)/$(1))
COPYRIGHT_COMPONENTS_FILES=$(call COMPONENTS_FILES,copyright)
DOCS_COMPONENTS_FILES=$(call COMPONENTS_FILES,docs)
INSTALL_COMPONENTS_FILES=$(call COMPONENTS_FILES,install)
MODULES_COMPONENTS=$(foreach component,$(COMPONENTS),$(shell echo $(component) | sed 's/^module[-]//g'))

$(BUILDDIR)/builddir.stamp: src rollup
	mkdir -p $(BUILDDIR)
	mkdir -p dist
	ln -sf ../bin $(BUILDDIR)/bin
	ln -sf ../dist $(BUILDDIR)/dist
	cp -rf src $(BUILDDIR)
	cp -rf rollup  $(BUILDDIR)
	# remove identifier.js
	rm -f  $(BUILDDIR)/src/identifier.js
	touch $@

$(BUILDDIR)/src/identifier.js: src/identifier.js $(BUILDDIR)/builddir.stamp
	node bin/generate-identifier-regex.js src/identifier.js > $@

override_dh_auto_build: $(BUILDDIR)/builddir.stamp $(BUILDDIR)/src/identifier.js
	cd $(BUILDDIR) && rollup -c rollup/config.main.js \
	&& rollup -c rollup/config.walk.js \
	&& rollup -c rollup/config.loose.js \
	&& rollup -c rollup/config.loose_es.js \
	&& rollup -c rollup/config.bin.js

module-acorn-node/node_modules/acorn:
	mkdir -p module-acorn-node/node_modules
	ln -sf ../.. module-acorn-node/node_modules/acorn

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
override_dh_auto_test: module-acorn-node/node_modules/acorn
	# create a link in order to run acorn
	mkdir -p $(TESTSUITEDIR)
	ln -s ../../dist $(TESTSUITEDIR)/dist
	cp bin/_acorn.js $(TESTSUITEDIR)/binacorn
	chmod +x $(TESTSUITEDIR)/binacorn
	sed -i "s,require('../dist/acorn.js'),require('./dist/acorn.js'),g" $(TESTSUITEDIR)/binacorn
	@echo '*********************************************************'
	@echo 'Run test suite                                           '
	@echo '**********************************************************'
	# basic smoke test
	@echo Run bin acorn on itself
	@echo '=========================================================='
	./$(TESTSUITEDIR)/binacorn < dist/acorn.js
	@echo '=========================================================='
	# run package testsuite
	@echo '=========================================================='
	node test/run.js
	@echo '=========================================================='
	@echo '**********************************************************'
	@echo '*********************************************************'
	@echo 'Run test suite for node-acorn-node                       '
	@echo '**********************************************************'
	# run node-acorn
	cd module-acorn-node/ && tap -R spec test/index.js
else
override_dh_auto_test:
	@echo '**********************************************************'
	@echo 'Skip test suite                                           '
	@echo '**********************************************************'
endif	

debian/node-acorn.install: debian/components/install $(INSTALL_COMPONENTS_FILES)
	cp -f debian/components/install $@
	set -e; \
	for c in $(COMPONENTS); do \
			sed -e "/^./ s,^,$$c/,g" < debian/components/$$c/install >> $@; \
	done;

override_dh_install: debian/node-acorn.install
	dh_install

%.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

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

module-acorn-node/README.acorn-node.md: module-acorn-node/README.md
	ln $< $@

debian/node-acorn.docs: debian/components/docs $(DOCS_COMPONENTS_FILES)
	cp -f debian/components/docs $@
	set -e; \
	for c in $(COMPONENTS); do \
	    if test -f debian/components/$$c/docs; then \
		sed -e "/^./ s,^,$$c/,g" < debian/components/$$c/docs >> $@; \
	    else \
		true; \
	    fi \
	done;

ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
override_dh_installdocs: debian/node-acorn.docs module-acorn-node/README.acorn-node.md
	mkdir -p debian/node-acorn/usr/share/doc/
	ln -s node-acorn debian/node-acorn/usr/share/doc/node-acorn-node
	dh_installdocs
else
override_dh_installdocs:
	@echo '**********************************************************'
	@echo 'Skip dh_installdocs                                       '
	@echo '**********************************************************'
endif

override_dh_installchangelogs:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
	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
endif
	dh_installchangelogs


override_dh_auto_clean:
	rm -f debian/node-acorn.install
	rm -f debian/node-acorn.docs
	rm -f module-acorn-node/README.acorn-node.md
	rm -f module-acorn-node/node_modules/acorn
	rm -rf $(BUILDDIR)
	dh_auto_clean

# 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)
