#!/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=$(shell set -e;cd debian/components && (find . -type d -printf "%f\n" | sed '/^[.]$$/d' | xargs printf "%s " | sed 's/[[:space:]]*$$//g'))
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)
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

BUILD_STAMPS_ACORN_MAIN=$(foreach pkg,$(ACORN_MAIN_PACKAGES),$(BUILDDIR)/$(pkg)/build.stamp)
BUILD_STAMPS_COMPONENTS=$(foreach pkg,$(COMPONENTS),$(pkg)/build.stamp)
BUILD_STAMPS=$(BUILD_STAMPS_ACORN_MAIN) $(BUILD_STAMPS_COMPONENTS)

# work arround transpile bug
ROLLUP=TRANSPILE=babel6 BABEL_PRESET=es2015 rollup

ACORN_SRC= $(shell find acorn/src -name "*.js" -type f | LC_ALL=C sort)

$(BUILDDIR)/acorn/builddir.stamp: $(ACORN_SRC) acorn/rollup*.js acorn/package.json acorn/bin/acorn
	mkdir -p $(dir $@)
	cp -rf acorn $(BUILDDIR)
	# remove identifier
	rm -f  $(dir $@)/src/identifier.js
	touch $@

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

$(BUILDDIR)/acorn/build.stamp: $(BUILDDIR)/acorn/builddir.stamp $(BUILDDIR)/acorn/src/identifier.js
	cd $(BUILDDIR) && $(ROLLUP) -c acorn/rollup.config.js
	cd $(BUILDDIR) && $(ROLLUP) -c acorn/rollup.config.bin.js
	touch $@

ACORN_WALK_SRC= $(shell find acorn-walk/src -name "*.js" -type f | LC_ALL=C sort)

$(BUILDDIR)/acorn-walk/builddir.stamp: $(ACORN_WALK_SRC) acorn-walk/rollup*.js acorn-walk/package.json
	mkdir -p $(dir $@)
	cp -rf acorn-walk $(BUILDDIR)
	touch $@

$(BUILDDIR)/acorn-walk/build.stamp: $(BUILDDIR)/acorn-walk/builddir.stamp $(BUILDDIR)/acorn/build.stamp
	cd $(BUILDDIR) && $(ROLLUP) -c acorn-walk/rollup.config.js
	touch $@

ACORN_LOOSE_SRC= $(shell find acorn-walk/src -name "*.js" -type f | LC_ALL=C sort)

$(BUILDDIR)/acorn-loose/build.stamp: $(BUILDDIR)/acorn/build.stamp
	mkdir -p $(dir $@)
	cp -rf acorn-loose $(BUILDDIR)
	mkdir -p $(dir $@)/node_modules/
	ln -s ../../acorn $(dir $@)/node_modules/acorn
	touch $@
	cd $(BUILDDIR) && $(ROLLUP) -c acorn-loose/rollup.config.js
	touch $@

ACORN_DYNAMIC_IMPORT_SRC=$(shell find node-acorn-dynamic-import/src -name "*.js" -type f | LC_ALL=C sort)

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

ACORN_BIGINT_SRC=node-acorn-bigint/index.js

node-acorn-bigint/build.stamp: $(BUILDDIR)/acorn/build.stamp $(ACORN_BIGINT_SRC)
	mkdir -p $(dir $@)/node_modules
	ln -sf ../../$(BUILDDIR)/acorn $(dir $@)/node_modules/acorn
	touch $@

ACORN_IMPORT_META_SRC=node-acorn-import-meta/index.js

node-acorn-import-meta/build.stamp: $(BUILDDIR)/acorn/build.stamp $(ACORN_IMPORT_META_SRC)
	mkdir -p $(dir $@)/node_modules
	ln -sf ../../$(BUILDDIR)/acorn $(dir $@)/node_modules/acorn
	touch $@


ACORN_PRIVATE_CLASS_ELEMENTS_SRC=node-acorn-private-class-elements/index.js

node-acorn-private-class-elements/build.stamp: $(BUILDDIR)/acorn/build.stamp $(ACORN_PRIVATE_CLASS_ELEMENTS_SRC)
	mkdir -p $(dir $@)/node_modules
	ln -sf ../../$(BUILDDIR)/acorn $(dir $@)/node_modules/acorn


