#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.
#
# This version is for a hypothetical package that can build a kernel modules
# architecture-dependant package via make-kpkg, as well as an
# architecture-independent module source package, and other packages
# either dep/indep for things like common files or userspace components
# needed for the kernel modules.

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

# This is the debhelper compatibility version to use.
export DH_COMPAT=4

# 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)
DEB_HOST_ARCH       ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)


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

### KERNEL SETUP
### Setup the stuff needed for making kernel module packages
### taken from /usr/share/kernel-package/sample.module.rules

# Name of package
package		= oprofile
# KSRC is the location of the kernel source. This is the default value,
# when make-kpkg is used it will supply to real value
ifeq ($(strip $(KSRC)),)
KSRC		= $(shell \
      if test -d /usr/src/linux; then   \
         echo /usr/src/linux ;          \
      else                              \
         if test $$( ls -d /usr/src/kernel-source-* | grep -v gz | grep -v bz2 | grep -v dsc | wc -l ) -eq 1 ; then     \
            echo $$( ls -d /usr/src/kernel-source-* | grep -v gz | grep -v bz2 | grep -v dsc | head -1 ) ;              \
         else                           \
            echo /tmp  ;                \
         fi    ;                        \
      fi                                \
   )
endif
# KDREV is the package-revision, as given to make-kpkg by the user.
# Just put a simply default value in here which we use when we test
# the packagebuilding without make-kpkg
ifeq ($(strip $(KDREV)),)
KDREV		= "test1.0"
endif

## Now to determine the kernel version, normally supplied by make-kpkg
ifeq ($(strip $(KVERS)),)
# Now we need to get the kernel-version somehow (we are not running
# under make-kpkg?)
ifeq ($(strip $(KSRC)),)
$(error Error. I do not know how to determine the kernel version)
else
kversion :=$(shell egrep '^VERSION +=' $(KSRC)/Makefile 2>/dev/null | \
                 sed -e 's/[^0-9]*\([0-9]*\)/\1/')
kplevel  :=$(shell egrep '^PATCHLEVEL +=' $(KSRC)/Makefile 2>/dev/null | \
                    sed -e 's/[^0-9]*\([0-9]*\)/\1/')
ksublevel:=$(shell egrep '^SUBLEVEL +=' $(KSRC)/Makefile 2>/dev/null | \
                  sed -e 's/[^0-9]*\([0-9]*\)/\1/')
EXTRA_VERSION:=$(shell egrep '^EXTRAVERSION +=' $(KSRC)/Makefile 2>/dev/null | \
                 sed -e 's/EXTRAVERSION[\t ]*=[\t ]*\(.*\)/\1/')
kextra:=$(strip $(EXTRA_VERSION))
HAVE_NEW_MODLIB:=$(shell egrep '\(INSTALL_MOD_PATH\)' \
                            $(KSRC)/Makefile 2>/dev/null )
KMOD=$(kversion).$(kplevel).$(ksublevel)$(EXTRA_VERSION)

KCONF=$(KSRC)/include/linux/autoconf.h

# If you want to have more than one kernel configuration per kernel
# version, set FLAVOUR in the top level kernel Makefile *before*
# invoking make-kpkg -- it will be appended to UTS_RELEASE in
# version.h (separated by a hyphen). This affects everything -- the
# names and versions of the image, source, headers, and doc packages,
# and where the modules are searched for in /lib/modules.

ifdef FLAVOUR
# uhm - should check if we really have a Makefile capable of Flavours?
endif

FLAVOUR:=$(shell grep ^FLAVOUR $(KSRC)/Makefile 2>/dev/null | \
                  perl -ple 's/FLAVOUR[\s:=]+//g')

ifneq ($(strip $(FLAVOUR)),)
INT_FLAV := -$(FLAVOUR)
FLAV_ARG := FLAVOUR=$(FLAVOUR)
else
INT_FLAV :=
FLAV_ARG :=
endif

## This is the replacement for FLAVOUR
ifneq ($(strip $(APPEND_TO_VERSION)),)
iatv := $(strip $(APPEND_TO_VERSION))
EXTRAV_ARG := EXTRAVERSION=${EXTRA_VERSION}${iatv}
else
iatv :=
EXTRAV_ARG :=
endif

KVERS = $(kversion).$(kplevel).$(ksublevel)$(kextra)$(iatv)$(INT_FLAV)

endif
endif

non_epoch_version=$(shell echo $(KVERS) | perl -pe 's/^\d+://')
epoch=$(shell echo $(KVERS) | perl -ne 'm/^(\d+:)/ && print $$1')

# We also need the package version
pversion	= $(shell sed -ne '1s/.*\((.*)\).*/\1/' debian/changelog)
pversion	= $(shell sed -ne '1s/.*(\(.*\)).*/\1/p' debian/changelog)

