#!/usr/bin/make -f

package		:= libopendbx1
backends		:= firebird mssql mysql pgsql 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)

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


CFLAGS = -Wall -pedantic -std=c99 -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif


build:
	dh_testdir

	CPPFLAGS="${includes} ${CPPFLAGS}" \
	CFLAGS="${CFLAGS}" \
	./configure ${CONFFLAGS} \
		--prefix=/usr \
		--with-backends="${backends}"

	$(MAKE)
	touch build-stamp


install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

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



binary-arch: build install
	dh_testdir
	dh_testroot

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

	dh_install -p${package} "${tmpdir}/usr/lib/libopendbx.so.*" "usr/lib/"
	dh_install -p${package} "${tmpdir}/usr/share/locale/*" "usr/share/locale/"
	dh_install -p${package}-dev "${tmpdir}/usr/include/odbx.h" "usr/include/"
	dh_install -p${package}-dev "${tmpdir}/usr/lib/libopendbx.so" "${tmpdir}/usr/lib/libopendbx.*a" "usr/lib/"
	dh_install -p${package}-dev "opendbx.pc" "usr/lib/pkgconfig/"
	dh_link -p${package}-dev "usr/share/doc/${package}" "usr/share/doc/${package}-dev"
	@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_link
	dh_strip
	dh_compress
	dh_fixperms

	dh_makeshlibs --exclude=backend
	dh_installdeb
	dh_shlibdeps
	dh_md5sums

	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


binary-indep:


binary: binary-arch binary-indep


clean:
	rm -f build-stamp
	rm -f debian/files

	dh_testdir
	dh_testroot

	-$(MAKE) distclean
	autoreconf --force

	dh_clean


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

