#!/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'))
NODE_SUBPACKAGE=$(MODULES_COMPONENTS)
ACORN_COMPONENTS=acorn-walk acorn-loose
ACORN_SUBPACKAGE=$(foreach component,$(ACORN_COMPONENTS),$(shell echo $(component) | sed 's/^/node-/g'))

$(BUILDDIR)/builddir.stamp:
	mkdir -p $(BUILDDIR)
	cp -r acorn $(BUILDDIR)
	cp -r acorn-walk $(BUILDDIR)
	cp -r acorn-loose $(BUILDDIR)
	cp -r test $(BUILDDIR)
	cp package.json $(BUILDDIR)
	mkdir -p $(BUILDDIR)/acorn-loose/node_modules/
	ln -s ../../acorn $(BUILDDIR)/acorn-loose/node_modules/acorn
	# remove identifier
	rm -f  $(BUILDDIR)/acorn/src/identifier.js
	touch $@

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


$(BUILDDIR)/build.stamp: $(BUILDDIR)/builddir.stamp $(BUILDDIR)/acorn/src/identifier.js
	# build main package
	cd $(BUILDDIR) && rollup -c acorn/rollup.config.js
	cd $(BUILDDIR) && rollup -c acorn-walk/rollup.config.js
	cd $(BUILDDIR) && rollup -c acorn-loose/rollup.config.js
	cd $(BUILDDIR) && rollup -c acorn/rollup.config.bin.js
	touch $@

module-node-acorn-dynamic-import/node_modules/acorn: $(BUILDDIR)/builddir.stamp
	mkdir -p module-node-acorn-dynamic-import/node_modules
	ln -sf ../../$(BUILDDIR)/acorn module-node-acorn-dynamic-import/node_modules/acorn

module-node-acorn-dynamic-import/node_modules/acorn-walk: $(BUILDDIR)/builddir.stamp
	mkdir -p module-node-acorn-dynamic-import/node_modules
	ln -sf ../../$(BUILDDIR)/acorn-walk module-node-acorn-dynamic-import/node_modules/acorn-walk

module-node-acorn-dynamic-import/build.stamp: $(BUILDDIR)/build.stamp module-node-acorn-dynamic-import/node_modules/acorn module-node-acorn-dynamic-import/node_modules/acorn-walk
	cd module-node-acorn-dynamic-import && babeljs src --out-dir lib
	touch $@

module-node-acorn-bigint/node_modules/acorn: $(BUILDDIR)/builddir.stamp
	mkdir -p module-node-acorn-bigint/node_modules
	ln -sf ../../$(BUILDDIR)/acorn module-node-acorn-bigint/node_modules/acorn

module-node-acorn-bigint/build.stamp: module-node-acorn-bigint/node_modules/acorn
	touch $@

module-node-acorn-import-meta/node_modules/acorn: $(BUILDDIR)/builddir.stamp
	mkdir -p module-node-acorn-import-meta/node_modules
	ln -sf ../../$(BUILDDIR)/acorn module-node-acorn-import-meta/node_modules/acorn

module-node-acorn-import-meta/build.stamp: module-node-acorn-import-meta/node_modules/acorn
	touch $@

module-node-acorn-node/node_modules/acorn: $(BUILDDIR)/builddir.stamp
	mkdir -p module-node-acorn-dynamic-import/node_modules
	ln -sf ../../$(BUILDDIR)/acorn module-node-acorn-node/node_modules/acorn

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

module-node-acorn-node/node_modules/acorn-import-meta: module-node-acorn-import-meta/build.stamp
	mkdir -p module-node-acorn-node/node_modules
	ln -sf ../../module-node-acorn-import-meta module-node-acorn-node/node_modules/acorn-import-meta

module-node-acorn-node/node_modules/acorn-walk: $(BUILDDIR)/builddir.stamp
	mkdir -p module-node-acorn-node/node_modules
	ln -sf ../../$(BUILDDIR)/acorn-walk module-node-acorn-node/node_modules/acorn-walk

module-node-acorn-node/node_modules/acorn-dynamic-import: module-node-acorn-dynamic-import/build.stamp
	mkdir -p module-node-acorn-node/node_modules
	ln -sf ../../module-node-acorn-dynamic-import module-node-acorn-node/node_modules/acorn-dynamic-import

module-node-acorn-jsx/node_modules/acorn: $(BUILDDIR)/builddir.stamp
	mkdir -p module-node-acorn-jsx/node_modules
	ln -sf ../../$(BUILDDIR)/acorn module-node-acorn-jsx/node_modules/acorn

module-node-acorn-node/build.stamp: $(BUILDDIR)/build.stamp module-node-acorn-node/node_modules/acorn-walk module-node-acorn-node/node_modules/acorn module-node-acorn-node/node_modules/acorn-bigint module-node-acorn-node/node_modules/acorn-import-meta module-node-acorn-node/node_modules/acorn-dynamic-import module-node-acorn-bigint/build.stamp module-node-acorn-dynamic-import/build.stamp module-node-acorn-import-meta/build.stamp
	mkdir -p  module-node-acorn-node/lib
	mkdir -p  module-node-acorn-node/lib/bigint
	mkdir -p  module-node-acorn-node/lib/import-meta
	mkdir -p  module-node-acorn-node/lib/dynamic-import
	cd module-node-acorn-node && nodejs build.js

