#!/usr/bin/make -f

# we need our test artifacts too
export DH_GOLANG_INSTALL_ALL := 1

# github.com/miekg/pkcs11/test requires more test parameters
export DH_GOLANG_EXCLUDES := github.com/miekg/pkcs11/test

MODULE_DIR = debian/golang-github-miekg-pkcs11-dev/usr/share/gocode/src/github.com/miekg/pkcs11

%:
	dh $@ --buildsystem=golang --with=golang

override_dh_auto_install:
	dh_auto_install
	# Remove extra license file copied by DH_GOLANG_INSTALL_ALL
	rm -f $(MODULE_DIR)/LICENSE
	# Remove tests & related non-reproducible output
	rm -f $(MODULE_DIR)/hsm.db rm -f $(MODULE_DIR)/*_test.db

# Create token for unit tests in temporary directory

export SOFTHSM2_CONF := $(CURDIR)/debian/softhsm2.conf

$(SOFTHSM2_CONF):
	echo "directories.tokendir = $(CURDIR)/debian/tmp" > debian/softhsm2.conf
	echo "objectstore.backend = file" >> debian/softhsm2.conf
	echo "log.level = INFO" >> debian/softhsm2.conf

override_dh_auto_test: $(SOFTHSM2_CONF)
	mkdir -p debian/tmp
	softhsm2-util --init-token --slot 0  --label test --pin 1234 --so-pin 1234
	dh_auto_test
	rm -rf debian/tmp

override_dh_auto_clean:
	dh_auto_clean
	rm -f $(SOFTHSM2_CONF)
