#!/usr/bin/make -f
# progsreiserfs debian/rules
#
# This file is used by dpkg-buildpackage to build the progsreiserfs packages.  
# To build a package, run `dpkg-buildpackage' or `debuild' from the
# parent directory. (You may need to specify `-rfakeroot' to
# dpkg-buildpackage if you are not running it as root ...)
#
# Copyright (C) 1999, 2000, 2001, 2002  Timshel Knoll
# Licensed under the terms of the GNU General Public License
# Based (a long time ago) on parted's debian/rules, which in turn is based on
# `Sample debian/rules that uses debhelper' from dh_make,
# GNU Copyright (C) 1997 to 1999 by Joey Hess
# Copyright 2004-2006 Jose Luis Tallon
#

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

# The name of the library package. This will be lib$(LIBPKG), so for "libreiserfs",
# this should be "reiserfs"
LIBPKG = reiserfs

# The version of the debian package, used in naming the udeb
DEBPKGVER = $(shell dpkg-parsechangelog | sed -n -e '/^Version: /{s///;p;q;}')

# The library package version, ie. version 0.2 will build package
# libparted0.2, libparted0.2-dev etc.
# When incrementing this because of a new library SONAME, add an 'a' to the
# end if there isn't already one, otherwise increment the letter that is
# already there.
# When incrementing this, also set DEPVER to the current package version.
LIBPKGVER = 0.3

# The name of the main binary package
BINPKG = progsreiserfs

# Append the LIBPKGVER to non-lib package names? This is useful when building
# packages of the development versions of parted (eg. to build a parted1.5
# package)
# BINPKGVER = $(LIBPKGVER)

# The first version of the package to build with the current LIBPKGVER
# This should be changed to the current version when LIBPKGVER is changed ...
DEPVER = 0.3.0

# This is the library's soname. The build will fail if this is wrong, since
# if the soname has changed we need to update stuff ...
LIBSONAME = 0

# This is for the version for the shared library
SHAREDLIBPKGVER = $(LIBPKGVER)-$(LIBSONAME)

# The list of sed substitutions we need to do when generating files ...
# if you put `x' in here somewhere, the substitution `s/@x@/$(x)/'
# will be done ...
SUBSTS = LIBPKGVER BINPKGVER DEPVER LIBSONAME SHAREDLIBPKGVER

# Files which are generated by this script ...
# You will probably need to change this depending on what files your package
# needs ...
GENFILES = debian/control debian/shlibs.local \
	debian/lib$(LIBPKG)$(SHAREDLIBPKGVER).files \
	debian/lib$(LIBPKG)$(LIBPKGVER)-dev.files \
	debian/lib$(LIBPKG)$(LIBPKGVER)-dev.docs


ifneq (, $(BINPKGVER))
GENFILES += debian/$(BINPKG)$(BINPKGVER).files \
	debian/$(BINPKG)$(BINPKGVER)-bf.manpages \
	debian/$(BINPKG)$(BINPKGVER)-doc.files \
	debian/$(BINPKG)$(BINPKGVER)-doc.docs \
	debian/$(BINPKG)$(BINPKGVER)-doc.postinst \
	debian/$(BINPKG)$(BINPKGVER)-doc.prerm
endif

ifeq (, $(DEB_BUILD_ARCH))
DEB_BUILD_ARCH = $(shell dpkg-architecture -qDEB_BUILD_ARCH)
endif

ifeq (, $(DEB_BUILD_GNU_TYPE))
DEB_BUILD_GNU_TYPE = $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
endif

ifeq (, $(DEB_HOST_GNU_TYPE))
DEB_HOST_GNU_TYPE = $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
endif

ifeq (, $(ARCH))
ARCH = $(DEB_BUILD_ARCH)
endif

ifeq (, $(CFLAGS))
CFLAGS = -g
UDEB_CFLAGS = -g

