#!/usr/bin/make -f
# debian/rules file for the kdebindings Debian source package.
# 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 has to be exported to make some magic below work.
export DH_OPTIONS

PACKAGES_WITH_USD_SYMLINK = kdebindings-java libkde3-java libqt3-java \
			    libdcop3-java-dev libdcop3-java \
			    libsmokekde-dev libsmokeqt-dev \
			    kjscmd libkjsembed-dev \

# 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 = -Wall -g

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

objdir = $(CURDIR)/obj-$(DEB_BUILD_GNU_TYPE)
upstream_version=`head -1 $(CURDIR)/debian/changelog | sed -e "s,.*:\([^-]*\).*,\1,"`

ifndef PERL
PERL=/usr/bin/perl
endif

# DCOPPerl is disabled.
#tmp = $(shell pwd)/debian/tmp
#archlib=`$(PERL) -MConfig -e 'print $$Config{installarchlib}'`
#perlconfig=INSTALLDIRS=vendor

-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 -d $(CURDIR)/debian/patches ;\
	then \
		for i in $(CURDIR)/debian/patches/*.diff.uu ;\
		do \
			cd $(CURDIR)/debian/patches ;\
			uudecode "$$i" ;\
			cd $(CURDIR) ;\
		done ;\
		patches=$$(find $(CURDIR)/debian/patches -name *.diff | sort);\
	fi ;\
	if test ! -f patch-stamp; then \
		for patch in $$patches ;\
		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 dist ;\
	fi

	# ensure configure is executable
	chmod +x configure

	# make build directory
	mkdir $(objdir)

	# run configure with build tree $(objdir)
	cd $(objdir) && \
	../configure \
	$(configkde) \
	--with-java=/usr \
	--with-pythondir=/usr/lib/python2.3/site-packages \
	DO_NOT_COMPILE='dcopperl kalyptus qtsharp xparts python'
	# --enable-final

	touch configure-stamp

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

	cd $(objdir) && \
	$(MAKE)

	# build dcopjava even though it's disabled upstream.
	cd $(objdir) && \
	$(MAKE) -C dcopjava 

	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 -d $(CURDIR)/debian/patches ;\
	then \
		patches=$$(find $(CURDIR)/debian/patches -name *.diff | sort -r);\
	fi ;\
	if test -f patch-stamp; then \
		for patch in $$patches ;\
		do \
			echo REMOVING PATCH\: "$${patch##*/}" ;\
			patch -p1 -R < "$$patch" ;\
		done ;\
		for i in $(CURDIR)/debian/patches/*.diff.uu ;\
		do \
			 rm -f "$${i%%.uu}" ;\
		done ;\
		rm -f patch-stamp ;\
	fi

	# Remove build tree
	rm -rf $(objdir)

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

	-rm -f kjsembed/docs/embedding/simple-embed/Makefile

	dh_clean


install: build

	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	dh_link

	# Main install.
	cd $(objdir) && \
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	cd $(objdir) && \
	$(MAKE) -C dcopjava install DESTDIR=$(CURDIR)/debian/tmp
#DCOPPerl is disabled.
#	$(MAKE) -C dcopperl pure_install PREFIX=$(CURDIR)/debian/tmp/usr

	# Fix up the Java packages so they conform to policy
	mkdir -p debian/tmp/usr/share/java
	(cd debian/tmp/usr/lib/java ; jar cvf ../../share/java/dcopjava-$(upstream_version).jar org/ )
	cd debian/tmp/usr/lib/java; rm -rf org
	ln -s dcopjava-$(upstream_version).jar debian/tmp/usr/share/java/dcopjava.jar
	mv debian/tmp/usr/lib/java/qtjava.jar debian/tmp/usr/share/java/qtjava-$(upstream_version).jar
	ln -s qtjava-$(upstream_version).jar debian/tmp/usr/share/java/qtjava.jar
	mv debian/tmp/usr/lib/java/koala.jar debian/tmp/usr/share/java/koala-$(upstream_version).jar
	ln -s koala-$(upstream_version).jar debian/tmp/usr/share/java/koala.jar

	cp $(objdir)/kjsembed/docs/embedding/simple-embed/Makefile kjsembed/docs/embedding/simple-embed

	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_install --list-missing

	for p in $(PACKAGES_WITH_USD_SYMLINK); do \
	  rm -f debian/$$p/usr/share/doc/$$p/{copyright,changelog.Debian}; \
	done


# 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_link
	dh_strip
	dh_compress -X.java -X.ui -X.js -X.rb -X.map -X.wav -X.bz2 -X.css -X.dcl -X.docbook -X-license -X.tag
	dh_fixperms 
# hack: dh_makeshlibs insists on adding ldconfig calls to postinst and
# postrm even if no libs are in ldconfig'able directories.
	dh_perl
	dh_python
	dh_makeshlibs -V -Nlibkde3-jni -Nlibqt0-ruby1.8 -Nlibkorundum0-ruby1.8
	dh_makeshlibs -n -V -plibkde3-jni -plibqt0-ruby1.8 -plibkorundum0-ruby1.8
	dh_installdeb
	dh_shlibdeps -l debian/libqt3-jni/usr/lib:debian/libsmokeqt1/usr/lib:debian/libsmokekde1/usr/lib:debian/libkjsembed1/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

