#!/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_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
builddir=$(CURDIR)/oprofile-0.9.3

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

patch: patch-stamp
patch-stamp: unpack-stamp
	ln -sf ../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) && CXXFLAGS="-O0 -g" CFLAGS="-O0 -g" LDFLAGS="-L/usr/lib -L/lib" ./configure --host=$(DEB_HOST_GNU_TYPE) --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-kernel-support 
	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 debian/patches/patches
	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 

	#-- 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_installmenu 
	dh_installman 
	dh_installchangelogs $(builddir)/ChangeLog $(builddir)/ChangeLog-2006
	dh_install --sourcedir=debian/oprofile 
	#-- now that everything has been installed, clean up the
	#   original debian/oprofile directory
	rm -rf debian/oprofile/usr/bin
	rm -rf debian/oprofile/usr/share/man
	rm -rf debian/oprofile/usr/share/oprofile
	dh_strip --dbg-package=oprofile-common
	dh_link 
	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
