#!/usr/bin/make -f

SHELL := sh -e
export DEB_LDFLAGS_MAINT_STRIP=-Wl,-Bsymbolic-functions
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/default.mk

%:
	dh ${@}

override_dh_auto_clean:
	dh_auto_clean
	rm -f usr/lib/pkcs11/api/pkcs11

override_dh_auto_configure:
	dh_auto_configure -- --enable-tpmtok --with-systemd=/lib/systemd/system ac_cv_path_CHGRP=true $(if $(filter armhf, $(DEB_HOST_ARCH)), --disable-locks)

override_dh_auto_install:
	dh_auto_install

	# create links to .so-files in usr/lib
	( cd debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) && \
	 for FILE in ./opencryptoki/*.so.* ./opencryptoki/stdll/*.so.*; \
	 do \
		ln -s $$FILE ; \
	 done \
	)

	# Remove broken symlinks in /usr/lib/pkcs11/
	cd debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkcs11 && rm -f \
		libopencryptoki.so PKCS11_API.so

	# Removing useless files
	rm -rf debian/tmp/etc/ld.so.conf.d
	rm -rf debian/tmp/etc/init.d
	rm -f debian/tmp/usr/include/Makefile*
	rm -f debian/tmp/usr/include/*/Makefile*
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/opencryptoki/*.la \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/opencryptoki/stdll/*.la
	rm -f debian/tmp/lib/systemd/system/tmpfiles.conf

override_dh_install:
	dh_install --fail-missing  --sourcedir=debian/tmp

override_dh_installinit:
	dh_installinit -p opencryptoki --name pkcsslotd

get-orig-source:
	cd $(dir $(firstword $(MAKEFILE_LIST))).. && \
	uscan --force-download --repack --rename --destdir $(CURDIR)
