#!/usr/bin/make -f
# Made with the aid of debmake, by Christoph Lameter,
# based on the sample debian/rules file for GNU hello by Ian Jackson.
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# This is the debhelper compatibility version to use.
export DH_COMPAT=4

# This has to be exported to make some magic below work.
export DH_OPTIONS

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

objdir = $(CURDIR)/obj-$(DEB_BUILD_GNU_TYPE)

tmp = $(shell pwd)/debian/tmp
archlib=`$(PERL) -MConfig -e 'print $$Config{installarchlib}'`
perlconfig=INSTALLARCHLIB=$(tmp)$(archlib) \
INSTALLDIRS=perl INSTALLMAN1DIR=$(tmp)/usr/share/man/man1 \
INSTALLMAN3DIR=$(tmp)/usr/share/man/man3
INSTALLPRIVLIB=$(tmp)/usr/lib/perl5

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	PERLOPTIMIZE='OPTIMIZE="-O2 -g -Wall"'
else
	PERLOPTIMIZE=""
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

ifndef PERL
PERL=/usr/bin/perl
endif


-include debian/debiandirs

debian/debiandirs: admin/debianrules
	perl -w admin/debianrules echodirs > debian/debiandirs

configure: configure-stamp
configure-stamp:
	dh_testdir

	# Apply Debian specific patches
	if test ! -f patch-stamp; then \
		for patch in $(CURDIR)/debian/patches/*.diff ;\
		do \
		echo APPLYING PATCH\: $${patch##*/};\
			patch -p1 < $$patch ;\
		done ;\
		touch patch-stamp ;\
	fi

	# KDE CVS does not have aclocal.m4 or configure
	if test ! -f configure; then \
		$(MAKE) -f admin/Makefile.common ;\
	fi

	# make build directory

	# run configure with build tree $(objdir)
	AWK=gawk \
	./configure $(configkde) --with-java=/usr/lib/fjsdk --with-pythondir=/usr/lib/python2.3/site-packages DO_NOT_COMPILE='smoke kalyptus xparts'
	touch configure-stamp

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

	$(MAKE)  
	$(MAKE) -C dcoppython  
	$(MAKE) -C dcopjava 
	cd dcopperl && $(PERL) Makefile.PL $(perlconfig) ; 
	make -C dcopperl $(PERLOPTTIMIZE)
	touch build-stamp

debian-clean:
	dh_testdir
	dh_testroot

	dh_clean

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp debian/debiandirs

	# Remove Debian specific patches
	if test -f patch-stamp; then \
		for patch in $(CURDIR)/debian/patches/*.diff ;\
		do \
			echo REMOVING PATCH\: $${patch##*/};\
			patch -p1 -R < $$patch ;\
		done ;\
		rm -f patch-stamp ;\
	fi

	# Remove build tree
	#rm -rf $(objdir)

	# if Makefile exists run distclean
	if test -f Makefile; then \
		$(MAKE) distclean; \
	fi

	dh_clean


install: build

	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Install lintian overrides.
	#$(INSTALL_LINTIAN_OVERRIDES)

	# Main install.
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C dcoppython install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C dcopjava install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C dcopperl pure_install PREFIX=$(CURDIR)/debian/tmp/usr
#	$(MAKE) -C kalyptus install DESTDIR=$(CURDIR)/debian/tmp
	dh_install --list-missing


# 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_installlogrotate
#	dh_installemacsen
#	dh_installcron
#	dh_installinfo
	dh_undocumented
	dh_installman
	dh_strip
	dh_link
	dh_compress -X.docbook -X.css -X-license -X.dcl -X.bz2
	dh_fixperms 
	dh_makeshlibs -V
	dh_installdeb
	dh_perl
	dh_shlibdeps -ldebian/libdcopc1/usr/lib:debian/libqtc1/usr/lib:debian/libkdec1/usr/lib:debian/libkjsembed1/usr/lib:debian/libkdexparts1/usr/lib:debian/qtjava/usr/lib:debian/kdejava/usr/lib
	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 install configure

