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

# Uncomment this to turn on verbose mode. 
#export DH_VERBOSE=1

DEB_HOST_ARCH   ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
BUILDDIR=$(CURDIR)/oprofile-0.9.3

ifneq (,$(filter $(DEB_BUILD_ARCH), powerpc ppc64))
  on_powerpc = yes
endif

unpack: unpack-stamp
unpack-stamp:
	tar xvzf $(CURDIR)/oprofile-0.9.3.tar.gz
	touch unpack-stamp

patch: patch-stamp
patch-stamp: unpack-stamp
	[ -L $(BUILDDIR)/patches ] || ln -s ../debian/patches $(BUILDDIR)/patches
	cd $(BUILDDIR) && \
	if quilt next; then \
	  echo -n "Applying patches... "; \
	      if quilt push -a -v > patch-log; then  \
	    echo "successful."; \
	  else \
            quilt next; \
            echo "failed: "; \
            echo ; \
	    cat patch-log ; \
            echo ; \
            exit 1; \
          fi; \
        else \
	  echo "No patches to apply"; \
        fi; \
	>$@

configure: config-stamp patch
config-stamp:
	cd $(BUILDDIR) && ACLOCAL=aclocal-1.9 AUTOMAKE=automake-1.9 ./autogen.sh
	cd $(BUILDDIR) && ./configure --host=$(DEB_HOST_GNU_TYPE) --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-qt-includes=/usr/include/qt3 --with-kernel-support  $(if $(on_powerpc),--with-target=cell-be)
	touch config-stamp

build-arch: unpack-stamp patch-stamp configure build-arch-stamp
build-arch-stamp: 
	# Add here commands to build the package
	cd $(BUILDDIR) && DESTDIR=$(CURDIR)/debian/oprofile $(MAKE)
	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp:
	dh_testdir
	touch build-indep-stamp

build: build-arch build-indep

clean:
	dh_testdir
	dh_testroot
	rm -rf oprofile-0.9.3
	rm -f unpack-stamp patch-stamp config-stamp build-arch-stamp 
	rm -f build-indep-stamp patch-log
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/oprofile.
	cd $(BUILDDIR) && DESTDIR=$(CURDIR)/debian/oprofile $(MAKE) install 

	# Move some files to their proper location
	mv debian/oprofile/usr/bin/oprof_start \
	   debian/oprofile-gui/usr/bin
	cp debian/oprofile/usr/share/man/man1/oprofile.1 \
	   debian/oprofile-gui/usr/share/man/man1/oprof_start.1
	
	# Copy the driver source HOWTOs to the proper locations
	cp debian/oprofile-source.HOWTO-ia64 \
		debian/oprofile/usr/share/doc/oprofile/HOWTO-ia64
	cp debian/oprofile-source.HOWTO-i386 \
		debian/oprofile/usr/share/doc/oprofile/HOWTO-i386

	#-- clean up some of the mess from
	# an over-exuberant install command
	rm -f debian/oprofile/usr/share/mangled-name
	dh_link usr/share/man/man1/oprofile.1 usr/share/man/man8/oprofiled.8

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build install

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir 
	dh_testroot 
	dh_installdocs
	dh_installexamples 
	dh_installman 
	dh_installchangelogs $(BUILDDIR)/ChangeLog $(BUILDDIR)/ChangeLog-2006
	dh_strip 
	dh_link 
	dh_compress 
	dh_compress 
	dh_fixperms 
	dh_installdeb 
	dh_shlibdeps 
	dh_gencontrol 
	dh_md5sums 
	dh_builddeb 

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install patch unpack
