#!/usr/bin/make -f

#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# crossbuild
ifneq  ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
include /usr/share/dpkg/buildtools.mk
endif

override_dh_installsystemd:
	dh_installsystemd -ptinysshd tinysshd@.service
	dh_installsystemd -ptinysshd tinysshd.socket

override_dh_auto_install:
	echo >/dev/null

override_dh_auto_configure:
	# disable some case tests, to allow reproducible build
	( cd tinyssh-tests; [ -f opentest.c.orig ] || ( mv opentest.c opentest.c.orig; ln -s emptytest.c opentest.c; ) )
	( cd tinyssh-tests; [ -f dropuidgidtest.c.orig ] || ( mv dropuidgidtest.c dropuidgidtest.c.orig; ln -s emptytest.c dropuidgidtest.c; ) )
	( cd tinyssh-tests; [ -f subprocess_authtest.c.orig ] || ( mv subprocess_authtest.c subprocess_authtest.c.orig; ln -s emptytest.c subprocess_authtest.c; ) )
	# backup files
	[ -f 'conf-cc.orig' ] || cp 'conf-cc' 'conf-cc.orig'
	[ -f 'conf-ar.orig' ] || cp 'conf-ar' 'conf-ar.orig'
	echo '$(CC)' > conf-cc
	echo '$(AR)' > conf-ar

override_dh_auto_build:
ifneq  ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	make cross-compile || { cat build/log; exit 1; }
else
	dh_auto_build || { cat build/log; exit 1; }
endif

override_dh_auto_clean:
	dh_auto_clean
	mv -f 'tinyssh-tests/opentest.c.orig' 'tinyssh-tests/opentest.c' || :
	mv -f 'tinyssh-tests/dropuidgidtest.c.orig' 'tinyssh-tests/dropuidgidtest.c' || :
	mv -f 'tinyssh-tests/subprocess_authtest.c.orig' 'tinyssh-tests/subprocess_authtest.c' || :
	mv -f 'conf-cc.orig' 'conf-cc' || :
	mv -f 'conf-ar.orig' 'conf-ar' || :

%:
	dh $@