ifeq (, $(findstring noopt, $(DEB_BUILD_OPTIONS)))
CFLAGS += -O2
UDEB_CFLAGS += -Os
else
CFLAGS += -O0
UDEB_CFLAGS += -O0
endif
endif

# This builds a substitution list for sed based on the SUBSTS variable
# and the variables whose names SUBSTS contains ...
SUBSTLIST = $(foreach subst, $(SUBSTS), s/@$(subst)@/$($(subst))/g;)

# Pattern rules:

# How to generate various files ...
debian/%: debian/%.in debian/rules
	sed -e '$(SUBSTLIST)' < $< > $@

# This puts the lib$(LIBPKG)* packaging files in their right places
debian/lib$(LIBPKG)$(SHAREDLIBPKGVER)%: debian/lib$(LIBPKG)%
	cp $< $@

debian/lib$(LIBPKG)$(LIBPKGVER)%: debian/lib$(LIBPKG)%
	cp $< $@

debian/$(BINPKG)$(BINPKGVER)%: debian/$(BINPKG)%
	cp $< $@

# by default, do something sane ...
default: echo-vars

echo-vars:
	@echo "Try: debian/rules [configure|build|install|binary|binary-arch|binary-indep|clean]"
	@echo "CFLAGS:		$(CFLAGS)"
	@echo "UDEB_CFLAGS:	$(UDEB_CFLAGS)"
	@echo "DEB_BUILD_ARCH:	$(DEB_BUILD_ARCH)"
	@echo "DEB_BUILD_GNU_TYPE: $(DEB_BUILD_GNU_TYPE)"
	@echo "DEB_HOST_GNU_TYPE: $(DEB_HOST_GNU_TYPE)"
	@echo "LIBSONAME:	$(LIBSONAME)"
	@echo "LIBPKGVER:	$(LIBPKGVER)"
	@echo "BINPKGVER:	$(BINPKGVER)"
	@echo "GENFILES:	$(GENFILES)"
	@echo "SUBSTLIST:	$(SUBSTLIST)"

packaging-files: $(GENFILES)

configure: patch-stamp packaging-files autotools build/configure-stamp
build/configure-stamp:
	dh_testdir
	[ -d build ] || mkdir build

ifneq (, $(findstring debug, $(DEB_BUILD_OPTIONS)))
	@echo "warning: option \`debug' in \$$DEB_BUILD_OPTIONS: building with gcc option \`-g'" >&2
endif

#	 Add here commands to configure the package.
#	 Install executables to /sbin ...
	cd build && CFLAGS="$(CFLAGS)" ../configure --prefix=/usr \
	    --sbindir=/sbin --mandir=\$${prefix}/share/man \
	    --infodir=\$${prefix}/share/info --enable-shared \
	    --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)

	touch $@

configure-udeb: packaging-files build-udeb/configure-stamp
build-udeb/configure-stamp:
	dh_testdir
	[ -d build-udeb ] || mkdir build-udeb

ifneq (, $(findstring debug, $(DEB_BUILD_OPTIONS)))
	@echo "warning: option \`debug' in \$$DEB_BUILD_OPTIONS: building with gcc option \`-g'" >&2
endif

#	 Add here commands to configure the package.
#	 Install executables to /sbin ...
	cd build-udeb && CFLAGS="$(UDEB_CFLAGS)" ../configure --prefix=/usr \
	    --sbindir=/sbin --mandir=\$${prefix}/share/man \
	    --infodir=\$${prefix}/share/info --enable-shared \
	    --disable-static --disable-debug \
	    --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)

	touch $@

configure-dbg: packaging-files build-dbg/configure-stamp
build-dbg/configure-stamp:
	dh_testdir
	[ -d build-dbg ] || mkdir build-dbg

