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

# Append the upstream version 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 = $(DEBPKGVER:-%=)

# The first version of the package to build with the current LIBSONAME
# This should be changed to the current version when LIBSONAME is changed ...
DEPVER = 2.2-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 = 0

# 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 = BINPKGVER DEPVER LIBSONAME DEB_HOST_MULTIARCH

# 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$(LIBSONAME)debian1.files \
	debian/libparted$(LIBSONAME)debian1.lintian-overrides \
	debian/libparted$(LIBSONAME)-dev.files \
	debian/libparted$(LIBSONAME)-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_ARCH_OS))
DEB_BUILD_ARCH_OS = $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)
endif

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

ifeq (, $(DEB_HOST_ARCH))
DEB_HOST_ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)
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 (, $(DEB_HOST_MULTIARCH))
DEB_HOST_MULTIARCH = $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
endif

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

CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS := $(shell dpkg-buildflags --get CFLAGS)
UDEB_CFLAGS := $(patsubst -O2,-Os,$(CFLAGS))
LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)
buildflags := CPPFLAGS="$(CPPFLAGS)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"
udeb_buildflags := CPPFLAGS="$(CPPFLAGS)" CFLAGS="$(UDEB_CFLAGS)" \
		   LDFLAGS="$(LDFLAGS)"

# Tell automake to produce verbose build logs.
export V := 1

# 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

# Cell partition tables get misdetected for pc98; we don't need pc98 support
# on powerpc
ifeq (powerpc, $(DEB_HOST_ARCH))
  CONFFLAGS += --disable-pc98
endif

# Workaround/fix bug #442308
CFLAGS += -fgnu89-inline
UDEB_CFLAGS += -fgnu89-inline

# Enable device-mapper only on Linux
ifeq (linux, $(DEB_BUILD_ARCH_OS))
  CONFDEVMAPPER = --enable-device-mapper
else
  CONFDEVMAPPER = --disable-device-mapper
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$(LIBSONAME)debian1.%: debian/libparted.%
	cp $< $@

debian/libparted$(LIBSONAME)%: 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 "CPPFLAGS:	$(CPPFLAGS)"
	@echo "CFLAGS:		$(CFLAGS)"
	@echo "LDFLAGS:		$(LDFLAGS)"
	@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 "BINPKGVER:	$(BINPKGVER)"
	@echo "GENFILES:	$(GENFILES)"
	@echo "SUBSTLIST:	$(SUBSTLIST)"

packaging-files: $(GENFILES)
	touch $@

debian/stamp-autoreconf:
	dh_autoreconf
	touch $@

build-deb/config.status: debian/stamp-autoreconf
	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 && $(buildflags) ../configure --prefix=/usr \
	    --sbindir=/sbin --mandir=\$${prefix}/share/man \
	    --infodir=\$${prefix}/share/info --enable-mtrace \
	    --enable-shared \
	    --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE) \
		$(CONFDEVMAPPER) \
	    $(CONFFLAGS)
	touch $@

build-udeb/config.status: debian/stamp-autoreconf
	dh_testdir
	[ -d build-udeb ] || mkdir build-udeb

	cd build-udeb && $(udeb_buildflags) ac_cv_header_execinfo_h=no ../configure --prefix=/usr \
	    --sbindir=/sbin --libdir=/lib --mandir=\$${prefix}/share/man \
	    --infodir=\$${prefix}/share/info --enable-mtrace \
	    --enable-shared --disable-static \
	    --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE) \
	    --without-readline $(CONFDEVMAPPER) \
		$(CONFFLAGS)
	touch $@

build build-arch: build-deb build-udeb
build-indep: build-deb

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 amd64 machines.
	if [ $(DEB_BUILD_ARCH) = "amd64" ]; then \
		$(MAKE) -C build-deb check-abi; \
	fi

	touch $@

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

#	 Only build binaries
	$(MAKE) -C build-udeb/lib
	$(MAKE) -C build-udeb/libparted
	$(MAKE) -C build-udeb/parted

	touch $@

clean: packaging-files
	dh_testdir
	dh_testroot

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

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

	dh_autoreconf_clean
	rm -f debian/stamp-autoreconf
	dh_clean

install: install-deb install-udeb
	touch $@

install-deb: DH_OPTIONS=-Nparted$(BINPKGVER)-udeb -Nlibparted$(LIBSONAME)-udeb
install-deb: build-deb packaging-files
	dh_testdir
	dh_testroot
	dh_prep
	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/$(DEB_HOST_MULTIARCH)/libparted.so.x.x.x rather than
#	      being relative & pointing to the current directory
	@(cd debian/tmp/usr/lib; \
	  SONAME=`ls libparted.so.$(LIBSONAME).*.* | \
	          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, set DEPVER to the current" \
	          "version of the" >&2; \
	     echo " *** package that you are building." >&2; \
	     exit 1; \
	  fi; \
	  ln -sf /lib/$(DEB_HOST_MULTIARCH)/`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/$(DEB_HOST_MULTIARCH)
	mv debian/tmp/usr/lib/*.so.* debian/tmp/lib/$(DEB_HOST_MULTIARCH)
	rm -f debian/tmp/usr/lib/*.la

	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
	touch $@

install-udeb: DH_OPTIONS=-pparted$(BINPKGVER)-udeb -plibparted$(LIBSONAME)-udeb
install-udeb: LIBPARTED_UDEB=$(shell dh_listpackages | grep -E '^libparted$(LIBSONAME)-udeb$$')
install-udeb: PARTED_UDEB=$(shell dh_listpackages | grep -E '^parted$(BINPKGVER)-udeb$$')
install-udeb: build-udeb
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	[ -z "$(LIBPARTED_UDEB)" ] || \
	   $(MAKE) -C build-udeb/libparted install \
			DESTDIR=$(CURDIR)/debian/libparted$(LIBSONAME)-udeb

	[ -z "$(LIBPARTED_UDEB)" ] || \
	   rm -f debian/libparted$(LIBSONAME)-udeb/lib/libparted.so \
	         debian/libparted$(LIBSONAME)-udeb/lib/libparted.la

	[ -z "$(PARTED_UDEB)" ] || $(MAKE) -C build-udeb/parted install \
			DESTDIR=$(CURDIR)/debian/parted$(BINPKGVER)-udeb
	touch $@

# Build architecture-independent packages.
# 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: DH_OPTIONS=-i
binary-indep: build-deb install-deb
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installchangelogs
	dh_installexamples
	dh_installmenu
	dh_installcron
	dh_installman
	dh_installinfo
	dh_lintian
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent packages.
binary-arch: DH_OPTIONS=-a
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installchangelogs
	dh_installexamples
	dh_installmenu
	dh_installcron
	dh_installman
	dh_installinfo
	dh_lintian
	dh_strip -plibparted$(LIBSONAME)debian1 \
		--dbg-package=libparted$(LIBSONAME)debian1-dbg
	dh_strip -Nlibparted$(LIBSONAME)debian1
	dh_link
	dh_compress
	dh_fixperms
	dh_makeshlibs -V'libparted$(LIBSONAME)debian1 (>= $(DEPVER))' \
		--add-udeb=$(shell dh_listpackages | grep -E '^libparted$(LIBSONAME)-udeb$$')
	dh_shlibdeps -Llibparted$(LIBSONAME)debian1 \
		-l/debian/libparted$(LIBSONAME)debian1/lib
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-arch binary-indep

.PHONY: build-deb build-udeb build
.PHONY: install-deb install-udeb install
.PHONY: binary-indep binary-arch binary
.PHONY: binary-arch binary-indep binary
.PHONY: packaging-files clean
