#!/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

# main packaging script based on dh7 syntax
%:
	dh $@ --with python2,systemd --fail-missing

override_dh_clean:
	dh_clean --exclude="corosync.conf.orig"
	rm -rf build pcs/usage.pyc pcs/bash_completion.d.pcs pcs.egg-info

override_dh_auto_build:
	echo "Bundler decoupled from build system"

override_dh_auto_install:
	mkdir -p $(CURDIR)/debian/tmp
	# Make pcs
	$(MAKE) install \
		PREFIX=/usr \
		DESTDIR=$(CURDIR)/debian/tmp \
		PYTHON_SITELIB=/usr/lib/python2.7/dist-packages \
		install_settings=true
	# Make pcsd
	$(MAKE) install_pcsd \
		PREFIX=/usr \
		DESTDIR=$(CURDIR)/debian/tmp \
		PYTHON_SITELIB=/usr/lib/python2.7/dist-packages \
		BUILD_GEMS=false \
		systemddir=/lib/systemd \
		SYSTEMCTL_OVERRIDE=true

	# Always install the init script
	install -m 755 -D pcsd/pcsd.debian $(CURDIR)/debian/tmp/etc/init.d/pcsd

	# remove embedded fonts
	set -e && cd $(CURDIR)/debian/tmp/usr/share/pcsd/public/css && \
	for ttf in LiberationSans-Bold.ttf LiberationSans-BoldItalic.ttf \
		   LiberationSans-Italic.ttf LiberationSans-Regular.ttf; \
	do \
		ln -sf /usr/share/fonts/truetype/liberation/$$ttf $$ttf; \
	done

override_dh_python2:
	dh_python2 -p pcs --skip-private
	dh_python2 -p pcs /usr/share/pcsd

override_dh_installinit:
	dh_installinit --onlyscripts --name=pcsd

override_dh_compress:
	# make non-scripts non-executable
	chmod -x $(CURDIR)/debian/pcs/usr/share/pcsd/public/css/* || : # ignore *.ttf symlinks
	chmod -x $(CURDIR)/debian/pcs/etc/pam.d/pcsd
	chmod -x $(CURDIR)/debian/pcs/usr/lib/python*/*-packages/pcs/settings.py
	chmod -x $(CURDIR)/debian/pcs/usr/share/bash-completion/completions/pcs
	chmod a+x $(CURDIR)/debian/pcs/usr/share/pcsd/pcsd.debian
	chmod a+x $(CURDIR)/debian/pcs/usr/share/pcsd/pcsd.service-runner
	chmod 755 $(CURDIR)/debian/pcs/usr/share/pcsd/public/css/images/
	dh_compress
