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

export DEB_CFLAGS_MAINT_PREPEND = -Wall

export AUTOMAKE := automake --foreign

configure: config-stamp 
config-stamp: $(QUILT_STAMPFN)
	dh_autoreconf
	cd $(CURDIR) && ./configure \
		--host=$(DEB_HOST_GNU_TYPE) \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--with-kernel-support \
		--disable-werror \
		--disable-account-check \
		--with-java=/usr/lib/jvm/default-java \
		$(shell dpkg-buildflags --export=cmdline)
	touch config-stamp

build-arch: configure build-arch-stamp
build-arch-stamp: 
	# Add here commands to build the package
	cd $(CURDIR) && 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
	[ ! -f Makefile ] || make distclean
	rm -f config-stamp build-arch-stamp 
	rm -f build-indep-stamp 
	dh_autoreconf_clean
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

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

	mkdir -p debian/libopagent1/usr/lib
	mkdir -p debian/libjvmti-oprofile0/usr/lib
	mv debian/oprofile/usr/lib/oprofile/libopagent.so.* \
	  debian/libopagent1/usr/lib/
	mv debian/oprofile/usr/lib/oprofile/libjvmti_oprofile.so.* \
	  debian/libjvmti-oprofile0/usr/lib/
	for i in debian/libopagent1/usr/lib/*.so.* \
	         debian/libjvmti-oprofile0/usr/lib/*.so.*; do \
	  b=$$(basename $$i); \
	  ln -sf ../$$b debian/oprofile/usr/lib/oprofile/$$b; \
	done
	ln -sf libopagent.so.1 debian/oprofile/usr/lib/libopagent.so
	ln -sf libjvmti_oprofile.so.0 debian/oprofile/usr/lib/libjvmti_oprofile.so

	# Fixup non-empty-dependency_libs-in-la-file lintian error
	sed -i "/dependency_libs/ s/'.*'/''/" `find . -name '*.la'`

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

	find debian/oprofile -name '*.la' | xargs -r rm -f

# 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 $(CURDIR)/ChangeLog $(CURDIR)/ChangeLog-2008
	dh_strip 
	dh_link 
	dh_compress -X.xsd -X.xml
	dh_fixperms 
	dh_makeshlibs
	dh_installdeb 
	dh_shlibdeps 
	dh_gencontrol 
	dh_md5sums 
	dh_builddeb 

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