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

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

POLYMER_BUILD_ARGS=--root ./src -v --css-minify --bundle

%:
	dh $@

override_dh_clean:
	dh_clean node_modules/ build/ src/polymer.json

override_dh_auto_configure:
	# Define HOME since npm stores downloaded files under ~/.npm/
	# Do not use global install because user is not root
	HOME=/tmp npm install polymer-cli@1.2

override_dh_auto_build:
	mkdir -p dist/static

	# Bundle index.html
	./node_modules/.bin/polymer build --entrypoint index.html --shell elements/app-shell/app-shell.html $(POLYMER_BUILD_ARGS)
	cp -a build/default/src/index.html dist/static/index.html
	mkdir -p dist/static/elements/app-shell
	cp -ar build/default/src/elements/app-shell/app-shell.html dist/static/elements/app-shell/
	cp -ar src/elements/app-shell/locales.json dist/static/elements/app-shell/

	# Bundle run_action.html
	cd src/run_action; ln -sf /usr/share/ewt/static/tiramisu-web tiramisu-web; ../../node_modules/.bin/polymer build
	ls build/default/
	cp -a src/run_action/build/default/run_action.html dist/static/run_action.html

	# Bundle read_file.html
	./node_modules/.bin/polymer build --entrypoint read_file.html --shell read_file.html $(POLYMER_BUILD_ARGS)
	cp -a build/default/src/read_file.html dist/static/read_file.html

	# Bundle ewt-custom-action.html
	./node_modules/.bin/polymer build --entrypoint elements/ewt-custom-action/index.html --shell elements/ewt-custom-action/ewt-custom-action.html $(POLYMER_BUILD_ARGS)
	mkdir -p dist/static/elements/ewt-custom-action
	cp -ar build/default/src/elements/ewt-custom-action/ewt-custom-action.html dist/static/elements/ewt-custom-action/
	cp -ar src/elements/ewt-custom-action/locales.json dist/static/elements/ewt-custom-action/

	# Bundle ewt-i18n.html
	./node_modules/.bin/polymer build --entrypoint elements/ewt-i18n/index.html --shell elements/ewt-i18n/ewt-i18n.html $(POLYMER_BUILD_ARGS)
	mkdir -p dist/static/elements/ewt-i18n
	cp -ar build/default/src/elements/ewt-i18n/ewt-i18n.html dist/static/elements/ewt-i18n/

	# Bundle ewt-session.html
	./node_modules/.bin/polymer build --entrypoint elements/ewt-session/index.html --shell elements/ewt-session/ewt-session.html $(POLYMER_BUILD_ARGS)
	mkdir -p dist/static/elements/ewt-session
	cp -ar build/default/src/elements/ewt-session/ewt-session.html dist/static/elements/ewt-session/

	# Bundle ewt-location-utils.html
	# FIXME Apply polymer bundling to ewt-location-utils
	#./node_modules/.bin/polymer build --entrypoint elements/ewt-location-utils/index.html --shell elements/ewt-location-utils/ewt-location-utils.html $(POLYMER_BUILD_ARGS)
	#cp -ar build/default/src/elements/ewt-location-utils dist/static/elements/
	mkdir -p dist/static/elements/ewt-location-utils
	cp -ar src/elements/ewt-location-utils/ewt-location-utils.html dist/static/elements/ewt-location-utils/

	# Copy assets
	cp -a src/styles dist/static/
	cp -a src/images dist/static/
	mkdir -p dist/static/bower_components/webcomponentsjs
	cp -a src/bower_components/webcomponentsjs/webcomponents-lite.min.js dist/static/bower_components/webcomponentsjs/
	mkdir -p dist/static/bower_components/font-roboto/fonts/roboto/
	cp -a src/bower_components/font-roboto/fonts/roboto/*.ttf dist/static/bower_components/font-roboto/fonts/roboto/
	mkdir -p dist/static/bower_components/font-awesome/fonts/
	cp -a src/bower_components/font-awesome/fonts/*.woff dist/static/bower_components/font-awesome/fonts/
	cp -a src/bower_components/font-awesome/fonts/*.woff2 dist/static/bower_components/font-awesome/fonts/
	cp -a src/bower_components/font-awesome/fonts/*.ttf dist/static/bower_components/font-awesome/fonts/
	cp -a src/locales.json dist/static/
	cp -a src/bower_components/prism dist/static/bower_components/
	mkdir -p dist/static/tiramisu-web/

override_dh_auto_install:
	dh_auto_install --destdir=debian/tmp

override_dh_install:
	dh_install -p ewt-portal --exclude=/static/ usr
	dh_install -p ewt-portal dist/static usr/share/ewt

override_dh_builddeb:
	# Make package compatible with older versions
	dh_builddeb -- -Zgzip -Srle

.PHONY: override_dh_clean override_dh_auto_configure override_dh_auto_build override_dh_auto_install override_dh_install override_dh_builddeb