#	 Add here commands to configure the package.
	cd build-dbg && CFLAGS="-g $(CFLAGS)" ../configure --prefix=/usr \
	    --enable-debug --disable-shared --build=$(DEB_BUILD_GNU_TYPE) \
	    --host=$(DEB_HOST_GNU_TYPE)

	touch $@

build: packaging-files configure build/build-stamp
build/build-stamp:
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE) -C build

	touch $@

build-udeb: packaging-files configure-udeb build-udeb/build-stamp
build-udeb/build-stamp:
	dh_testdir

#	 Add here commands to compile the package.
#	 Don't need to build headers or doco rubbish, just the library ...
#	 Note: this assumes that the library source code is in the
#	 lib(x) directory, change this if it isn't ...
	$(MAKE) -C build-udeb/libdal
	$(MAKE) -C build-udeb/libreiserfs

	touch $@

build-dbg: packaging-files configure-dbg build-dbg/build-stamp
build-dbg/build-stamp:
	dh_testdir

#	 Add here commands to compile the package.
#	 Don't need to build headers or doco rubbish, just the library ...
#	 Note: this assumes that the library source code is in the
#	 lib(x) directory, change this if it isn't ...
	$(MAKE) -C build-dbg/libdal
	$(MAKE) -C build-dbg/libreiserfs

	touch $@

clean: unpatch packaging-files autotools
	dh_testdir
	dh_testroot

	# Add here commands to clean up after the build process
	$(RM) -r build build-udeb build-dbg

	# Remove all debian/lib$(LIBPKG)*.* except for lib$(LIBPKG).*,
	# lib$(LIBPKG)-dev.* and lib$(LIBPKG)$(LIBPKGVER)*
	@for file in debian/lib$(LIBPKG)*; do \
	   file="`basename $$file`"; \
	   case "$$file" in \
	   lib$(LIBPKG).*|lib$(LIBPKG)-dev.*|lib$(LIBPKG)$(SHAREDLIBPKGVER).*| \
	   lib$(LIBPKG)$(LIBPKGVER)-dev.*|lib$(LIBPKG)$(LIBPKGVER)-dbg.*) \
	      ;; \
	   *) \
	      echo "rm -rf \"debian/$$file\""; \
	      rm -rf "debian/$$file" ;; \
	   esac; \
	done

	-$(MAKE) distclean
	$(RM) config.sub config.guess
	dh_clean


autotools:
	test -e config.sub -a -L config.sub || \
		ln -sf /usr/share/misc/config.sub config.sub
	test -e config.guess -a -L config.guess || \
		ln -sf /usr/share/misc/config.guess config.guess


install: DH_OPTIONS=-Nlib$(LIBPKG)$(LIBPKGVER)-dbg
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) -C build install DESTDIR=$(CURDIR)/debian/tmp

#	 We need to do 2 things here:
#	   1. Check that $(LIBSONAME) is actually what the library's SONAME is
#	   2. Fix the lib(whatever).so symlink to point to /lib/lib(whatever).so.x.x.x
#	      rather than being relative & pointing to the current directory
	@(cd debian/tmp/usr/lib; \
	  for lib in dal reiserfs ; do \
	    SONAME=`ls lib$${lib}-$(LIBPKGVER).so.*.*.* | \
		    sed -e 's/^.*\.so\.\([0-9]\+\)\..*$$/\1/'`; \
	    if [ "$(LIBSONAME)" -ne "$$SONAME" ]; then \
	       echo " *** error: debian/rules thinks that lib$${lib} is" \
	            "version $(LIBSONAME)" >&2; \
	       echo " *** library is actually version $$SONAME. These should" \
	            "be the same." >&2; \
	       echo " *** Change the value of LIBSONAME to $$SONAME in" \
	            "debian/rules, and" >&2; \
	       echo " *** while you're there, bump the value of LIBPKGVER and" \
	            "also set"; \
	       echo " *** DEPVER to the current version of the package that" \
	            "you are building." >&2; \
	       exit 1; \
	    fi; \
	    ln -sf /lib/`readlink lib$${lib}.so` lib$${lib}.so; \
	  done)

