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

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

%:
	dh $@

override_dh_auto_build:
	# forget shepherd until it is packaged:
	-rm assets/stylesheets/shepherd.scss t/ui/24-feature-tour.t
	# install files from debian/missing-sources
	mkdir -p node_modules/anser/lib node_modules/d3js_4.13.0
	cp debian/missing-sources/d3js_4.13.0/d3.v4.js node_modules/d3js_4.13.0
	tar -xvzf debian/missing-sources/dagre-d3_0.5.0/v0.5.0.tar.gz -C node_modules dagre-d3-0.5.0/dist/dagre-d3.js
	cp debian/missing-sources/anser_2.0.1/index.js node_modules/anser/lib
	# ensure that there are no remote references left
	! grep http assets/assetpack.def

override_dh_auto_install:
	mkdir -p node_modules
	ln -s /usr/share node_modules/_usr_share
	dh_auto_install -- SHELL="/bin/bash" GENERATE_PACKED_ASSETS_FAILS_ON_MISSING_ASSETS=1

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:
	# remeber, there used to be more disabled tests here, inspired by the RPM spec file.
	# revert the change that introduced this comment if those tests start failing again.

	# FIXME: upstream depends upon a version of perltidy that's not yet in Debian
	-rm ./t/00-tidy.t
	# On debian we can't run test connecting to internet
	-rm ./t/40-script_openqa-clone-custom-git-refspec.t \
		./t/40-openqa-clone-job.t
	# Not enough permission for unshare use
	-rm ./t/32-openqa_client-script.t

	-export CI=1; export OPENQA_TEST_TIMEOUT_SCALE_CI=10 ; \
		PATH=$$PATH:$$(ls -d /usr/lib/postgresql/*/bin) FULLSTACK=1 \
		dh_auto_test -- \
		SHELL="bash -x" PROVE_ARGS='-r -v' CHECKSTYLE=0 TEST_PG_PATH=$(CURDIR)/debian/DB

execute_before_dh_auto_clean:
	-rm -r node_modules/
	-rm -r t/pool*
	-rm -r t/data/openqa/share/factory/other
	-rm -r t/data/openqa/share/factory/foo
	-rm -r t/data/openqa/pool
	-rm test_vars.json
	-rm -r .tidyall.d/
	-rm -r .tidyall
	-rm t/data/openqa/share/factory/hdd/hdd_image.qcow2
	-rm t/data/openqa/share/factory/hdd/00099963-hdd_image.qcow2
	-rm t/data/openqa/share/factory/foo/some-repo
	-rm -r t/data/openqa/testresults/ .sass-cache/ assets/cache
	-rm 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 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 -r $(CURDIR)/debian/DB

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)
