#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# 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)
DEB_HOST_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS := $(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -DOBEXFTP_DEBUG=5
endif

bindings_configure_flags=
static_build_flags=
compile_shared_libs=yes
COMPILE_PERL_BINDING=yes
COMPILE_PYTHON_BINDING=yes
COMPILE_RUBY_BINDING=yes
COMPILE_TCL_BINDING=no
ifeq ($(COMPILE_PERL_BINDING),yes)
	bindings_configure_flags += --enable-perl
	compile_shared_libs=yes
else
	bindings_configure_flags += --disable-perl
endif
ifeq ($(COMPILE_PYTHON_BINDING),yes)
	bindings_configure_flags += --enable-python
	compile_shared_libs=yes
else
	bindings_configure_flags += --disable-python
endif
ifeq ($(COMPILE_RUBY_BINDING),yes)
	bindings_configure_flags += --enable-ruby
	compile_shared_libs=yes
	rubyvendorarchdir=$(shell ruby -e "puts RbConfig::CONFIG['vendorarchdir']")
else
	bindings_configure_flags += --disable-ruby
endif
ifeq ($(COMPILE_TCL_BINDING),yes)
	CFLAGS += -I/usr/include/tcl8.4
	bindings_configure_flags += --enable-tcl
	compile_shared_libs=yes
else
	bindings_configure_flags += --disable-tcl
endif

ifneq ($(compile_shared_libs),yes)
	static_build_flags += --enable-static --disable-shared --with-pic
endif

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif

PERL_ARCHLIB := $(shell perl -MConfig -e 'print $$Config{vendorarch}')

configure-stamp:
	dh_autoreconf
	CPPFLAGS="$(CPPFLAGS)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
	    ./configure $(CROSS) \
	        --prefix=/usr --mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
	        $(bindings_configure_flags) \
	        $(static_build_flags) \
	        --disable-rpath --enable-swig
	touch configure-stamp

build-indep: build
build-arch: build
build: build-stamp
build-stamp: configure-stamp
	$(MAKE)
	doxygen
	touch build-stamp

clean:
	rm -f build-stamp configure-stamp
	rm -rf doc/api
	rm -f debian/libobexftp-perl.install
	$(MAKE) -C doc clean || true
	$(MAKE) distclean || true
	rm -f swig/python/__init__.py[co]
	dh_autoreconf_clean
	dh_clean

install: build
	dh_prep
	dh_installdirs
	#sitearchdir needs to be prefixed with $(DESTDIR) with ruby2.1
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp mandir=/usr/share/man INSTALLDIRS=vendor sitearchdir=$(CURDIR)/debian/tmp/$(rubyvendorarchdir)

binary-indep: build install

binary-arch: build install
	sed -e 's;@PERL_ARCHLIB@;$(PERL_ARCHLIB);g;' $(CURDIR)/debian/libobexftp-perl.install.in > $(CURDIR)/debian/libobexftp-perl.install
	dh_install --sourcedir=debian/tmp --autodest --fail-missing -X.la
	dh_installdocs -XCOPYING -XINSTALL -Xobexftp.1
	dh_installman
	dh_installchangelogs ChangeLog
	dh_compress
	dh_link
#running dh_fixperms is needed here to fix permission of the perl module
	dh_fixperms
	dh_strip

ifeq ($(COMPILE_PERL_BINDING),yes)
	find $(CURDIR)/debian/*/$(PERL_ARCHLIB) -name *.so -type f -exec chrpath -d {} \;
#run it again to make sure that permissions are correct
	dh_perl -plibobexftp-perl
	dh_installexamples -plibobexftp-perl examples/perl_*.pl
endif

ifeq ($(COMPILE_PYTHON_BINDING),yes)
	dh_python2 -ppython-obexftp
	dh_installexamples -ppython-obexftp examples/python_*.py
endif

ifeq ($(COMPILE_RUBY_BINDING),yes)
	dh_installexamples -pruby-obexftp examples/ruby_*.rb
endif

ifeq ($(COMPILE_TCL_BINDING),yes)
	dh_installexamples -plibobexftp0-dev examples/tcl_*.tcl
endif

	dh_installexamples -plibobexftp0-dev examples/c_example*.c
	chmod 0644 debian/libobexftp0-dev/usr/share/doc/libobexftp0-dev/examples/c_example*.c

	dh_fixperms

ifeq ($(compile_shared_libs),yes)
	dh_makeshlibs
	dh_shlibdeps -L libbfb0 -L libmulticobex1 -L libobexftp0 \
	             -l debian/libbfb0/usr/lib:debian/libmulticobex1/usr/lib:debian/libobexftp0/usr/lib
else
	dh_shlibdeps
endif

	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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