ACORN_CLASS_FIELDS_SRC=node-acorn-class-fields/index.js

node-acorn-class-fields/build.stamp: $(BUILDDIR)/acorn/build.stamp node-acorn-private-class-elements/build.stamp $(ACORN_CLASS_FIELDS_SRC)
	mkdir -p $(dir $@)/node_modules
	ln -sf ../../$(BUILDDIR)/acorn $(dir $@)/node_modules/acorn
	ln -sf ../../node-acorn-private-class-elements $(dir $@)/node_modules/acorn-private-class-elements

ACORN_STATIC_CLASS_FEATURES_SRC=node-acorn-static-class-features/index.js

node-acorn-static-class-features/build.stamp: $(BUILDDIR)/acorn/build.stamp node-acorn-private-class-elements/build.stamp $(ACORN_STATIC_CLASS_FEATURES_SRC)
	mkdir -p $(dir $@)/node_modules
	ln -sf ../../$(BUILDDIR)/acorn $(dir $@)/node_modules/acorn
	ln -sf ../../node-acorn-private-class-elements $(dir $@)/node_modules/acorn-private-class-elements

ACORN_EXPORT_NS_FROM_SRC=node-acorn-export-ns-from/index.js

node-acorn-export-ns-from/build.stamp: $(BUILDDIR)/acorn/build.stamp $(ACORN_EXPORT_NS_FROM_SRC)
	mkdir -p $(dir $@)/node_modules
	ln -sf ../../$(BUILDDIR)/acorn $(dir $@)/node_modules/acorn

ACORN_NUMERIC_SEPARATOR_SRC=node-acorn-numeric-separator/index.js

node-acorn-numeric-separator/build.stamp: $(BUILDDIR)/acorn/build.stamp $(ACORN_NUMERIC_SEPARATOR_SRC)
	mkdir -p $(dir $@)/node_modules
	ln -sf ../../$(BUILDDIR)/acorn $(dir $@)/node_modules/acorn

ACORN_PRIVATE_METHODS_SRC=node-acorn-private-methods/index.js

node-acorn-private-methods/build.stamp: $(BUILDDIR)/acorn/build.stamp node-acorn-private-class-elements/build.stamp $(ACORN_PRIVATE_METHODS_SRC)
	mkdir -p $(dir $@)/node_modules
	ln -sf ../../$(BUILDDIR)/acorn $(dir $@)/node_modules/acorn
	ln -sf ../../node-acorn-private-class-elements $(dir $@)/node_modules/acorn-private-class-elements

ACORN_NODE_SRC=node-acorn-node/build.js node-acorn-node/walk.js node-acorn-node/index.js

node-acorn-node/build.stamp: $(BUILDDIR)/acorn/build.stamp node-acorn-bigint/build.stamp node-acorn-import-meta/build.stamp $(BUILDDIR)/acorn-walk/build.stamp node-acorn-dynamic-import/build.stamp $(ACORN_NODE_SRC) node-acorn-private-class-elements/build.stamp node-acorn-class-fields/build.stamp node-acorn-static-class-features/build.stamp node-acorn-export-ns-from/build.stamp
	mkdir -p $(dir $@)/node_modules
	ln -sf ../../$(BUILDDIR)/acorn $(dir $@)/node_modules/acorn
	ln -sf ../../node-acorn-bigint $(dir $@)/node_modules/acorn-bigint
	ln -sf ../../node-acorn-import-meta $(dir $@)/node_modules/acorn-import-meta
	ln -sf ../../$(BUILDDIR)/acorn-walk $(dir $@)/node_modules/acorn-walk
	ln -sf ../../node-acorn-dynamic-import $(dir $@)/node_modules/acorn-dynamic-import
	ln -sf ../../node-acorn-private-class-elements $(dir $@)/node_modules/acorn-private-class-elements
	ln -sf ../../node-acorn-class-fields $(dir $@)/node_modules/acorn-class-fields
	ln -sf ../../node-acorn-static-class-features $(dir $@)/node_modules/acorn-static-class-features
	ln -sf ../../node-acorn-export-ns-from $(dir $@)/node_modules/acorn-export-ns-from
	touch $@

