#!/usr/bin/make -f
#export DH_VERBOSE = 1

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

%:
	dh $@

SCRATCH_DIR := $(shell mktemp -d $${TMPDIR:-/tmp}/openqa_build.XXXXXX)

ifneq (,$(filter x-with-npm,$(DEB_BUILD_PROFILES)))
override_dh_gencontrol:
	dh_gencontrol -- -v$(shell dpkg-parsechangelog -SVersion | sed -E 's/^([^-]*-[0-9]*)/\1~~~NPM~~/')
endif

setup_node_modules:
ifneq (,$(filter x-with-npm,$(DEB_BUILD_PROFILES)))
#	this is here to enable test builds to compare with the way upstream would build it
#	NEVER use 'x-with-npm' profile for a real build!
	HOME=$${TMPDIR:-/tmp}/fake_npm_home npm install
	mv node_modules node_modules_npm
	-ls -lR node_modules_npm
endif
	mkdir -p node_modules
	for d in javascript nodejs sass; do [ -e "node_modules/_u_s_$$d" ] || ln -s /usr/share/$$d node_modules/_u_s_$$d; done
	[ -e node_modules/_u_s_ffa ] || ln -s /usr/share/fonts-fork-awesome node_modules/_u_s_ffa

override_dh_auto_build:
	# ensure that there are no remote references left
	! grep http assets/assetpack.def

override_dh_auto_install: setup_node_modules
	dh_auto_install -- SHELL="/bin/bash" GENERATE_PACKED_ASSETS_FAILS_ON_MISSING_ASSETS=1

override_dh_installdocs:
	dh_installdocs -X .gitignore

execute_after_dh_install:
	# make #! lines Debian Perl/Python Policy compliant
	find $(CURDIR)/debian/openqa*/usr/share/openqa -type f -print0 | \
		xargs -r0 sed -i -e '1s%^\(#!/usr/bin/\)env \(perl\|python3\)%\1\2%'
	# apache & nginx vhost configs are found in .../sites-available on Debian
	sed -i 's#\bvhosts\.d\b#sites-available#' \
		$(CURDIR)/debian/openqa/usr/share/openqa/script/configure-web-proxy \
		$(CURDIR)/debian/openqa/etc/*/sites-available/openqa*.conf.template

execute_after_dh_fixperms:
	chmod -x debian/openqa/etc/logrotate.d/openqa
	chmod +x debian/openqa/usr/share/openqa/dbicdh/*/upgrade/*-*/0*-*.pl

override_dh_auto_test: setup_node_modules
	ln -s ../node_modules t/
	-export CONTAINER_TEST=0 ; \
		PATH=$$PATH:$$(ls -d /usr/lib/postgresql/*/bin) \
		PROVE_ARGS='-v' \
		make test-t test-ui test-api SHELL="bash -x" CI=1 OPENQA_TEST_TIMEOUT_SCALE_CI=10 TEST_PG_PATH=${SCRATCH_DIR}/DB

execute_before_dh_auto_clean:
	rm -rf node_modules/
	rm -rf t/pool*
	rm -rf t/data/openqa/share/factory/other
	rm -rf t/data/openqa/share/factory/foo
	rm -rf t/data/openqa/pool
	rm -f test_vars.json
	rm -rf .tidyall.d/
	rm -rf .tidyall
	rm -f t/data/openqa/share/factory/hdd/hdd_image.qcow2
	rm -f t/data/openqa/share/factory/hdd/00099963-hdd_image.qcow2
	rm -f t/data/openqa/share/factory/foo/some-repo
	rm -rf t/data/openqa/testresults/ .sass-cache/ assets/cache
	rm -f assets/assetpack.db job.json t/data/openqa/pool/1/.locked t/data/openqa/pool/1/t/data/openqa/pool/1/.locked t/data/openqa/pool/1/t/data/openqa/pool/1/t/data/openqa/pool/1/.locked t/data/openqa/pool/1/t/data/openqa/pool/1/t/data/openqa/pool/1/worker-log.txt t/data/openqa/pool/1/t/data/openqa/pool/1/worker-log.txt t/data/openqa/share/factory/iso/Core-7.2.iso t/data/openqa/share/tests/opensuse/needles/inst-timezone-text.json t/data/openqa/webui/cache/asset-status.json
	rm -f t/data/openqa/share/factory/hdd/00099963-hdd_image3.qcow2 t/data/openqa/share/factory/hdd/00099963-hdd_image7.qcow2 t/data/openqa/share/factory/hdd/hdd_image2.qcow2 t/data/openqa/share/factory/hdd/hdd_image5.qcow2 t/data/openqa/share/factory/other/00099963-hdd_image3.xml t/data/openqa/share/factory/other/00099963-hdd_image4.xml
	rm -rf ${SCRATCH_DIR}
	# FIXME: no idea why I need to remove this file, as I'm also removing it via a patch, but salsa hates me otherwise
	rm -f assets/stylesheets/cropper.css

pod2man_opts := --errors=stderr --section=1 --center "openQA Documentation" --release 'openQA $(DEB_VERSION_UPSTREAM)'
execute_before_dh_installman:
	make build-manpages -- DESTDIR=debian/tmp OPENQA_VERSION=$(DEB_VERSION_UPSTREAM)
