#!/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.57 2002/03/07 13:20:08 timshel Exp $
#
# Copyright (C) 1999, 2000, 2001 Timshel Knoll
# Licensed under the terms of the GNU General Public License
#
# Based 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 is the debhelper compatability version to use.
export DH_COMPAT=3

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

# The library package version, ie. version 0.2 will build package
# lib$(LIBRARY)0.2, lib$(LIBRARY)0.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.4

# 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.4.13+14pre1

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

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

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


DEB_BUILD_ARCH		?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_BUILD_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

CFLAGS =

ifeq (, $(findstring noopt, $(DEB_BUILD_OPTIONS)))
OPT_CFLAGS = -O2
endif

ifneq (, $(findstring debug, $(DEB_BUILD_OPTIONS)))
DBG_CFLAGS = -g
endif

CFLAGS = $(OPT_CFLAGS) $(DBG_CFLAGS)

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

packaging-files: $(GENFILES)

# by default, just build ...
default: build

configure: packaging-files build/configure-stamp
build/configure-stamp:
	dh_testdir
	[ ! -d build ] && mkdir build || true

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 && CFLAGS="$(CFLAGS)" ../configure --prefix=/usr --without-readline \
	    --sbindir=/sbin --mandir=\$${prefix}/share/man --enable-shared \
	    --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE) \
	    $(CONFFLAGS)

	touch $@

configure-bf: build build-bf/configure-stamp
build-bf/configure-stamp:
	dh_testdir
	[ ! -d build-bf ] && mkdir build-bf || true

ifneq (, $(findstring debug, $(DEB_BUILD_OPTIONS)))
	@echo "warning: option \`debug' in \$$DEB_BUILD_OPTIONS: building with gcc option \`-g'" >&2
endif
	cd build-bf && CFLAGS="$(CFLAGS)" ../configure --prefix=/usr --without-readline \
	    --sbindir=/sbin --mandir=\$${prefix}/share/man --enable-shared \
	    --without-readline $(CONFFLAGS) \
	    --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE) && \
	rm -r libparted && ln -s ../build/libparted .

	touch $@

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

#	 Add here commands to configure the package.
	cd build-dbg && CFLAGS="-g $(CFLAGS)" ../configure --prefix=/usr --without-readline \
	    --enable-mtrace --disable-shared $(CONFFLAGS) \
	    --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-bf: packaging-files configure-bf build-bf/build-stamp
build-bf/build-stamp:
	dh_testdir

#	 Only build binary
	$(MAKE) -C build-bf/parted

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

	touch $@

clean: packaging-files
	dh_testdir
	dh_testroot

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

#	 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.*) \
	      ;; \
	   *) \
	      echo "rm -rf \"debian/$$file\""; \
	      rm -rf "debian/$$file" ;; \
	   esac; \
	done

	-$(MAKE) distclean

	dh_clean

install: DH_OPTIONS=-Nparted-bf -Nlibparted$(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 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/`ls -l libparted.so | awk '{ print $$11 }'` libparted.so)

#	 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-bf: DH_OPTIONS=-pparted-bf
install-bf: build-bf
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

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

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_installdebconf
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installemacsen
#	dh_installpam
#	dh_installinit
	dh_installcron
	dh_installman
	dh_installinfo
#	dh_undocumented
	dh_installchangelogs ChangeLog
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
	dh_makeshlibs -V 'libparted$(LIBPKGVER) (>= $(DEPVER))'
	dh_installdeb
#	dh_perl
	dh_shlibdeps -l$(CURDIR)/debian/libparted$(LIBPKGVER)/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.
# 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 -Nparted-bf" binary-common
	$(MAKE) -f debian/rules binary-parted-bf
	$(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-%-bf: build-bf install-bf
	$(MAKE) -f debian/rules DH_OPTIONS="-p$*-bf" binary-common

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

binary: binary-arch binary-indep

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