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

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

%:
	dh $@

ifeq (openqa-server,$(DEB_SOURCE))
README_CFGS := openqa.ini database.ini
else
README_CFGS := workers.ini client.conf
SKIP_GENERATE_PACKED_ASSETS := 1
endif

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 [ -L "node_modules/_u_s_$$d" ] || ln -s /usr/share/$$d node_modules/_u_s_$$d; done

override_dh_auto_build:
ifeq (openqa-server,$(DEB_SOURCE))
	cat debian/openqa-[cw]*.install >> debian/not-installed
else
	sed -E '/^\s*#/d;/^$$/d;s,^(debian/tmp/)?([^\s]*),\2,;s/ => .*$$//' debian/openqa-server.install >> debian/not-installed
endif
	# ensure that there are no remote references left
	! grep http assets/assetpack.def

override_dh_auto_install: setup_node_modules
	# install READMEs in /etc/.../*.d/ dirs
	echo "README_CFGS == '$(README_CFGS)'"
	for cfg in $(README_CFGS); do \
		ext=$${cfg#*.}; \
		dst_dir=debian/tmp/etc/openqa/$$cfg.d; \
		R_cfg=debian/README.$$cfg; \
		mkdir -p $$dst_dir; \
		if [ -e "$$R_cfg" ]; then \
			cp "$$R_cfg" $$dst_dir/README; \
		else \
			sed -E "s/<CONFNAME>/$$cfg/" debian/README.drop-in_configs.in > $$dst_dir/README; \
		fi; \
	done

	dh_auto_install -- SHELL="/bin/bash" GENERATE_PACKED_ASSETS_FAILS_ON_MISSING_ASSETS=1 SKIP_GENERATE_PACKED_ASSETS=$(SKIP_GENERATE_PACKED_ASSETS)

ifeq (openqa-server,$(DEB_SOURCE))
	dh_apparmor --profile-name=usr.share.openqa.script.openqa -p openqa-server
else
	dh_apparmor --profile-name=usr.share.openqa.script.worker -p openqa-worker
# drop the assets stuff for client/worker build, to avoid upsetting dh_missing
	rm -rf debian/tmp/usr/share/openqa/assets
endif

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%'

ifeq (openqa-server,$(DEB_SOURCE))
	# apache & nginx vhost configs are found in .../sites-available on Debian
	sed -i 's#\bvhosts\.d\b#sites-available#' \
		$(CURDIR)/debian/openqa-server/usr/share/openqa/script/configure-web-proxy \
		$(CURDIR)/debian/openqa-server/etc/*/sites-available/openqa*.conf.template
	# grab local copy of Roboto, as symlinks to other directories don't always work by default
	cp /usr/share/fonts/truetype/roboto-fontface/roboto/Roboto-Regular.ttf debian/openqa-server/usr/share/openqa/public/fonts/

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

# add some tests that fail on Debian to the unstables list
EXTRA_UNSTABLES := t/*/*_obs_* t/03-auth.t t/09-job_clone.t t/24-worker-engine.t t/37-limit_assets.t t/40-script_load_dump_templates.t t/api/02-iso-yaml.t t/api/08-jobtemplates.t t/api/16-webhooks.t
# when building with unshare, these attempts to nest unshare fail
EXTRA_UNSTABLES += t/32-openqa_client-script.t t/40-openqa-clone-job.t t/40-script_openqa-clone-custom-git-refspec.t
# silence tests that don't backport to fastrack
EXTRA_UNSTABLES += t/14-grutasks-git.t t/10-jobs.t t/api/16-webhooks.t t/api/04-jobs.t t/api/09-comments.t
# depend on git
EXTRA_UNSTABLES += t/01-style.t
# extra debris
EXTRA_UNSTABLES += t/api/01-workers.t t/ui/16-tests_dependencies.t t/04-scheduler.t t/43-scheduling-and-worker-scalability.t


override_dh_auto_test: setup_node_modules
ifeq (openqa-server,$(DEB_SOURCE))
	ln -s ../node_modules t/
	cp tools/unstable_tests.txt tools/unstable_tests.txt.bak
	for i in $(EXTRA_UNSTABLES); do echo $$i >> tools/unstable_tests.txt; done
	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 CHECK_GIT_STATUS=0
	mv tools/unstable_tests.txt.bak tools/unstable_tests.txt
else
	: # skip tests when we do a non-server build
endif

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)

execute_before_dh_gencontrol:
	# this allows a fasttrack package (~fto) to depend on the matching backport (~bpo) 
	echo "nearby:Version=$(subst ~fto,~bpo,$(DEB_VERSION))" >> debian/openqa-server.substvars

debian/control: ./debian/generate-control.pl
	$^ > $@

# switch to openqa-server (do this in the debian/latest-server branch)
sw-server:
	./debian/generate-control.pl openqa-server > debian/control
	sed -i -E '1,/5.1764349525/ { /5.1764349525/! s/^(openqa) /\1-server / }; 3i\  * rename source package as `openqa-server` (see README.source)\n' debian/changelog

# switch to openqa (do this in the debian/latest branch)
sw-client:
	./debian/generate-control.pl openqa > debian/control
	sed -i -E '1,/5.1764349525/ { /5.1764349525/! s/^(openqa)-server /\1 / }' debian/changelog