# MODDIR is the place where the final .deb package should be made. This is the
# default value, when make-kpkg is used it will supply to real value
MODDIR		= ../..

pmodules = $(package)-modules-$(non_epoch_version)
psource = $(package)-source

# Prepares the package for distribution.  Intended for the kernel
# maintainer.
kdist: kdist_image

# The kdist_configure target is called by make-kpkg modules_config. It
# should configure the module so it is ready for compilation (mostly
# useful for calling configure)
kdist_config: 
	ACLOCAL=aclocal-1.7 AUTOMAKE=automake-1.7 ./autogen.sh
	./configure --host=$(DEB_HOST_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --without-kernel-support --with-linux=$(KSRC)
	( if test ! -f $(KCONF); then \
	   echo '***** cannot build oprofile module until kernel is configured' ; \
	   exit 1 ; \
	fi )


# the kdist_image target is called by make-kpkg modules_image. It is
# responsible for compiling the module and creating the package. It
# should also clean up after making the module. Please note we use a
# seperate binary-modules target to make testing the package building
# easier
kdist_image: kdist_config
	export DH_OPTIONS='-p$(pmodules)'

	# Is this needed for setting up a Depends?
	echo "kpkg:Kernel-Version=$(KVERS)" > \
		debian/$(pmodules).substvars

	# The substvars mechanism seems slightly broken, hack to get around it
	# stolen from the qce-ga package. Yaaaa!
	sed -e 's/$${kpkg\:Kernel\-Version}/$(KVERS)/' \
	   debian/control.in > debian/control

	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	
	# Build & Install the module
	DESTDIR=$(CURDIR)/debian/$(pmodules) \
	   CFLAGS="-DCONFIG_SMP" $(MAKE) $(MFLAGS) install.debian

	dh_installdocs README
	dh_installchangelogs

	dh_installdeb
	dh_gencontrol -- -v$(pversion)
	dh_md5sums
	dh_builddeb --destdir=$(MODDIR)


# the kdist_clean target is called by make-kpkg modules_clean. It is
# responsible for cleaning up any changes that have been made by the
# other kdist_commands (except for the .deb files created).
kdist_clean:
	$(MAKE) $(MFLAGS) -f debian/rules clean

### end  KERNEL SETUP

configure: config-stamp
config-stamp:
	cp debian/Makefile.modules.am .
	ACLOCAL=aclocal-1.7 AUTOMAKE=automake-1.7 ./autogen.sh
	cp debian/configure.modules.in .
	autoconf --include=m4 configure.modules.in > configure.modules
	chmod +x configure.modules
	./configure --host=$(DEB_HOST_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --with-qt-includes=/usr/include/qt3 --with-kernel-support --with-linux=$(KSRC)
	touch config-stamp

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

# the binary-modules target prepares the $(pmodules) package.
# It is called by make-kpkg and *not* during a normal build
binary-modules: kdist_image

build-indep: config.status build-indep-stamp
build-indep-stamp:
	dh_testdir
	touch build-indep-stamp

build: build-arch build-indep

OPROFILE_SUBDIRS = $(shell find * -type d -maxdepth 0 )
clean:
	dh_testdir
	dh_testroot
	-$(MAKE) clean
	-$(MAKE) distclean
	rm -f automake-stamp
	rm -f config-stamp build-arch-stamp build-indep-stamp 
	rm -f Makefile.modules.am Makefile.modules.in Makefile.modules
	rm -f configure.modules configure.modules.in
	rm -f debian/Makefile.modules 
	rm -f debian/oprofiled24 debian/oprofiled26
	rm -f config.status config.cache
	for ii in $(OPROFILE_SUBDIRS) ; \
	do \
		(cd $$ii && rm -rf .deps) || exit 1 ; \
	done
	-test -r /usr/share/misc/config.sub && \
	  cp -f /usr/share/misc/config.sub config.sub
	-test -r /usr/share/misc/config.guess && \
	  cp -f /usr/share/misc/config.guess config.guess
	find * -name '*.[oa]' | xargs rm -f
	rm -f libregex/regex_test libdb/db_test daemon/oprofiled
	rm -f dae/oprofiled
	rm -f utils/op_session utils/op_help
	rm -f pp/oprofpp pp/op_to_source pp/op_time pp/op_merge
	rm -f pp/opannotate pp/opgprof pp/opreport
	rm -f gui/oprof_start
	rm -rf debian/oprofile-modules-*
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Create the directories to install the source into
	dh_installdirs -p$(psource)  usr/src/modules/$(package)
	dh_installdirs -p$(psource)  usr/src/modules/$(package)/libop
	dh_installdirs -p$(psource)  usr/src/modules/$(package)/libutil
	dh_installdirs -p$(psource)  usr/src/modules/$(package)/module
	dh_installdirs -p$(psource)  usr/src/modules/$(package)/debian

	# Copy only the driver source to the proper locations
	cp Chang*         debian/$(psource)/usr/src/modules/$(package)
	cp [RT]*          debian/$(psource)/usr/src/modules/$(package)
	cp Makefile.modules \
		debian/$(psource)/usr/src/modules/$(package)/Makefile
	cp Makefile.modules.in \
		debian/$(psource)/usr/src/modules/$(package)/Makefile.in
	cp Makefile.modules.am \
		debian/$(psource)/usr/src/modules/$(package)/Makefile.am
	cp install-sh     debian/$(psource)/usr/src/modules/$(package)
	cp *.sh           debian/$(psource)/usr/src/modules/$(package)
	cp configure.modules \
		debian/$(psource)/usr/src/modules/$(package)/configure
	cp configure.modules.in \
		debian/$(psource)/usr/src/modules/$(package)/configure.in
	cp debian/oprofile-source.postinst \
		debian/$(psource)/usr/src/modules/$(package)/debian/postinst
	chmod 755 debian/$(psource)/usr/src/modules/$(package)/debian/postinst
	cp debian/oprofile-source.HOWTO-ia64 \
		debian/$(psource)/usr/share/doc/$(psource)/HOWTO-ia64
	cp debian/oprofile-source.HOWTO-i386 \
		debian/$(psource)/usr/share/doc/$(psource)/HOWTO-i386
	cp config.guess   debian/$(psource)/usr/src/modules/$(package)
	cp config.h.in    debian/$(psource)/usr/src/modules/$(package)
	cp -r *m4         debian/$(psource)/usr/src/modules/$(package)
	( for ii in libop/Makefile* ; do \
	     jj=`basename $$ii` ;        \
	     cat $$ii | \
	        sed 's/SUBDIRS = \. tests/SUBDIRS = ./' >  \
	           debian/$(psource)/usr/src/modules/$(package)/libop/$$jj ; \
	   done )
	cp -a libop/*.[ch]  \
		debian/$(psource)/usr/src/modules/$(package)/libop
	( for ii in libutil/Makefile* ; do \
	     jj=`basename $$ii` ;        \
	     cat $$ii | \
	        sed 's/SUBDIRS = \. tests/SUBDIRS = ./' >  \
	           debian/$(psource)/usr/src/modules/$(package)/libutil/$$jj ; \
	   done )
	cp -a libutil/*.[ch]  \
		debian/$(psource)/usr/src/modules/$(package)/libutil
	cp -a module/Makefile*  \
		debian/$(psource)/usr/src/modules/$(package)/module
	cp -a module/*.[chS]  \
		debian/$(psource)/usr/src/modules/$(package)/module
	cp -a module/ia64  \
		debian/$(psource)/usr/src/modules/$(package)/module
	cp -a module/x86  \
		debian/$(psource)/usr/src/modules/$(package)/module
	cp -a module/x86  \
		debian/$(psource)/usr/src/modules/$(package)/module
	rm -f debian/$(psource)/usr/src/modules/$(package)/module/x86/.cvsignore

	# Copy the needed debian/ pieces to the proper location
	cp debian/control.modules.in \
		debian/$(psource)/usr/src/modules/$(package)/debian/control.in
	cp debian/rules \
		debian/$(psource)/usr/src/modules/$(package)/debian
	cp debian/changelog \
		debian/$(psource)/usr/src/modules/$(package)/debian
	cp debian/copyright \
		debian/$(psource)/usr/src/modules/$(package)/debian
	cp debian/dirs.modules.in \
		debian/$(psource)/usr/src/modules/$(package)/debian/dirs
	#-- need to correct one m4 file
	cp debian/findkernel.m4 \
		debian/$(psource)/usr/src/modules/$(package)/m4/findkernel.m4
	#-- build the tar.gz file
	( cd debian/$(psource)/usr/src; \
	  tar cvzf oprofile-source.tar.gz modules )
	( cd debian/$(psource)/usr/src; rm -rf modules )


# 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 -a
	dh_testroot -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installmenu -a
	dh_installcron -a
	dh_installman -a
	dh_installinfo -a
	dh_installdebconf -a
	dh_installchangelogs ChangeLog -a
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	( if [ $(DEB_HOST_ARCH) == "ia64" ]; \
	  then \
	     echo "$(DEB_HOST_ARCH):OtherDeps=, libunwind7-dev, libpfm2-dev, libpfm3-dev" >> debian/oprofile-source.substvars ; \
	  else \
	     echo "$(DEB_HOST_ARCH):OtherDeps=" >> debian/oprofile-source.substvars ; \
	  fi; \
	) 
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install  binary-modules kdist kdist_config kdist_image kdist_clean
