#!/usr/bin/make -f
# -*- makefile -*-
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

# 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 = 
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS_RELEASE = -O0 -g -DNDEBUG $(CFLAGS)
	CFLAGS_DEBUG   = -O0 -g $(CFLAGS)
else
	CFLAGS_RELEASE = -O2 -g -DNDEBUG $(CFLAGS)
	CFLAGS_DEBUG   = -O2 -g $(CFLAGS)
endif

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
CFLAGS+=$(CPPFLAGS)
CXXFLAGS+=$(CPPFLAGS)

BUILD_TYPE = Release
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	BUILD_TYPE = Debug
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(NUMJOBS)
endif

BUILD_FLAGS = -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) \
	      -DCMAKE_C_FLAGS_RELEASE="$(CFLAGS_RELEASE)" -DCMAKE_C_FLAGS_DEBUG="$(CFLAGS_DEBUG)" \
              -DCMAKE_SKIP_RPATH=ON -DCMAKE_VERBOSE_MAKEFILE=ON 
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
	BUILD_FLAGS += -DCMAKE_SYSTEM_NAME=$(shell dpkg-architecture -qDEB_BUILD_ARCH_OS) \
                       -DCMAKE_SYSTEM_PROCESSOR=$(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU) \
                       -DCMAKE_C_COMPILER=$(DEB_BUILD_GNU_TYPE)-gcc
endif

debian/build/CMakeCache.txt:
	mkdir -p debian/build
	cd $(CURDIR)/debian/build && \
	  cmake $(BUILD_FLAGS) ../..

debian/build: debian/build/CMakeCache.txt

debian/build-static/CMakeCache.txt:
	mkdir -p debian/build-static
	cd $(CURDIR)/debian/build-static && \
	  cmake -DBUILD_SHARED_LIBS=OFF -DBUILD_DOCUMENTATION=OFF $(BUILD_FLAGS) ../..

debian/build-static: debian/build-static/CMakeCache.txt

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: debian/build debian/build-static
	dh_testdir
	$(MAKE) -C debian/build $(MAKEFLAGS) openobex-apps
	$(MAKE) -C debian/build-static $(MAKEFLAGS) openobex
	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp: debian/build
	dh_testdir
	$(MAKE) -C debian/build $(MAKEFLAGS) doc
	touch build-indep-stamp

clean:
	dh_testdir
	dh_testroot
	rm -rf debian/build
	rm -rf debian/build-static
	debian/rules unpatch
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) -C debian/build-static install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C debian/build install DESTDIR=$(CURDIR)/debian/tmp


# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_install --sourcedir=debian/tmp --autodest --fail-missing -X share/doc -X share/man
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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