node-acorn-jsx/build.stamp: $(BUILDDIR)/acorn/build.stamp node-acorn-jsx/*.js
	mkdir -p $(dir $@)/node_modules
	ln -sf ../../$(BUILDDIR)/acorn $(dir $@)/node_modules/acorn
	touch $@

override_dh_auto_build: $(BUILD_STAMPS)

override_dh_auto_test_acorn: $(BUILD_STAMPS_ACORN_MAIN)
	# create a link in order to run acorn
	mkdir -p $(TESTSUITEDIR)
	mkdir -p $(TESTSUITEDIR)/node_modules
	ln -sf ../../$(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 '=========================================================='
	# cp -r test $(BUILDDIR)
	# cd $(BUILDDIR) && node test/run.js
	@echo '=========================================================='
	@echo Run smoke test for walk
	ln -s ../../../$(BUILDDIR)/acorn $(TESTSUITEDIR)/node_modules/acorn
	ln -s ../../../$(BUILDDIR)/acorn-walk $(TESTSUITEDIR)/node_modules/acorn-walk
	cp debian/tests/walk.js $(TESTSUITEDIR)/walk.js
	cd $(TESTSUITEDIR) && ./walk.js
	@echo '**********************************************************'


ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
override_dh_auto_test: override_dh_auto_test_acorn
	# run bigint
	@echo '*********************************************************'
	@echo 'Run test suite for node-acorn-bigint                     '
	@echo '**********************************************************'
	cd node-acorn-bigint/ && mocha -R spec
	# run acorn-dynamic-import
	@echo '*********************************************************'
	@echo 'Run test suite for node-acorn-dynamic-import               '
	@echo '**********************************************************'
	# smoke test
	cd 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 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
	# not yet licence problem
	# cd node-acorn-jsx/ && node test/run.js
	cd node-acorn-jsx && node -e 'var acorn = require("acorn");var jsx = require(".");'
        # run node-acorn-private-class-elements
	@echo '*********************************************************'
	@echo 'Run test suite for node-private-class-elements                   '
	@echo '**********************************************************'
	cd node-acorn-private-class-elements/ && mocha -R spec
	# run acorn-class-fields
	@echo '*********************************************************'
	@echo 'Run test suite for node-class-fields'
	@echo '**********************************************************'
	cd node-acorn-class-fields/ && mocha -R spec
	# run acorn-static-class-features
	@echo '*********************************************************'
	@echo 'Run test suite for node-acorn-static-class-features'
	@echo '**********************************************************'
	cd node-acorn-static-class-features/ && mocha -R spec
	# run acorn-export-ns-from
	@echo '*********************************************************'
	@echo 'Run test suite for node-acorn-export-ns-from'
	@echo '**********************************************************'
	cd node-acorn-numeric-separator/ && mocha -R spec
	@echo '*********************************************************'
	@echo 'Run test suite for node-acorn-numeric-separator'
	@echo '**********************************************************'
	cd node-acorn-numeric-separator/ && mocha -R spec
	@echo '*********************************************************'
	@echo 'Run test suite for node-acorn-private-methods'
	@echo '**********************************************************'
	cd node-acorn-private-methods/ && mocha -R spec
	# run node-acorn
	@echo '*********************************************************'
	@echo 'Run test suite for node-acorn-node                       '
	@echo '**********************************************************'
	# run node-acorn
	cd node-acorn-node/ && tap -R spec test/index.js
else
override_dh_auto_test:
	@echo '**********************************************************'
	@echo 'Skip test suite                                           '
	@echo '**********************************************************'
endif	

debian/node-debbundle-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-debbundle-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


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




override_dh_auto_clean:
	# rm autogenerated
	rm -f debian/node-debbundle-acorn.install
	rm -f debian/node-debbundle-acorn.docs
	rm -rf $(TESTSUITEDIR)
	# 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 node-acorn-dynamic-import/lib
	rm -rf acorn/dist/*.js
	rm -rf acorn/dist/*.mjs
	rm -rf acorn/dist/*.map
	rm -rf 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)

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