#	 Make sure the shared libs are in /lib (otherwise there's no point
#	 installing the executable to /sbin ...) :-)
	mkdir -p debian/tmp/lib
	mv debian/tmp/usr/lib/*.so.* debian/tmp/lib

	dh_movefiles

#	 Remove all empty directories under debian/tmp
#	 The `sort -r' makes sure that the dir `x/y' is removed before `x'
	rmdir --ignore-fail-on-non-empty `find debian/tmp -type d | sort -r`

	@if [ -d debian/tmp ]; then \
	   echo " *** WARNING: files exists in debian/tmp after" \
	        "dh_movefiles!!!" >&2; \
	   echo " *** Maybe the debian/*.files files need updating ..." >&2; \
	 fi

install-udeb: DH_OPTIONS=-plib$(LIBPKG)$(LIBPKGVER)-udeb
install-udeb: build-udeb
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) -C build-udeb/libdal install \
		DESTDIR=$(CURDIR)/debian/lib$(LIBPKG)$(LIBPKGVER)-udeb
	$(MAKE) -C build-udeb/libreiserfs install \
		DESTDIR=$(CURDIR)/debian/lib$(LIBPKG)$(LIBPKGVER)-udeb
	
	rm -f debian/lib$(LIBPKG)$(LIBPKGVER)-udeb/usr/lib/*.la
	rm -f debian/lib$(LIBPKG)$(LIBPKGVER)-udeb/usr/lib/*.so

install-dbg: DH_OPTIONS=-plib$(LIBPKG)$(LIBPKGVER)-dbg
install-dbg: build-dbg
	dh_testdir
	dh_testroot
	dh_clean -k -plib$(LIBPKG)$(LIBPKGVER)-dbg
	dh_installdirs

	install -D -m644 build-dbg/libdal/.libs/libdal.a \
	   debian/lib$(LIBPKG)$(LIBPKGVER)-dbg/usr/lib/libdal_g.a
	install -D -m644 build-dbg/libreiserfs/.libs/libreiserfs.a \
	   debian/lib$(LIBPKG)$(LIBPKGVER)-dbg/usr/lib/libreiserfs_g.a

# 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
#	dh_installdebconf
	dh_installdocs
	dh_installexamples
#	dh_installmenu
#	dh_installpam
#	dh_installinit
	dh_installcron
	dh_installman
	dh_installinfo
	dh_installchangelogs ChangeLog
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
	dh_makeshlibs -V 'lib$(LIBPKG)$(SHAREDLIBPKGVER) (>= $(DEPVER))'
	dh_installdeb
#	dh_perl
	dh_shlibdeps -l$(CURDIR)/debian/lib$(LIBPKG)$(SHAREDLIBPKGVER)/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
# Unfortunately, we still need to build since the info file has to be
# re-generated before installation and the locales need to be installed :-(
binary-indep: build install
# (Uncomment this next line if you have such packages.)
#	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build build-udeb build-dbg install install-udeb install-dbg
	$(MAKE) -f debian/rules DH_OPTIONS="-a" binary-common

# Any other binary targets build just one binary package at a time.
binary-%-dbg: build-dbg install-dbg
	$(MAKE) -f debian/rules DH_OPTIONS="-p$*-dbg" binary-common

binary-%-udeb: build-udeb install-udeb
	$(MAKE) -f debian/rules DH_OPTIONS="-p$*-udeb" binary-common

binary-%: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-p$* binary-common

binary: binary-arch binary-indep

.PHONY: configure configure-udeb configure-bf configure-dbg
.PHONY: build build-udeb build-bf build-dbg
.PHONY: install install-bf install-dbg install-i18n
.PHONY: clean binary-indep binary-arch binary
.PHONY: packaging-files autotools

include /usr/share/dpatch/dpatch.make
