#!/usr/bin/make -f
# debian/rules makefile that uses debhelper.
#
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
CFLAGS = -DUSE_GETTEXT -DNDEBUG -D_FORTIFY_SOURCE=2 -Wall -fomit-frame-pointer $(shell dpkg-buildflags --get CFLAGS)
LDFLAGS= $(shell dpkg-buildflags --get LDFLAGS)
LIBCFLAGS =
LIBLDFLAGS = $(LDFLAGS)
INSTALL_PROGRAM = install -p -m 755
DESTDIR = $(CURDIR)/debian/tmp
LD_LIB=$(DESTDIR):$(LD_LIBRARY_PATH)

# see Debian Policy Manual - source packages
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

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

# using debhelper >= 8 to build the package
%:
	dh $@

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

override_dh_auto_build:
override_dh_auto_test:
	$(MAKE) test-static-lib CFLAGS="$(CFLAGS)" LIBLDFLAGS="$(LIBLDFLAGS)"
	$(MAKE) test-shared CFLAGS="$(CFLAGS)" LIBLDFLAGS="$(LIBLDFLAGS)" LD_LIBRARY_PATH="$(LD_LIB)"

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
	$(MAKE) PREFIX=/usr DESTDIR=$(CURDIR)/debian/librhash-dev install-lib-static
	# Install shared library
	$(MAKE) PREFIX=/usr DESTDIR=$(CURDIR)/debian/librhash0 install-lib-shared

override_dh_strip:
	dh_strip -plibrhash0 --dbg-package=librhash0-dbg
	dh_strip

override_dh_auto_clean:
	$(MAKE) distclean
