#!/usr/bin/make -f
# 
# $Id: rules 66 2010-09-12 09:51:36Z robert $
# 
# Uncomment this to turn on verbose mode. 
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS
export BDIR       := BUILD
export FLCOW_PATH := $(CURDIR)/$(BDIR)
export LD_PRELOAD := /usr/lib/fl-cow/libflcow.so:$(LD_PRELOAD)



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


ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CXXFLAGS = -g -Wall -O0
else	
  CXXFLAGS = -g -Wall -Wno-deprecated -O2
endif

LDFLAGS   = -Wl,-z,defs -Wl,--as-needed


export QTDIR=/usr

PKG_TMP=$(CURDIR)/debian/tmp
ifeq (,$(DEB_BUILD_ARCH))
	DEB_BUILD_ARCH=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
endif


configure: $(BDIR)/configure-stamp
$(BDIR)/configure-stamp:  $(QUILT_STAMPFN)
	dh_testdir
	#
	# setup our $(BDIR)
	mkdir  $(BDIR)
	cp -al [!dB]*  d[!e]*  $(BDIR)
	rm -f $(BDIR)/ac-helpers/config.sub $(BDIR)/ac-helpers/config.guess
	ln -s /usr/share/misc/config.sub $(BDIR)/ac-helpers/config.sub
	ln -s /usr/share/misc/config.guess $(BDIR)/ac-helpers/config.guess
	# check if cow-fl works correctly
	echo "###test" >> $(BDIR)/configure && ! cmp -s  $(BDIR)/configure ./configure 
	cd $(BDIR) && autoreconf && autoupdate
	#
	# configure package
	cd $(BDIR) &&  LDFLAGS="$(LDFLAGS)" \
	./configure 	--prefix=/usr  \
			--enable-nls 	\
			--disable-dependency-tracking \
			--disable-maintainer-mode \
			$(confflags)
	touch "$@"

build: $(BDIR)/configure-stamp $(BDIR)/build-stamp
$(BDIR)/build-stamp:
	dh_testdir

	# Create the binaries
	cd $(BDIR) && $(MAKE) "CXXFLAGS=$(CXXFLAGS)"

	# Create the HTML documentation
	cd $(BDIR) && $(MAKE) -C doc/docbook/guide

	cd $(BDIR) && $(MAKE) -C po

	touch "$@"

clean: 	
	dh_testdir
	dh_testroot

	rm -f $(BDIR)/build-stamp $(BDIR)/configure-stamp
	rm -rf $(BDIR)
	dh_clean


install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot

	dh_prep

	# Install binaries and documentation into temporary directory
	cd $(BDIR) && $(MAKE) install DESTDIR=$(PKG_TMP)
	cd $(BDIR) && $(MAKE) -C doc/docbook/guide install DESTDIR=$(PKG_TMP)/usr/share/doc/subcommander/html
	cd $(BDIR) && $(MAKE) -C po install DESTDIR=$(PKG_TMP)

	# Install the application icon (should be done in the upstream Makefile)
	mkdir -p $(PKG_TMP)/usr/share/pixmaps
	install --mode=644 $(BDIR)/pics/subcommander.xpm $(PKG_TMP)/usr/share/pixmaps

	dh_install --list-missing --sourcedir=$(PKG_TMP)

# This single target is used to build all the packages, all at once, or
# one at a time. So keep in mind: any options passed to commands here will
# affect _all_ packages. Anything you want to only affect one package
# should be put in another target, such as the install target.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
#	dh_installexamples
	dh_installmenu
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installcatalogs
#	dh_installpam
#	dh_installmime
#	dh_installinit
	dh_installman
#	dh_installcron
#	dh_installinfo
#	dh_installwm
#	dh_installudev
#	dh_lintian
#	dh_undocumented
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build install
	 $(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

# Any other binary targets build just one binary package at a time.
binary-%: build install
	make -f debian/rules binary-common DH_OPTIONS=-p$*

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