#!/usr/bin/make -f
# debian/rules makefile that uses debhelper.
#
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND = -DUSE_GETTEXT -DNDEBUG -Wall -fomit-frame-pointer
LIBCFLAGS =
LIBLDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)
INSTALL_PROGRAM = install -p -m 755

# by default compile RHash with openssl runtime linking
ifeq (,$(findstring nossl,$(DEB_BUILD_OPTIONS)))
ifneq (,$(findstring ssldynamic,$(DEB_BUILD_OPTIONS)))
	LIBCFLAGS += -DUSE_OPENSSL
	LIBLDFLAGS += -lcrypto
else
	LIBCFLAGS  += -DOPENSSL_RUNTIME -rdynamic
	LIBLDFLAGS += -ldl
endif
endif

%:
	dh $@

override_dh_auto_build:
	# Compile static/shared libraries and the program.
	dh_auto_build -- lib-static lib-shared build-shared CFLAGS="$(CPPFLAGS) $(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
		LIBCFLAGS="$(LIBCFLAGS)" LIBLDFLAGS="$(LIBLDFLAGS)"

override_dh_auto_test:
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
	# note: static and shared libraries will be built if not exist yet
	$(MAKE) test-static-lib CFLAGS="$(CPPFLAGS) $(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
		LIBCFLAGS="$(LIBCFLAGS)" LIBLDFLAGS="$(LIBLDFLAGS)"
	$(MAKE) test-shared CFLAGS="$(CPPFLAGS) $(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
		LIBCFLAGS="$(LIBCFLAGS)" LIBLDFLAGS="$(LIBLDFLAGS)"
endif

override_dh_auto_install:
	# Install the program and its translation strings
	$(MAKE) PREFIX=/usr DESTDIR=$(CURDIR)/debian/rhash INSTALL_PROGRAM="$(INSTALL_PROGRAM)" install-shared install-gmo
	# Install static library and so link
	$(MAKE) -C librhash PREFIX=/usr DESTDIR=$(CURDIR)/debian/librhash-dev install-lib-static install-so-link
	# Install shared library
	$(MAKE) PREFIX=/usr DESTDIR=$(CURDIR)/debian/librhash0 install-lib-shared

override_dh_strip:
	dh_strip -plibrhash0 --dbgsym-migration="librhash0-dbg (<< 1.3.5-1~)"
	dh_strip --remaining-packages

override_dh_auto_clean:
	$(MAKE) distclean
