#!/usr/bin/make -f
# -*- makefile -*-
# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

DATE=$(shell sed -n 's/^DATE\s*=\s*\(.*\)/\1/p' Makefile_start)
MAJORVERSION=$(shell sed -n 's/^MAJORVERSION\s*=\s*\(.*\)/\1/p' Makefile_start)
MINORVERSION=$(shell sed -n 's/^MINORVERSION\s*=\s*\(.*\)/\1/p' Makefile_start)
VERSION       = ${MAJORVERSION}.${MINORVERSION}

DB2MAN=/usr/share/sgml/docbook/stylesheet/xsl/nwalsh/manpages/docbook.xsl
XP=xsltproc -''-nonet

# Set SONAME
SOMAJOR=7
SOMINOR=0
SONAME="libbibutils.so."$(SOMAJOR)

DESTDIR=${CURDIR}/debian/tmp/
INSTALLDIR=${DESTDIR}/usr/bin
LIBINSTALLDIR=${DESTDIR}/usr/lib/$(DEB_HOST_MULTIARCH)

CFLAGSIN=$(shell dpkg-buildflags --get CFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
export CFLAGSIN

LDFLAGSIN=$(shell dpkg-buildflags --get LDFLAGS)
export LDFLAGSIN

%:
	dh $@ --buildsystem=makefile

override_dh_auto_configure:
	@echo Skipping auto_configure
	@echo building bibutils $(VERSION), upstream date $(DATE)

override_dh_auto_build:
	dh_auto_build -Dlib -- -f Makefile.dynamic LIBTARGETIN=libbibutils.so CFLAGSIN="-fPIC ${CFLAGSIN}" SONAME=$(SONAME) SOMINOR=$(SOMINOR) all
	dh_auto_build -Dbin -- -f Makefile.dynamic VERSION=$(VERSION) DATE=$(DATE) all
	# this relies on target clean not removing the shared libs
	dh_auto_build -Dlib -- -f Makefile.static clean
	dh_auto_build -Dlib -- -f Makefile.static RANLIB=ranlib libbibutils.a libbibcore.a
	$(XP) -o bibutils.1 $(DB2MAN) bibutils.dbk
	sed 's/VERSION/${VERSION}/g' packageconfig_start > lib/bibutils.pc

override_dh_auto_install:
	@echo Skipping auto_install

ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
override_dh_auto_test:
	make -C test -f Makefile.static all
	make -C test -f Makefile.static test
endif

override_dh_auto_clean:
	make -C lib -f Makefile.dynamic realclean
	make -C bin -f Makefile.dynamic realclean
	make -C test -f Makefile.dynamic realclean
