#!/usr/bin/make -f

export DH_VERBOSE = 1

export HOME=$(CURDIR)/debian/fakehomenpm
export PATH:=$(PATH):$(CURDIR)/node_modules/bower/bin:$(CURDIR)/node_modules/grunt/bin

%:
	dh $@

override_dh_auto_build:
	mkdir -p $(CURDIR)/debian/fakehomenpm/.sbt/launchers/1.4.6
	cp $(CURDIR)/debian/sbt-launch-1.4.6.jar $(CURDIR)/debian/fakehomenpm/.sbt/launchers/1.4.6/sbt-launch.jar
	npm install bower
	npm install grunt
	./sbt stage -Duser.home=$(HOME) -Dgraal.CompilationFailureAction=Silent
	cd frontend && npm install --legacy-peer-deps

execute_after_dh_install:
	cp package/thehive.default debian/thehive/etc/default/thehive
	# thehive only works with java 8 or java 11. Force the usage of
	# java 11
	for script in thehive migrate cloner; do \
		(cat debian/headers; tail -n +2 debian/thehive/usr/share/thehive/bin/$${script}) >debian/thehive/usr/share/thehive/bin/$${script}.tmp; \
		mv debian/thehive/usr/share/thehive/bin/$${script}.tmp debian/thehive/usr/share/thehive/bin/$${script}; \
		chmod 755 debian/thehive/usr/share/thehive/bin/$${script}; \
	done
