#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DOC_PKG_DIR = $(CURDIR)/debian/libcrypto++-doc/

CXXFLAGS+=-DCRYPTOPP_INIT_PRIORITY=250 -DCRYPTOPP_NO_UNALIGNED_DATA_ACCESS \
	  -DNDEBUG

override_dh_auto_clean:
	dh_auto_clean
	rm -f $(CURDIR)/libcrypto++.pc.in $(CURDIR)/config.log
	rm -rf $(CURDIR)/html-docs/
	# these will be re-generated
	rm -f $(CURDIR)/debian/libcrypto++6.links \
		$(CURDIR)/debian/libcrypto++-dev.links

override_dh_auto_configure:
	rm -f $(CURDIR)/GNUmakefile
	cp -f $(CURDIR)/debian/autotools/configure.ac \
		$(CURDIR)/debian/autotools/libcrypto++.pc.in \
		$(CURDIR)/debian/autotools/Makefile.am \
		$(CURDIR)
	autoreconf --verbose --force --install
	dh_auto_configure

override_dh_auto_build-indep:
	doxygen

override_dh_auto_install:
	# use automake
	dh_auto_install
	# install test files
	mkdir -p $(CURDIR)/debian/tmp/usr/share/crypto--/
	cp -a $(CURDIR)/TestVectors $(CURDIR)/debian/tmp/usr/share/crypto--/
	chmod a-x $(CURDIR)/debian/tmp/usr/share/crypto--/TestVectors/*.txt
	cp -a $(CURDIR)/TestData $(CURDIR)/debian/tmp/usr/share/crypto--/
	# install pkg-config file
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/${DEB_HOST_MULTIARCH}/pkgconfig/
	cp -a $(CURDIR)/*.pc \
		$(CURDIR)/debian/tmp/usr/lib/${DEB_HOST_MULTIARCH}/pkgconfig/
	# generate links files
	sed s/ARCH/${DEB_HOST_MULTIARCH}/g \
		$(CURDIR)/debian/libcrypto++6.links.in \
		>$(CURDIR)/debian/libcrypto++6.links
	sed s/ARCH/${DEB_HOST_MULTIARCH}/g \
		$(CURDIR)/debian/libcrypto++-dev.links.in \
		>$(CURDIR)/debian/libcrypto++-dev.links

override_dh_strip:
	dh_strip --dbg-package=libcrypto++6-dbg

override_dh_auto_test-arch:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
     	# Code to run the package test suite.
	$(CURDIR)/cryptestcwd v
endif

override_dh_auto_test-indep:

override_dh_installdocs:
	dh_installdocs
	if [ -d "$(DOC_PKG_DIR)/usr/share/doc/libcrypto++-doc/html-docs" ]; then \
		mv $(DOC_PKG_DIR)/usr/share/doc/libcrypto++-doc/html-docs \
		$(DOC_PKG_DIR)/usr/share/doc/libcrypto++-doc/html; \
	fi

%:
	dh $@ --parallel

.PHONY: override_dh_auto_clean override_dh_auto_configure \
	override_dh_auto_build-indep override_dh_auto_install \
	override_dh_strip override_dh_installdocs \
	override_dh_auto_test-arch override_dh_auto_test-indep
