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

INSTALL_DIR = install -p -d -o root -g root -m 755
INSTALL_FILE = install -p -o root -g root -m 644

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

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

# These options are for distributed compiling.
# DEB_CONCURRENCY_LEVEL must always be >= 1.
DEB_CONCURRENCY_LEVEL   ?= 1
do_parallel = -j$(DEB_CONCURRENCY_LEVEL)
DEB_DISTCC_HOSTS        ?= localhost
export DISTCC_HOSTS = $(DEB_DISTCC_HOSTS)

ifeq ($(DEB_BUILD_GNU_TYPE),i386-linux)
  # Uncomment the following 2 lines to enable i686 optimziation
  # You may want to tweak the settings to your likings
  #export CFLAGS=-O3 -march=i686 -ffast-math -finline-functions
  #export CXXFLAGS=-O3 -march=i686 -ffast-math -finline-functions
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

	# Force a rerun of automake/etc since this is required by the
	# debian patches.
	$(MAKE) -f admin/Makefile.common ;\

	# make build directory
	mkdir $(objdir)

	# ensure configure is executable
	chmod +x configure

	# run configure with build tree $(objdir)
	cd $(objdir) && \
	../configure $(configkde)

	touch configure-stamp

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

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

	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

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

	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	cd $(objdir) && \
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

	# Generate HTML docs.
	for i in `find debian/tmp -name "index.docbook"`; do \
	  ( cd `dirname $$i`; meinproc `basename $$i` ); \
	done

	# Split into separate binary packages.
	dh_install --sourcedir=debian/tmp --list-missing

	# Don't override the openoffice.org mimelnk entries with our own.
	rm -f debian/koffice-data/usr/share/mimelnk/application/vnd.sun.xml.*.desktop

	# Install standard documentation links.
	for pkg in `dh_listpackages`; do \
	  if test -d debian/$$pkg/usr/share/doc/kde/HTML/en/$$pkg; then \
	    dh_link -p$$pkg usr/share/doc/kde/HTML/en/$$pkg \
	      usr/share/doc/$$pkg/html; \
	  fi; \
	done

	# Install lintian and linda overrides.
	for pkg in `dh_listpackages`; do \
	  if test -e debian/$$pkg.override; then \
	    $(INSTALL_DIR) debian/$$pkg/usr/share/lintian/overrides; \
	    $(INSTALL_FILE) debian/$$pkg.override \
	      debian/$$pkg/usr/share/lintian/overrides/$$pkg; \
	  fi; \
	  if test -e debian/$$pkg.linda; then \
	    $(INSTALL_DIR) debian/$$pkg/usr/share/linda/overrides; \
	    $(INSTALL_FILE) debian/$$pkg.linda \
	      debian/$$pkg/usr/share/linda/overrides/$$pkg; \
	  fi; \
	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

	# Install the correct upstream changelogs.
	for pkg in `dh_listpackages`; do \
	  case "$$pkg" in \
	    karbon | kchart | kexi | kivio | kpresenter | kspread | kugar | kword ) \
	      DH_OPTIONS='' dh_installchangelogs -p$$pkg $$pkg/CHANGES ;; \
	    koffice-libs ) \
	      DH_OPTIONS='' dh_installchangelogs -p$$pkg lib/CHANGES ;; \
	    * ) \
	      DH_OPTIONS='' dh_installchangelogs -p$$pkg ;; \
	  esac; \
	done

	dh_installdocs
	dh_installexamples
	dh_installman
	dh_installmenu
	dh_installmime

	# Rename some newly installed files, ensure all demo files
	# are once compressed and fix incorrect man page installations.
	for pkg in `dh_listpackages`; do \
	  case "$$pkg" in \
	    kugar ) \
	      mv debian/kugar/usr/share/doc/kugar/examples/kugar.examples.README \
	        debian/kugar/usr/share/doc/kugar/examples/README.examples; \
	    ;; \
	    kword | kspread ) \
	      for eg in debian/$$pkg/usr/share/doc/$$pkg/examples/*.k??; do \
	        if ! gzip -t $$eg > /dev/null 2>&1; then \
	          gzip -9 $$eg; \
	          mv $$eg.gz $$eg; \
	        fi; \
	      done; \
	    ;; \
	    kpresenter ) \
	      mv debian/kpresenter/usr/share/man/pl/man1/kprconverter.1 \
	        debian/kpresenter/usr/share/man/man1/kprconverter.pl.1; \
	      rm -rf debian/kpresenter/usr/share/man/pl; \
	    ;; \
	  esac; \
	done

	dh_strip
	dh_link
	dh_compress -X.docbook -X.css -X-license -X.dcl -X.bz2 -X.ksp -X.kwd -X.kwt
	dh_fixperms
	dh_makeshlibs -V
	dh_installdeb
	dh_perl

	# Run dh_shlibdeps with package-specific library search paths.
	for pkg in `dh_listpackages`; do \
	  echo "Checking dependencies for $$pkg..."; \
	  case "$$pkg" in \
	    koffice | koffice-doc-html ) \
	      DH_OPTIONS='' dh_shlibdeps -p$$pkg ;; \
	    koffice-* ) \
	      DH_OPTIONS='' dh_shlibdeps -p$$pkg -ldebian/koffice-libs/usr/lib ;; \
	    kexi ) \
	      DH_OPTIONS='' dh_shlibdeps -p$$pkg \
	        -ldebian/koffice-libs/usr/lib:debian/kugar/usr/lib:debian/$$pkg/usr/lib ;; \
	    * ) \
	      DH_OPTIONS='' dh_shlibdeps -p$$pkg \
	        -ldebian/koffice-libs/usr/lib:debian/$$pkg/usr/lib ;; \
	  esac; \
	done

	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
