#!/usr/bin/make -f
# -*- makefile -*-

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

CFLAGS = -Wall -g

# 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)

# DEB_BUILD_OPTIONS
################################################################################

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

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
 CFLAGS += -ggdb3
endif

# parallel
ifneq (,$(findstring parallel,$(DEB_BUILD_OPTIONS)))
 CELLINFO_MAKEFLAGS += -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

# nostrip is handled by dh_strip

################################################################################

config: config-stamp
config-stamp:
	dh_testdir

	if [ -r /usr/share/misc/config.sub ] && [ ! -f config.sub.backup ]; then \
		mv -v config.sub config.sub.backup; \
		cp -f /usr/share/misc/config.sub $(CURDIR)/config.sub; \
	fi

	if [ -r /usr/share/misc/config.guess ] && [ ! -f config.guess.backup ]; then \
		mv -v config.guess config.guess.backup; \
		cp -f /usr/share/misc/config.guess $(CURDIR)/config.guess; \
	fi

	touch $@

config-clean:
	dh_testdir
	dh_testroot

	if [ -f config.sub.backup ]; then \
		mv -fv config.sub.backup config.sub; \
	fi

	if [ -f config.guess.backup ]; then \
		mv -fv config.guess.backup config.guess; \
	fi

	$(RM) config-stamp configure-stamp

configure: configure-stamp
configure-stamp:
	dh_testdir
  
	./autogen.sh
	./configure \
		--host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--mandir=/usr/share/man \
		$(conf_opt) \
		CFLAGS="$(CFLAGS)" LDFLAGS="-Wl,-z,defs,--as-needed"

	touch $@

build: config build-stamp
build-stamp: configure
	dh_testdir

	$(MAKE) $(CELLINFO_MAKEFLAGS)
	
	touch $@

distclean:
#	# touch config.status to prevent execution of autoconf
	[ ! -f Makefile ] || ( touch config.status && $(MAKE) distclean || true )

clean: config-clean distclean
	dh_testdir
	dh_testroot
	
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_link -i
	dh_installchangelogs -p celltowerinfo NEWS
	dh_installdocs -p celltowerinfo
	dh_installexamples -p celltowerinfo
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_link -a
	dh_installinit -a
	dh_installman -a
	dh_installmime -a
	dh_install -a --sourcedir=debian/tmp
	chmod +x $(CURDIR)/debian/maemo-optify
	$(CURDIR)/debian/maemo-optify
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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