#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#DH_VERBOSE = 1

# see FEATURE AREAS in dpkg-buildflags(1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Ensure that we link against all needed libraries (cf. Policy 10.2)
export DEB_LDFLAGS_MAINT_APPEND=-Wl,-z,defs

# Properly support multi-arch triplet paths
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# If your package provides eggs, install egg-info directories instead of real eggs
DEB_PYTHON_INSTALL_ARGS_ALL += --single-version-externally-managed

# main packaging script based on dh7 syntax
%:
	dh $@ --buildsystem=makefile --with python3 --shebang=/usr/bin/python3 --no-parallel

override_dh_missing:
	dh_missing --fail-missing

# Generate configuration files
override_dh_auto_configure:
	./configure

# Make generated documentation reproducible
override_dh_auto_build: export TZ=UTC
override_dh_auto_build:
	dh_auto_build
# Manually invoke building of the asciidoc html documentation
	PATH=$$PATH:$$PWD/doc/toolchain/bin:$$PWD/bin \
	    PYTHONPATH=$$PWD \
	    HOME=$$PWD \
	    CRM_HELP_FILE=$$PWD/doc/crm.8.adoc \
	    fakeroot -- make -C doc all website
	asciidoc -o doc/crm.8.html doc/generated-sources/crm.8.aio.adoc

DOC = debian/tmp/usr/share/doc/crmsh

override_dh_auto_install:
	dh_auto_install -- tmpfilesdir=/usr/lib/tmpfiles.d
# pip always installs in /usr/local
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages
	mv $(CURDIR)/debian/tmp/usr/local/lib/python3*/dist-packages/crmsh* \
           $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages
	find $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages -name __pycache__ -depth -exec rm -r {} \;
	chmod -x $(CURDIR)/debian/tmp/usr/share/bash-completion/completions/crm
# remove redundant files
	rm debian/tmp/usr/lib/firewalld/services/high-availability.xml
	rm -fv $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/*.dist-info/COPYING \
	       $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/*.dist-info/licenses/COPYING
	rm -f $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/*.dist-info/direct_url.json
# move README to doc dir
	mkdir -p $(DOC)
	mv debian/tmp/usr/share/crmsh/tests/README.regression $(DOC)/README.regression
# install documentation
	install -Dm0644 README.md $(DOC)/README.md
	install -Dm0644 doc/crm.8.html $(DOC)/crm.8.html
	install -Dm0644 doc/crmsh_crm_report.8.html $(DOC)/crmsh_crm_report.8.html
# keep crm in /usr/sbin for now
	install -D debian/tmp/usr/local/bin/crm debian/tmp/usr/sbin/crm
	rm debian/tmp/usr/local/bin/crm
# install vim plugin
	install -m 644 -D debian/vim-pcmk.yaml debian/tmp/usr/share/vim/registry/vim-pcmk.yaml
	install -m 644 -D contrib/pcmk-ftdetect.vim debian/tmp/usr/share/vim/addons/ftdetect/pcmk.vim
	install -m 644 -D contrib/pcmk.vim debian/tmp/usr/share/vim/addons/syntax/pcmk.vim
# fix timestamps on patched files for reproducible build
	find debian/tmp -print0 | xargs -0 touch --date=@$${SOURCE_DATE_EPOCH}

# clean with python 3
override_dh_auto_clean:
	python3 setup.py clean -a

override_dh_clean:
	dh_clean build/ dist/* doc/*.8 doc/*.html doc/website-v1/gen/ doc/generated-sources/*