override_dh_auto_build: $(BUILDDIR)/build.stamp module-node-acorn-dynamic-import/build.stamp module-node-acorn-node/build.stamp

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
override_dh_auto_test: module-node-acorn-bigint/node_modules/acorn module-node-acorn-import-meta/node_modules/acorn module-node-acorn-jsx/node_modules/acorn
	# create a link in order to run acorn
	mkdir -p $(TESTSUITEDIR)
	ln -s ../../$(BUILDDIR)/acorn/dist $(TESTSUITEDIR)/dist
	cp $(BUILDDIR)/acorn/bin/acorn $(TESTSUITEDIR)/binacorn
	chmod +x $(TESTSUITEDIR)/binacorn
	sed -i "s,require('../dist/bin.js'),require('./dist/bin.js'),g" $(TESTSUITEDIR)/binacorn
	@echo '*********************************************************'
	@echo 'Run test suite                                           '
	@echo '**********************************************************'
	# basic smoke test
	@echo Run bin acorn on itself
	@echo '=========================================================='
	./$(TESTSUITEDIR)/binacorn < $(TESTSUITEDIR)/dist/acorn.js
	@echo '=========================================================='
	# run package testsuite
	@echo '=========================================================='
	cd $(BUILDDIR) && node test/run.js
	@echo '=========================================================='
	@echo '**********************************************************'
	# run bigint
	@echo '*********************************************************'
	@echo 'Run test suite for node-acorn-bigint                     '
	@echo '**********************************************************'
	cd module-node-acorn-bigint/ && mocha -R spec
	# run acorn-dynamic-import
	@echo '*********************************************************'
	@echo 'Run test suite for node-acorn-dynamic-import               '
	@echo '**********************************************************'
	# smoke test
	cd module-node-acorn-dynamic-import && node -e "require('.')"
	#cd module-node-acorn-dynamic-import && mocha -R spec
	# run acorn-import-meta
	@echo '*********************************************************'
	@echo 'Run test suite for node-acorn-meta-import                '
	@echo '**********************************************************'
	cd module-node-acorn-import-meta/ && mocha -R spec
	# run node-acorn-jsx
	@echo '*********************************************************'
	@echo 'Run test suite for node-acorn-jsx                       '
	@echo '**********************************************************'
	# run node-acorn-jsx
	cd module-node-acorn-jsx/ && node test/run.js
	# run node-acorn
	@echo '*********************************************************'
	@echo 'Run test suite for node-acorn-node                       '
	@echo '**********************************************************'
	# run node-acorn
	cd module-node-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; \
	if test "X$(COMPONENTS)" != X; then \
		for c in $(COMPONENTS); do \
			sed -e "/^./ s,^,$$c/,g" < debian/components/$$c/install >> $@; \
		done; \
	else \
	     true; \
	fi;

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; \
	if test "X$(COMPONENTS)" != X; then \
		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; \
	else \
		true;\
	fi;

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

override_dh_installchangelogs:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
	# create changelog
	mkdir -p debian/node-acorn/usr/share/doc/node-acorn
	mkdir -p debian/node-acorn/usr/share/doc/node-acorn-bigint
	mkdir -p debian/node-acorn/usr/share/doc/node-acorn-walk
	mkdir -p debian/node-acorn/usr/share/doc/node-acorn-loose
	mkdir -p debian/node-acorn/usr/share/doc/node-acorn-import-meta
	if test "X$(NODE_SUBPACKAGE)$(ACORN_SUBPACKAGE)" != X ; then \
		for module in $(NODE_SUBPACKAGE) $(ACORN_SUBPACKAGE); do \
			mkdir -p "debian/node-acorn/usr/share/doc/$$module"; \
			ln -s "../node-acorn/changelog.Debian.gz" "debian/node-acorn/usr/share/doc/$$module/changelog.Debian.gz" ; \
		done; \
	else \
		true; \
	fi
	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
endif
	dh_installchangelogs



override_dh_auto_clean:
	# rm autogenerated
	rm -f debian/node-acorn.install
	rm -f debian/node-acorn.docs
	# buildir
	rm -rf $(BUILDDIR)
	# rm node_modules and dist
	if test "X$(COMPONENTS)$(ACORN_COMPONENTS)" != X ; then \
		for module in $(COMPONENTS) $(ACORN_COMPONENTS); do \
			rm -rf "$$module/node_modules";\
			rm -rf "$$module/dist";\
			rm -rf "$$module/build.stamp";\
		done; \
	else \
		true; \
	fi
	rm -rf module-node-acorn-dynamic-import/lib
	rm -rf acorn/dist/*.js
	rm -rf acorn/dist/*.mjs
	rm -rf acorn/dist/*.map
	rm -rf module-node-acorn-node/lib
	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)

git_dpm_rules:
	 git dpm inu -p upstreamdebian --pristine-tar --component ../acorn_6.0.2+20181021git007b08d01eff070+ds+~0.3.1+~4.0.0+~0.3.0+~5.0.0+ds+~1.6.1+ds.orig-module-node-acorn-bigint.tar.gz --component ../acorn_6.0.2+20181021git007b08d01eff070+ds+~0.3.1+~4.0.0+~0.3.0+~5.0.0+ds+~1.6.1+ds.orig-module-node-acorn-jsx.tar.xz --component ../acorn_6.0.2+20181021git007b08d01eff070+ds+~0.3.1+~4.0.0+~0.3.0+~5.0.0+ds+~1.6.1+ds.orig-module-node-acorn-dynamic-import.tar.gz --component ../acorn_6.0.2+20181021git007b08d01eff070+ds+~0.3.1+~4.0.0+~0.3.0+~5.0.0+ds+~1.6.1+ds.orig-module-node-acorn-import-meta.tar.gz --component ../acorn_6.0.2+20181021git007b08d01eff070+ds+~0.3.1+~4.0.0+~0.3.0+~5.0.0+ds+~1.6.1+ds.orig-module-node-acorn-node.tar.xz --rebase ../acorn_6.0.2+20181021git007b08d01eff070+ds+~0.3.1+~4.0.0+~0.3.0+~5.0.0+ds+~1.6.1+ds.orig.tar.xz
