#!/usr/bin/make -f

package			:= libopendbx1
backends		:= firebird mssql mysql pgsql odbc sqlite sqlite3 sybase
includes		:= -I/usr/include/mysql -I/usr/include/postgresql
tmpdir			:= debian/tmp

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
export DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

doxygen_version            := $(shell dpkg-query -W -f="\$${Version}\n" doxygen)

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	CONFFLAGS = --build $(DEB_HOST_GNU_TYPE)
else
	CONFFLAGS = --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif
ifneq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
  with_check := disabled by DEB_BUILD_OPTIONS.
else
  ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
    with_check := disabled because cross-compiling.
  else
    with_check := yes
  endif
endif

ifneq (/usr/share/pkg-kde-tools/bin,$(filter /usr/share/pkg-kde-tools/bin,$(subst :, ,$(PATH))))
    export PATH := /usr/share/pkg-kde-tools/bin:$(PATH)
endif

CFLAGS = $(shell dpkg-buildflags --get CFLAGS) -Wall -pedantic -std=c99
CPPFLAGS = $(shell dpkg-buildflags --get CPPFLAGS)
CXXFLAGS = $(shell dpkg-buildflags --get CXXFLAGS) -Wall -pedantic
LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)

build: build-stamp
build-stamp:
	dh_testdir
	dh_autoreconf
	CFLAGS="${CFLAGS}" \
	CPPFLAGS="${includes} ${CPPFLAGS}" \
	CXXFLAGS="${CXXFLAGS}" \
	LDFLAGS="${LDFLAGS}" \
	./configure ${CONFFLAGS} \
		--prefix=/usr \
		--disable-rpath \
		--with-backends="${backends}"

	$(MAKE)
	ln $(CURDIR)/lib/opendbx/api $(CURDIR)/lib/opendbx/api.dox

ifeq ($(with_check),yes)
	cp $(CURDIR)/debian/odbxtest.site $(CURDIR)/test
	cd test; ./odbxtest.sh
	rm -f $(CURDIR)/test/odbxtest.site
	rm -f $(CURDIR)/debian/testdb
	rm -r $(CURDIR)/test/testresult*
else
	@echo "Testsuite not run: $(with_check)."
endif
	touch build-stamp

build-arch: build-arch-stamp
build-arch-stamp: build-stamp

build-indep: build-indep-stamp
build-indep-stamp: build-stamp

install: install-indep install-arch
install-indep: install-indep-stamp
install-indep-stamp: build-indep-stamp

install-arch: install-arch-stamp
install-arch-stamp:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(MAKE) install DESTDIR="${CURDIR}/${tmpdir}"

binary-arch: install
	dh_testdir -a
	dh_testroot -a

	dh_installdocs -p${package}
	dh_installchangelogs -p${package} ChangeLog
	dh_installman -p${package}-dev ${tmpdir}/usr/share/man/man3/*

	dh_install -p${package} "${tmpdir}/usr/lib/libopendbx.so.*" "usr/lib/"
	dh_install -p${package} "${tmpdir}/usr/lib/libopendbxplus.so.*" "usr/lib/"
	dh_install -p${package} --autodest "${tmpdir}/usr/share/locale/*/*/opendbx.mo"
	dh_install -p${package}-dev "${tmpdir}/usr/include/opendbx/api" "usr/include/opendbx"
	dh_install -p${package}-dev "${tmpdir}/usr/include/opendbx/api.h" "usr/include/opendbx"
	dh_install -p${package}-dev "${tmpdir}/usr/lib/libopendbx.so" "${tmpdir}/usr/lib/libopendbx.a" "usr/lib/"
	dh_install -p${package}-dev "${tmpdir}/usr/lib/libopendbxplus.so" "${tmpdir}/usr/lib/libopendbxplus.a" "usr/lib/"
	dh_install -p${package}-dev "opendbx.pc" "usr/lib/${DEB_HOST_MULTIARCH}/pkgconfig/"
	dh_link -p${package}-dev "usr/share/doc/${package}" "usr/share/doc/${package}-dev"
	dh_link -p${package}-dev "usr/include/opendbx/api.h" "usr/include/odbx.h"
	dh_link -popendbx-utils "usr/share/doc/${package}" "usr/share/doc/opendbx-utils"
	set -e ; \
	for be in ${backends} ; do \
		dh_install  -p${package}-$${be} "${tmpdir}/usr/lib/opendbx/lib$${be}backend.so*" "usr/lib/opendbx/" ; \
		dh_link -p${package}-$${be} "usr/share/doc/${package}" "usr/share/doc/${package}-$${be}" ; \
	done
	dh_installman -popendbx-utils ${tmpdir}/usr/share/man/man1/*
	dh_install -popendbx-utils
	mv $(CURDIR)/debian/opendbx-utils/usr/bin/odbxtest.sh $(CURDIR)/debian/opendbx-utils/usr/bin/odbxtest.master

	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a

	dh_makeshlibs -a --exclude=backend
	dh_installdeb -a
	dh_shlibdeps -a
	dh_md5sums -a

	dh_gencontrol -p${package}
	dh_builddeb -p${package}
	dh_gencontrol -p${package}-dev
	dh_builddeb -p${package}-dev
	set -e ; \
	for be in ${backends} ; do \
		dh_gencontrol -p${package}-$${be} ; \
		dh_builddeb  -p${package}-$${be} ; \
	done
	dh_gencontrol -popendbx-utils
	dh_builddeb -popendbx-utils

binary-indep:
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installchangelogs -i ChangeLog
	dh_install -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_md5sums -i
	dh_gencontrol -i -- -Vdoxygen:Version=$(doxygen_version)
	dh_builddeb -i

binary: binary-arch binary-indep


clean:
	rm -f build*stamp
	rm -f install*stamp
	rm -f debian/files
	rm -rf doc/html
	rm -f doc/*.3
	rm -rf doc/man
	rm -rf doc/xml
	rm -rf doc/doxygen_sqlite3.db
	rm -rf utils/doc/odbx-sql.1
	rm -rf lib/opendbx/api.dox

	dh_testdir
	dh_testroot

	[ ! -f Makefile ] || $(MAKE) distclean
	dh_autoreconf_clean

	dh_clean

.PHONY: build clean binary-indep binary-arch binary install

