#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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


ifneq (,$(findstring withjava,$(DEB_BUILD_OPTIONS)))
	JAVA_FLAGS=--enable-javasupport
	ifneq (,$(JAVA_HOME))
		JAVA_FLAGS+= --with-java=$(JAVA_HOME)
	endif
endif

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	DEBUG_FLAGS=--enable-debug=full
endif

ifeq (,$(wildcard /usr/bin/po2debconf))
PO@DEBCONF := no
MINDEBCONFVER := 0.5
else
PO2DEBCONF := yes
MINDEBCONFVER := 1.2.0
endif

-include debian/debiandirs

debian/debiandirs: admin/debianrules
	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

	./configure --prefix=$(kde_prefix) \
				--sysconfdir=/etc/kde3 \
				--with-kdelibsdoxy-dir=/usr/share/doc/kde/HTML/en/kdelibs-apidocs \
				--with-pythondir=/usr \
				--enable-scripting \
				--enable-autoproject \
				--enable-scriptproject \
				--enable-trollproject \
				--enable-customproject \
				--enable-cvs \
				--enable-vba \
				--disable-final \
				$(JAVA_FLAGS) \
				$(DEBUG_FLAGS)

	touch configure-stamp

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

	$(MAKE)

	touch build-stamp

debian-clean:
	dh_testdir
	dh_testroot
	
	rm -f install-stamp

	dh_clean

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp install-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

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

#	if test -d CVS; then \
#		$(MAKE) -f admin/Makefile.common cvs-clean ;\
#	fi

	dh_clean
	-rm -f parts/appwizard/common/admin.tar.gz
	-rm -f parts/appwizard/common/gnu.tar.gz
	-rm -f parts/appwizard/common/incadmin.tar.gz
	-rm -f parts/appwizard/common/gnome.tar.gz
	-rm -f parts/appwizard/common/wxwidgets.tar.gz

install: DH_OPTIONS=
install-stamp: install
	touch install-stamp

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

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

	-install -d $(CURDIR)/debian/tmp/usr/share/kdevelop3
	-install debian/update_doc_index.sh $(CURDIR)/debian/tmp/usr/share/kdevelop3
	-rm -f debian/tmp/usr/share/mimelnk/text/x-pascal.desktop
	rm -f debian/tmp/usr/lib/libkdevcatalog.so
	rm -f debian/tmp/usr/lib/libkdevcppparser.so
	rm -f debian/tmp/usr/lib/libkdevelop.so
	rm -f debian/tmp/usr/lib/libkdevelopqextmdi.so
	rm -f debian/tmp/usr/lib/libpropertyeditor.so
	mv debian/tmp/usr/share/config/kdeveloprc debian/tmp/usr/share/config/kdevelop3rc
	mv debian/tmp/usr/bin/kdevelop debian/tmp/usr/bin/kdevelop3
	mv debian/tmp/usr/share/applnk/Development/kdevelop.desktop debian/tmp/usr/share/applnk/Development/kdevelop3.desktop
	mv debian/tmp/usr/share/apps/kdevelop debian/tmp/usr/share/apps/kdevelop3
	mv debian/tmp/usr/share/doc/kde/HTML/en/kdevelop debian/tmp/usr/share/doc/kde/HTML/en/kdevelop3
	mv debian/tmp/usr/share/icons/hicolor/16x16/apps/kdevelop.png debian/tmp/usr/share/icons/hicolor/16x16/apps/kdevelop3.png
	mv debian/tmp/usr/share/icons/hicolor/48x48/apps/kdevelop.png debian/tmp/usr/share/icons/hicolor/48x48/apps/kdevelop3.png
	mv debian/tmp/usr/share/icons/hicolor/32x32/apps/kdevelop.png debian/tmp/usr/share/icons/hicolor/32x32/apps/kdevelop3.png

	dh_install

# 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
ifeq ($(PO2DEBCONF),yes)
	po2debconf -e utf8 debian/templates.master > debian/templates
endif
	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
	dh_installmime
	dh_installinit
	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
	dh_gencontrol -- -V'debconf-depends=debconf (>= $(MINDEBCONFVER))'
	dh_md5sums
	dh_builddeb

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

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

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

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