#!/usr/bin/make -f

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

confflags += --with-compile-warnings=no		\
	     --enable-tracing 			\
	     --enable-stderr-logging 		\
	     --with-version-check=partial 	\
	     --with-sudo=yes

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 += -g

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

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

# Use soft-float and thumb mode if it enabled. 
ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
	THUMB_CFLAGS = -mthumb
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

ifneq (,$(findstring maemo-launcher,$(DEB_BUILD_OPTIONS)))
	LAUNCHER = yes
endif

ifeq "$(strip $(LAUNCHER))" "yes"
	LAUNCHER_DEPENDS = -Vlauncher:Depends="maemo-launcher"
	LAUNCHER_CFLAGS = -shared
	LAUNCHER_LDFLAGS = -shared -export-dynamic
endif

configure: configure-stamp
configure-stamp:
	dh_testdir

	NOCONFIGURE=1 ./autogen.sh

	./configure $(confflags) \
		--prefix=/usr --sysconfdir=/etc --localstatedir=/var

	touch configure-stamp


build: build-stamp
build-stamp: configure-stamp 
	dh_testdir
	$(MAKE) \
	MAEMO_LAUNCHER_CFLAGS="$(LAUNCHER_CFLAGS)" \
	MAEMO_LAUNCHER_LDFLAGS="$(LAUNCHER_LDFLAGS)" \
	THUMB_CFLAGS="$(THUMB_CFLAGS)"
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	-$(MAKE) clean
	-if [ -f Makefile ] ; then $(MAKE) distclean ; fi

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
ifeq "$(strip $(LAUNCHER))" "yes"
	mv $(CURDIR)/debian/tmp/usr/bin/osso-backup \
		$(CURDIR)/debian/tmp/usr/bin/osso-backup.launch
	ln -s /usr/bin/maemo-invoker \
		$(CURDIR)/debian/tmp/usr/bin/osso-backup
endif

binary-arch: install
	dh_testdir
	dh_testroot
	dh_install --sourcedir=debian/tmp
	dh_installchangelogs 
	dh_installdocs README
	dh_link
	dh_strip --dbg-package=osso-backup
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol -- $(LAUNCHER_DEPENDS)
	dh_md5sums
	dh_builddeb

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