#!/usr/bin/make -f

#export DH_VERBOSE=1

# ar/ranlib don't work with LTO objects -> optimize=-lto
export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto
export DEB_CFLAGS_MAINT_APPEND = -fPIC

override_dh_auto_configure:
	# backup files
	[ -f okcompilers/c.orig ] || cp okcompilers/c okcompilers/c.orig
	[ -f okcompilers/cpp.orig ] || cp okcompilers/cpp okcompilers/cpp.orig
	[ -f okcompilers/archivers.orig ] || cp okcompilers/archivers okcompilers/archivers.orig
	# reproducible build - workaround: disable duplicit implementation
	[ -f crypto_core/hsalsa20/ref2/api.h ] && mv -f crypto_core/hsalsa20/ref2/api.h crypto_core/hsalsa20/ref2/api.h.orig
	[ -f crypto_hashblocks/sha256/ref/api.h ] && mv -f crypto_hashblocks/sha256/ref/api.h crypto_hashblocks/sha256/ref/api.h.orig
	[ -f crypto_hashblocks/sha512/ref/api.h ] && mv -f crypto_hashblocks/sha512/ref/api.h crypto_hashblocks/sha512/ref/api.h.orig
	# configure compiler, disable c++ compiler, configure ar
	echo '$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)' > okcompilers/c
	true > okcompilers/cpp
	echo '$(AR)' > okcompilers/archivers

override_dh_auto_build:
	./do
	echo 'log'
	echo '==='
	cat build/debianbuildhost/log
	echo 'data'
	echo '===='
	cat build/debianbuildhost/data | grep -v 'cycles' | grep -v 'bytes'

override_dh_install:
	dh_install -plibnacl-dev build/debianbuildhost/lib/*/libnacl.a '/usr/lib/$(DEB_HOST_MULTIARCH)'
	dh_install -plibnacl-dev build/debianbuildhost/lib/*/librandombytes.a '/usr/lib/$(DEB_HOST_MULTIARCH)'
	dh_install -plibnacl-dev build/debianbuildhost/include/*/crypto_*.h '/usr/include/nacl'
	dh_install -plibnacl-dev build/debianbuildhost/include/*/randombytes.h '/usr/include/nacl'
	dh_install

%:
	dh $@

override_dh_auto_clean:
	mv -f okcompilers/c.orig okcompilers/c || :
	mv -f okcompilers/cpp.orig okcompilers/cpp || :
	mv -f okcompilers/archivers.orig okcompilers/archivers || :
	mv -f crypto_core/hsalsa20/ref2/api.h.orig crypto_core/hsalsa20/ref2/api.h || :
	mv -f crypto_hashblocks/sha256/ref/api.h.orig crypto_hashblocks/sha256/ref/api.h || :
	mv -f crypto_hashblocks/sha512/ref/api.h.orig crypto_hashblocks/sha512/ref/api.h || :
	rm -rf build
	dh_auto_clean