#!/usr/bin/make -f
# GNU Parted debian/rules
#
# This file is used by dpkg-buildpackage to build the parted 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 ...)
#
# $Id: rules,v 1.50 2003/02/19 11:13:18 timshel Exp $
#
# 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

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

# 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 = 1.7.1-1

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

# 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/libparted$(SHAREDLIBPKGVER).files \
	debian/libparted$(LIBPKGVER)-dev.files \
	debian/libparted$(LIBPKGVER)-i18n.files \

ifneq (, $(BINPKGVER))
GENFILES += debian/parted$(BINPKGVER).files \
	debian/parted$(BINPKGVER)-doc.files \
	debian/parted$(BINPKGVER)-doc.docs \
	debian/parted$(BINPKGVER)-doc.postinst \
	debian/parted$(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 (, $(DEB_BUILD_GNU_SYSTEM))
DEB_BUILD_GNU_SYSTEM = $(shell dpkg-architecture -qDEB_BUILD_GNU_SYSTEM)
endif

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

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

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

endif

# This is a workaround for a m68k compiler bug ...
ifeq (m68k, $(DEB_BUILD_ARCH))
  CONFFLAGS += --disable-Werror
endif
ifeq (s390, $(DEB_BUILD_ARCH))
  CONFFLAGS += --disable-Werror
endif
ifeq (gnu, $(DEB_BUILD_GNU_SYSTEM))
  CONFFLAGS += --disable-Werror
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 libparted* packaging files in their right places
debian/libparted$(SHAREDLIBPKGVER)%: debian/libparted%
	cp $< $@

debian/libparted$(LIBPKGVER)%: debian/libparted%
	cp $< $@

debian/parted$(BINPKGVER)%: debian/parted%
	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 "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)

autotools-update: autotools-update-stamp
autotools-update-stamp: patch-stamp
	 ACLOCAL=aclocal-1.8 AUTOMAKE=automake-1.8 autoreconf -f -i
	 touch $@

build-deb/config.status: autotools-update-stamp
	dh_testdir
	[ -d build-deb ] || mkdir build-deb

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 the `parted' executable to /sbin ...
	cd build-deb && 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) \
	    $(CONFFLAGS)

build-dbg/config.status: autotools-update-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-mtrace --disable-shared $(CONFFLAGS) \
	    --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)

	touch $@

build: build-deb build-dbg

build-deb: build-deb/build-stamp
build-deb/build-stamp: build-deb/config.status
	dh_testdir

	$(MAKE) -C build-deb

#	By now just use ABI detection on i386 machines. (enable in 1.7.1)
	if [ $(DEB_BUILD_ARCH) = "i386" ]; then $(MAKE) -C build-deb check-abi; fi

	touch $@

build-dbg: build-dbg/build-stamp
build-dbg/build-stamp: build-dbg/config.status
	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
#	 libparted directory, change this if it isn't ...
	$(MAKE) -C build-dbg/libparted

	touch $@

clean: unpatch packaging-files
	dh_testdir
	dh_testroot

#	 Add here commands to clean up after the build process
	rm -rf build-deb build-udeb build-dbg

	rm -f autotools-update-stamp

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

	dh_clean

install: DH_OPTIONS=-Nlibparted$(LIBPKGVER)-dbg
install: build packaging-files
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) -C build-deb 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 libparted.so symlink to point to /lib/libparted.so.x.x.x
#	      rather than being relative & pointing to the current directory
	@(cd debian/tmp/usr/lib; \
	  SONAME=`ls libparted-$(LIBPKGVER).so.*.*.* | \
	          sed -e 's/^.*\.so\.\([0-9]\+\)\..*$$/\1/'`; \
	  if [ "$(LIBSONAME)" -ne "$$SONAME" ]; then \
	     echo " *** error: debian/rules thinks that libparted 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 libparted.so` libparted.so)

	exit
#	 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-dbg: DH_OPTIONS=-plibparted$(LIBPKGVER)-dbg
install-dbg: build-dbg
	dh_testdir
	dh_testroot
	dh_clean -k -plibparted$(LIBPKGVER)-dbg

	install -D -m644 build-dbg/libparted/.libs/libparted.a \
	   debian/libparted$(LIBPKGVER)-dbg/usr/lib/libparted_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_installdocs
	dh_installchangelogs parted/ChangeLog
	dh_installexamples
	dh_installmenu
	dh_installcron
	dh_installman
	dh_installinfo
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
	dh_makeshlibs -V'libparted$(SHAREDLIBPKGVER) (>= $(DEPVER))'
	dh_shlibdeps -Llibparted$(SHAREDLIBPKGVER) \
		-l/debian/libparted$(SHAREDLIBPKGVER)/lib
	dh_installdeb
	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.
# FIXME: this is an ugly hack :-( Work out a better way to build the
# separate debug package ...
binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS="-a -Nlibparted$(LIBPKGVER)-dbg" binary-common
	$(MAKE) -f debian/rules binary-libparted$(LIBPKGVER)-dbg

# 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-%: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-p$* binary-common

binary: binary-arch binary-indep

.PHONY: configure configure-dbg
.PHONY: build build-dbg
.PHONY: install install-dbg
.PHONY: clean binary-indep binary-arch binary
.PHONY: binary-%-dbg binary-%
.PHONY: packaging-files autotools

include /usr/share/dpatch/dpatch.make
