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

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

%:
	dh $@

README_CFGS := workers.ini database.ini client.conf
ifeq (yes,$(shell dpkg-vendor --is Debian && echo yes))
# let's limit the removal of the webUI to actual Debian,
# rather than also inflicting this on downstreams.
# Downstreams that want to have the webUI split out should ask to be included (patches welcome)
SUITE := $(shell dpkg-parsechangelog -S Distribution)
endif
ifneq (,$(filter $(SUITE),stable testing unstable trixie-backports))
export DEB_BUILD_PROFILES += pkg.openqa.noserver
else
README_CFGS += openqa.ini
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 [ -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
	# install READMEs in /etc/.../*.d/ dirs
	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

	dh_apparmor --profile-name=usr.share.openqa.script.openqa -p openqa
	dh_apparmor --profile-name=usr.share.openqa.script.worker -p openqa-worker

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 (,$(filter pkg.openqa.noserver,$(DEB_BUILD_PROFILES)))
	# 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
	# 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/usr/share/openqa/public/fonts/

execute_after_dh_fixperms:
	chmod -x debian/openqa/etc/logrotate.d/openqa
	chmod +x debian/openqa/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

override_dh_auto_test: setup_node_modules
ifneq (,$(filter pkg.openqa.noserver,$(DEB_BUILD_PROFILES)))
	: # skip tests when we do a partial (noserver) build
else
	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
	mv tools/unstable_tests.txt.bak tools/unstable_tests.txt
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)
