#! /usr/bin/make -f
# -*- makefile -*-
#
# Invoke each target with `./debian/rules <target>'.  All targets should be
# invoked with the package root as the current directory.
#
# The `binary' target must be run as root, as it needs to install files with
# specific ownerships.

-include debian/rules.custom

export DEB_BUILD_MAINT_OPTIONS ?= hardening=+all

# be paranoid
export LC_ALL ?= C

# 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_ARCH		?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_BUILD_ARCH		?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_HOST_OS		?= $(shell dpkg-architecture -qDEB_HOST_OS)
DEB_HOST_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_MULTIARCH	?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH 2>/dev/null)

# Allow distro-specific behaviour
DISTRO :=$(shell sed -ne '/DISTRIB_ID/s/.*=//p' /etc/lsb-release 2>/dev/null || echo Debian)
UTIL_LINUX_NG ?= yes

ifeq ($(DEB_BUILD_GNU_SYSTEM), gnu)
SKIP_FUSE2FS=yes
endif

DH_VERSION := $(shell dpkg-query -W -f '$${Version}' debhelper)

# USE_DBGSYM :=
USE_DBGSYM ?= $(shell if dpkg --compare-versions $(DH_VERSION) ">=" 9.20160114 ; then echo yes ; fi)

ifeq ($(USE_DBGSYM),yes)
dh_strip_args = -p$(1) --dbgsym-migration='$(1)-dbg (<= 1.43-1)'
dh_strip_args2 = -p$(1) --dbgsym-migration='$(2)-dbg (<= 1.43-1)'
else
dh_strip_args = -p$(1) --dbg-package=$(1)-dbg
dh_strip_args2 = -p$(1) --dbg-package=$(2)-dbg
endif
# find the version for the main package, from changelog file
MAIN_VERSION = $(shell head -n 1 debian/changelog | cut '-d ' -f 2 | sed 's/[()]//g')
# find versions for libraries going into their own packages, from their Makefile.in's,
# and sonames for all libs
COMERR_VERSION ?= $(shell grep ELF_VERSION lib/et/Makefile.in | cut '-d ' -f3)
COMERR_SOVERSION ?= $(shell grep ELF_SO_VERSION lib/et/Makefile.in | cut '-d ' -f3)
SS_VERSION ?= $(shell grep ELF_VERSION lib/ss/Makefile.in | cut '-d ' -f3)
SS_SOVERSION ?= $(shell grep ELF_SO_VERSION lib/ss/Makefile.in | cut '-d ' -f3)

ifneq ($(UTIL_LINUX_NG),yes)
UUID_VERSION ?= $(shell grep ELF_VERSION lib/uuid/Makefile.in | cut '-d ' -f3)
UUID_SOVERSION ?= $(shell grep ELF_SO_VERSION lib/uuid/Makefile.in | cut '-d ' -f3)

BLKID_VERSION ?= $(shell grep ELF_VERSION lib/blkid/Makefile.in | cut '-d ' -f3)
BLKID_SOVERSION ?= $(shell grep ELF_SO_VERSION lib/blkid/Makefile.in | cut '-d ' -f3)
endif

EXT2FS_SOVERSION ?= $(shell grep ELF_SO_VERSION lib/ext2fs/Makefile.in | cut '-d ' -f3)
E2P_SOVERSION ?= $(shell grep ELF_SO_VERSION lib/e2p/Makefile.in | cut '-d ' -f3)

package		?= e2fsprogs

topdir		?= $(shell pwd)
debdir		?= ${topdir}/debian
tmpdir		?= ${debdir}/tmp
udebdir		?= ${debdir}/e2fsprogs-udeb
ifneq ($(UTIL_LINUX_NG),yes)
blkidudebdir	?= ${debdir}/libblkid1-udeb
uuidudebdir	?= ${debdir}/libuuid1-udeb
endif
libcomerrdir	?= ${debdir}/libcomerr${COMERR_SOVERSION}
comerrdevdir	?= ${debdir}/comerr-dev
libcomerrdbgdir	?= ${debdir}/libcomerr2-dbg
libssdir	?= ${debdir}/libss${SS_SOVERSION}
ssdevdir	?= ${debdir}/ss-dev
libssdbgdir	?= ${debdir}/libss2-dbg
ifneq ($(UTIL_LINUX_NG),yes)
libblkiddir	?= ${debdir}/libblkid${BLKID_SOVERSION}
libblkiddevdir	?= ${debdir}/libblkid-dev
libblkiddbgdir	?= ${debdir}/libblkid1-dbg
libuuiddir	?= ${debdir}/libuuid${UUID_SOVERSION}
uuiddevdir	?= ${debdir}/uuid-dev
libuuiddbgdir	?= ${debdir}/libuuid1-dbg
uuidruntimedir	?= ${debdir}/uuid-runtime
uuidruntimedbgdir ?= ${debdir}/uuid-runtime-dbg
endif
libext2dir	?= ${debdir}/e2fslibs
libext2devdir	?= ${debdir}/e2fslibs-dev
libext2dbgdir	?= ${debdir}/e2fslibs-dbg
maindir		?= ${debdir}/e2fsprogs
e2fsckstaticdir	?= ${debdir}/e2fsck-static
debugdir	?= ${debdir}/e2fsprogs-dbg
stdbuilddir	?= ${debdir}/BUILD-STD
mipsbuilddir	?= ${debdir}/BUILD-MIPS
mipsbuilddir64	?= ${debdir}/BUILD-MIPS-64
# docdir	?= ${maindir}/usr/share/doc/${package}
MANDIR		?= /usr/share/man
mandir		?= ${tmpdir}${MANDIR}

UDEB_NAME 	?= $(package)-udeb_$(MAIN_VERSION)_$(DEB_HOST_ARCH).udeb
UDEB_PRIORITY	?= $(shell grep '^Package: e2fsprogs-udeb' debian/control.in -A 10 | grep ^Priority: | cut -d ' ' -f 2)

ifneq ($(UTIL_LINUX_NG),yes)
BLKID_UDEB_NAME ?= libblkid1-udeb_$(MAIN_VERSION)_$(DEB_HOST_ARCH).udeb
BLKID_UDEB_PRIORITY ?= $(shell grep '^Package: libblkid1-udeb' debian/control.in -A 10 | grep ^Priority: | cut -d ' ' -f 2)

UUID_UDEB_NAME ?= libuuid1-udeb_$(MAIN_VERSION)_$(DEB_HOST_ARCH).udeb
UUID_UDEB_PRIORITY ?= $(shell grep '^Package: libuuid1-udeb' debian/control.in -A 10 | grep ^Priority: | cut -d ' ' -f 2)
endif

STAMPSDIR	?= debian/stampdir
CFGSTDSTAMP	?= ${STAMPSDIR}/configure-std-stamp
CFGBFSTAMP	?= ${STAMPSDIR}/configure-bf-stamp
BUILDSTDSTAMP	?= ${STAMPSDIR}/build-std-stamp
BUILDBFSTAMP	?= ${STAMPSDIR}/build-bf-stamp

INSTALL ?= install
INSTALL_PROGRAM ?= $(INSTALL) -p -o root -g root -m 0755

#ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
#INSTALL_PROGRAM += -s
#endif

ifneq (,$(findstring update-symbols,$(DEB_BUILD_OPTIONS)))
SYMBOL_LIBS := e2fslibs libcomerr2 libss2
ifneq ($(UTIL_LINUX_NG),yes)
SYMBOL_LIBS += libblkid1 libuuid1
endif
endif

DEFAULT_CFLAGS ?= -g -O2
DEFAULT_LDFLAGS ?= -Wl,-Bsymbolic-functions

CFLAGS ?= $(shell if dpkg-buildflags > /dev/null 2>&1 ; then \
	DEB_BUILD_MAINT_OPTIONS=$(DEB_BUILD_MAINT_OPTIONS) \
	dpkg-buildflags --get CFLAGS; else echo $(DEFAULT_CFLAGS) ; fi)
LDFLAGS ?= $(shell if dpkg-buildflags > /dev/null 2>&1 ; then \
	DEB_BUILD_MAINT_OPTIONS=$(DEB_BUILD_MAINT_OPTIONS) \
	dpkg-buildflags --get LDFLAGS; else echo $(DEFAULT_LDFLAGS) ; fi)
CPPFLAGS ?= $(shell if dpkg-buildflags > /dev/null 2>&1 ; then \
	DEB_BUILD_MAINT_OPTIONS=$(DEB_BUILD_MAINT_OPTIONS) \
	dpkg-buildflags --get CPPFLAGS; fi)

ifeq (${DEB_HOST_ARCH},alpha)
CFLAGS += -DHAVE_NETINET_IN_H
else
CFLAGS += -D__NO_STRING_INLINES
endif

E2FSCK_STATIC = ${stdbuilddir}/e2fsck/e2fsck.static

CFLAGS_SHLIB = $(CFLAGS)
CFLAGS_STLIB = $(CFLAGS)
LDFLAGS_SHLIB = $(LDFLAGS)
LDFLAGS_STATIC = $(filter-out -fPIE -fpie -pie,$(LDFLAGS))

ifneq ($(SKIP_UDEB),)
SKIP_BF ?= yes
endif

ifeq ($(SKIP_BF),yes)
BUILD_BF =
bfbuilddir	?= ${stdbuilddir}
else
BUILD_BF = build-bf
bfbuilddir	?= ${debdir}/BUILD-BF
endif

BF_CFLAGS = -Os -fomit-frame-pointer

ifeq ($(UTIL_LINUX_NG),yes)
UTIL_CONF_FLAGS ?= --disable-fsck --disable-libblkid \
			--disable-libuuid --disable-uuidd
else
UTIL_CONF_FLAGS ?= --enable-fsck --enable-libblkid \
			--enable-libuuid --enable-uuidd
endif

ifneq ($(SKIP_FUSE2FS),)
UTIL_CONF_FLAGS +=  --disable-fuse2fs
endif

ifneq ($(strip $(DEB_HOST_MULTIARCH)),)
MULTIARCH_CONF ?= --with-multiarch=$(DEB_HOST_MULTIARCH)
# This doesn't work yet because gdb and lintian don't expect and/or
# don't work with /usr/lib/<triplet>/debug
#USRLIB = /usr/lib/$(DEB_HOST_MULTIARCH)
USRLIB ?= /usr/lib
else
USRLIB ?= /usr/lib
endif

BACKTRACE_CONF_FLAGS ?= $(shell if ${debdir}/scripts/test-backtrace ; then echo --disable-backtrace ; fi)

COMMON_CONF_FLAGS = --disable-e2initrd-helper --enable-quota \
	--infodir=/usr/share/info  --enable-symlink-install \
	$(MULTIARCH_CONF) $(BACKTRACE_CONF_FLAGS) $(UTIL_CONF_FLAGS)

STD_CONF_FLAGS ?= --enable-elf-shlibs

BF_CONF_FLAGS ?= --enable-elf-shlibs --disable-nls --disable-imager \
	--disable-testio-debug --disable-uuidd --disable-tls \
	--disable-tdb --disable-debugfs

MIPS_NOPIC_CONF_FLAGS ?= --disable-nls --disable-imager \
	--disable-uuidd --disable-tls \
        --disable-resizer # --disable-debugfs

# we can't use those flags at configure time
MIPS_CFLAGS ?= -G 0 -fno-pic -mno-abicalls
MIPS_CFLAGS_64 ?= -mabi=64 -G 0 -fno-pic -mno-abicalls

ifeq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
ifneq (,$(findstring $(DEB_BUILD_ARCH),mips mipsel))
ismips=ismips
endif
endif

M4_ARGS=
DBG_PACKAGES=

ifeq ($(USE_DBGSYM),yes)
M4_ARGS+=-DUSE_DBGSYM
else
DBG_PACKAGES += -pe2fsprogs-dbg -pe2fslibs-dbg -plibcomerr2-dbg -plibss2-dbg
M4_ARGS+=-UUSE_DBGSYM
endif

ifeq ($(UTIL_LINUX_NG),yes)
M4_ARGS+=-DUTIL_LINUX_NG
else
DBG_PACKAGES += -puuid-runtime-dbg -plibuuid1-dbg -plibblkid1-dbg
M4_ARGS+=-UUTIL_LINUX_NG
endif

ifneq ($(strip $(DEB_HOST_MULTIARCH)),)
M4_ARGS+=-DDO_MULTIARCH
else
M4_ARGS+=-UDO_MULTIARCH
endif

ifneq ($(BUILD_E2FSCK_STATIC),no)
M4_ARGS+=-DE2FSCK_STATIC
else
M4_ARGS+=-UE2FSCK_STATIC
endif

ifeq ($(SKIP_UDEB),)
INSTALL_UDEB = install-udeb
M4_ARGS+=-DUDEB_PKGS
else
M4_ARGS+=-UUDEB_PKGS
endif

ifeq ($(SKIP_FUSE2FS),)
M4_ARGS+=-DFUSE2FS
else
M4_ARGS+=-UFUSE2FS
endif

FILES_FIXUP= libcomerr2.files comerr-dev.files libss2.files ss-dev.files \
	libuuid1.files uuid-dev.files libblkid1.files libblkid-dev.files \
	e2fslibs.files e2fslibs-dev.files

debian-files: debian/control debian/e2fsprogs.shlibs.local
ifeq ($(strip $(DEB_HOST_MULTIARCH)),)
	for i in $(FILES_FIXUP); do \
		sed -e 's;lib/\*/;lib/;' debian/$$i.in > debian/$$i; \
	done
else
	for i in $(FILES_FIXUP); do cp debian/$$i.in debian/$$i; done
endif

mrproper: clean
	rm debian/control debian/e2fsprogs.shlibs.local

debian/control: debian/control.in debian/rules
	m4 $(M4_ARGS) < debian/control.in | grep -v ^REMOVE_ME$$ > $@

debian/e2fsprogs.shlibs.local: debian/e2fsprogs.shlibs.local.in
	m4 $(M4_ARGS) < debian/e2fsprogs.shlibs.local.in > $@

${CFGSTDSTAMP}:
	dh_testdir

	# Make sure we don't try to rebuild the configure scripts
	find . -name configure | xargs touch

	mkdir -p ${stdbuilddir}
ifeq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
	cd ${stdbuilddir} && AWK=/usr/bin/awk \
		../../configure ${COMMON_CONF_FLAGS} ${STD_CONF_FLAGS} \
		${EXTRA_CONF_FLAGS} CFLAGS="${CFLAGS}" CPPFLAGS="$(CPPFLAGS)" \
		LDFLAGS="$(LDFLAGS)" CFLAGS_SHLIB="$(CFLAGS_SHLIB)" \
		CFLAGS_STLIB="$(CFLAGS_STLIB)" \
		LDFLAGS_SHLIB="$(LDFLAGS_SHLIB)" \
		LDFLAGS_STATIC="$(LDFLAGS_STATIC)"
else
	cd ${stdbuilddir} && AWK=/usr/bin/awk CC="${DEB_HOST_GNU_TYPE}-gcc" \
		../../configure ${COMMON_CONF_FLAGS} ${STD_CONF_FLAGS} \
		${EXTRA_CONF_FLAGS} --build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(DEB_HOST_GNU_TYPE) CFLAGS="${CFLAGS}" \
		CPPFLAGS="$(CPPFLAGS)" CFLAGS_SHLIB="$(CFLAGS_SHLIB)" \
		CFLAGS_STLIB="$(CFLAGS_STLIB)" \
		LDFLAGS="$(LDFLAGS)" \
		LDFLAGS_SHLIB="$(LDFLAGS_SHLIB)" \
		LDFLAGS_STATIC="$(LDFLAGS_STATIC)"
endif

  # specially-built MIPS libs
ifneq ($(ismips),)
	mkdir -p ${mipsbuilddir} ${mipsbuilddir64}
	cd ${mipsbuilddir} && AWK=/usr/bin/awk \
		../../configure ${COMMON_CONF_FLAGS} \
		${MIPS_NOPIC_CONF_FLAGS} CFLAGS="${CFLAGS}" \
		CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" \
		CFLAGS_SHLIB="$(CFLAGS_SHLIB)" \
		CFLAGS_STLIB="$(CFLAGS_STLIB)" \
		LDFLAGS_SHLIB="$(LDFLAGS_SHLIB)" \
		LDFLAGS_STATIC="$(LDFLAGS_STATIC)"
	cd ${mipsbuilddir64} && AWK=/usr/bin/awk \
		../../configure ${COMMON_CONF_FLAGS} \
		${MIPS_NOPIC_CONF_FLAGS} CFLAGS="${CFLAGS}" \
		CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" \
		CFLAGS_SHLIB="$(CFLAGS_SHLIB)" \
		CFLAGS_STLIB="$(CFLAGS_STLIB)" \
		LDFLAGS_SHLIB="$(LDFLAGS_SHLIB)" \
		LDFLAGS_STATIC="$(LDFLAGS_STATIC)"
endif

	mkdir -p ${STAMPSDIR}
	touch ${CFGSTDSTAMP}

${CFGBFSTAMP}:
	dh_testdir
	rm -f config.cache

	mkdir -p ${bfbuilddir}
ifeq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
	cd ${bfbuilddir} && AWK=/usr/bin/awk \
		../../configure ${COMMON_CONF_FLAGS} ${BF_CONF_FLAGS} \
		${EXTRA_CONF_FLAGS} CFLAGS="${CFLAGS} ${BF_CFLAGS}" \
		CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" \
		CFLAGS_SHLIB="$(CFLAGS_SHLIB)" \
		CFLAGS_STLIB="$(CFLAGS_STLIB)" \
		LDFLAGS_SHLIB="$(LDFLAGS_SHLIB)" \
		LDFLAGS_STATIC="$(LDFLAGS_STATIC)"
else
	cd ${bfbuilddir} && AWK=/usr/bin/awk CC="${DEB_HOST_GNU_TYPE}-gcc" \
		../../configure ${COMMON_CONF_FLAGS} ${BF_CONF_FLAGS} \
		${EXTRA_CONF_FLAGS} --build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(DEB_HOST_GNU_TYPE) CFLAGS="${CFLAGS}" \
		CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" \
		CFLAGS_SHLIB="$(CFLAGS_SHLIB)" \
		CFLAGS_STLIB="$(CFLAGS_STLIB)" \
		LDFLAGS_SHLIB="$(LDFLAGS_SHLIB)" \
		LDFLAGS_STATIC="$(LDFLAGS_STATIC)"
endif
	mkdir -p ${STAMPSDIR}
	touch ${CFGBFSTAMP}

build-arch: build
build-indep: build
build: build-std $(BUILD_BF)

build-std: ${BUILDSTDSTAMP}
${BUILDSTDSTAMP}: ${CFGSTDSTAMP}
	dh_testdir
	if which dh_update_autotools_config > /dev/null 2>&1 ; then \
		dh_update_autotools_config ;\
	fi
	$(MAKE) -C ${stdbuilddir} V=1 all
ifneq ($(BUILD_E2FSCK_STATIC),no)
	$(MAKE) -C ${stdbuilddir}/e2fsck V=1 e2fsck.static
endif
	if ! test -d debian/orig-gmo ; then \
		mkdir debian/orig-gmo ; \
		mv po/*.gmo po/*.po debian/orig-gmo ; \
		cp debian/orig-gmo/*.po po ; \
	fi
	$(MAKE) -C ${stdbuilddir}/po V=1 update-gmo

	( cd ${stdbuilddir}/doc && $(MAKE) V=1 libext2fs.html )
	( cd ${stdbuilddir}/lib/et && $(MAKE) V=1 com_err.info com_err.html )

  # specially-built MIPS libs
ifneq ($(ismips),)
	$(MAKE) -C ${mipsbuilddir}/util V=1
	$(MAKE) -C ${mipsbuilddir} V=1 \
		CFLAGS="${CFLAGS} ${MIPS_CFLAGS}" \
		LIB_SUBDIRS="lib/et lib/ext2fs" libs
	$(MAKE) -C ${mipsbuilddir64}/util V=1
	$(MAKE) -C ${mipsbuilddir64} V=1 \
		CFLAGS="${CFLAGS} ${MIPS_CFLAGS_64}" \
		LIB_SUBDIRS="lib/et lib/ext2fs" libs
endif

	touch ${BUILDSTDSTAMP}

build-bf: ${BUILDBFSTAMP}
${BUILDBFSTAMP}: ${CFGBFSTAMP}
	dh_testdir
	$(MAKE) -C ${bfbuilddir} V=1 libs
	$(MAKE) -C ${bfbuilddir}/e2fsck V=1 all
	$(MAKE) -C ${bfbuilddir}/misc V=1 all
	$(MAKE) -C ${bfbuilddir}/resize V=1 all
	touch ${BUILDBFSTAMP}

clean:
	dh_testdir
	if test -d debian/orig-gmo ; then \
		rm -f po/*.gmo po/*.po ; \
		mv debian/orig-gmo/* po ; \
		rmdir debian/orig-gmo ; \
	fi
	rm -rf ${STAMPSDIR}
	[ ! -f ${stdbuilddir}/Makefile ] || $(MAKE) -C ${stdbuilddir} V=1 distclean
	[ ! -f ${bfbuilddir}/Makefile ] || $(MAKE) -C ${bfbuilddir} V=1 distclean
	[ ! -f ${staticbuilddir}/Makefile ] || $(MAKE) -C ${staticbuilddir} V=1 distclean
	rm -rf ${stdbuilddir} ${bfbuilddir} ${staticbuilddir} ${mipsbuilddir} ${mipsbuilddir64}
	rm -f debian/*.substvars
	dh_clean

install: cleanup install-std

# This rule allows to factorize the dh_clean between the 2 install rules
# This must be launched before install-* (if launching them by hand, for
# exemple) or results are unpredictable
cleanup:
	dh_testdir
	dh_testroot
	dh_prep

install-std: DH_OPTIONS=
install-std: build
	dh_testdir
	dh_testroot
	dh_installdirs

	mkdir -p ${tmpdir}/sbin
	$(MAKE) -C ${stdbuilddir} V=1 install DESTDIR=${tmpdir} \
		INSTALL_PROGRAM="${INSTALL_PROGRAM}" LDCONFIG=true
  # static libs and .h files
	$(MAKE) -C ${stdbuilddir} V=1 install-libs DESTDIR=${tmpdir} LDCONFIG=true

ifneq ($(BUILD_E2FSCK_STATIC),no)
  # statically-linked fsck
	${INSTALL_PROGRAM} $(E2FSCK_STATIC) ${tmpdir}/sbin
	cp ${mandir}/man8/e2fsck.8 ${mandir}/man8/e2fsck.static.8
endif

ifeq ($(DEB_BUILD_GNU_SYSTEM), gnu) 
	${INSTALL} -m 0644 misc/mke2fs-hurd.conf ${tmpdir}/etc/mke2fs.conf
endif

	dh_movefiles
	test -z "`find ${tmpdir} -type f`"

  # specially-built MIPS libs
ifneq ($(ismips),)
	$(INSTALL) -p -m 0644 ${mipsbuilddir}/lib/libext2fs.a \
		${debdir}/e2fslibs-dev/usr/lib/libext2fs-nopic.a
	$(INSTALL) -p -m 0644 ${mipsbuilddir64}/lib/libext2fs.a \
		${debdir}/e2fslibs-dev/usr/lib/lib64ext2fs-nopic.a
endif

install-udeb: DH_OPTIONS=
install-udeb: build
	dh_testdir
	dh_testroot

	$(MAKE) -C ${bfbuilddir} V=1 install-shlibs-libs-recursive DESTDIR=${udebdir} \
		INSTALL_PROGRAM="${INSTALL_PROGRAM}" LDCONFIG=true
	$(MAKE) -C ${bfbuilddir}/e2fsck V=1 install DESTDIR=${udebdir} \
		INSTALL_PROGRAM="${INSTALL_PROGRAM}" LDCONFIG=true
	$(MAKE) -C ${bfbuilddir}/misc V=1 install DESTDIR=${udebdir} \
		INSTALL_PROGRAM="${INSTALL_PROGRAM}" LDCONFIG=true
	$(MAKE) -C ${bfbuilddir}/resize V=1 install DESTDIR=${udebdir} \
		INSTALL_PROGRAM="${INSTALL_PROGRAM}" LDCONFIG=true

	rm -rf ${udebdir}/usr
	find ${udebdir}/sbin -type f -a ! -name e2fsck \
		-a ! -name mke2fs -a ! -name tune2fs \
		-a ! -name resize2fs -a ! -name badblocks -print | xargs rm

	(cd ${udebdir}/sbin; ln -sf e2fsck fsck.ext2 ; \
		ln -sf e2fsck fsck.ext3 ; ln -sf e2fsck fsck.ext4 ; \
		ln -sf mke2fs mkfs.ext2 ; ln -sf mke2fs mkfs.ext3 ; \
		ln -sf mke2fs mkfs.ext4)

ifneq ($(UTIL_LINUX_NG),yes)
	mkdir -p ${blkidudebdir}/lib
	mv ${udebdir}/lib/libblkid.* ${blkidudebdir}/lib

	mkdir -p ${uuidudebdir}/lib
	mv ${udebdir}/lib/libuuid.* ${uuidudebdir}/lib
endif

binary-indep: 
  # no arch-independant debs.

binary-arch: DH_OPTIONS= -a
binary-arch: install $(INSTALL_UDEB)
	dh_testdir
	dh_testroot

	dh_lintian

  # symlinks to prepare dh_installdocs run

ifneq ($(UTIL_LINUX_NG),yes)
	mkdir -p ${debdir}/libblkid${BLKID_SOVERSION}/usr/share/doc/libblkid${BLKID_SOVERSION}
	mkdir -p ${debdir}/libblkid-dev/usr/share/doc
	ln -sf libblkid${BLKID_SOVERSION} ${debdir}/libblkid-dev/usr/share/doc/libblkid-dev
endif

	mkdir -p ${debdir}/libss${SS_SOVERSION}/usr/share/doc/libss${SS_SOVERSION}
	mkdir -p ${debdir}/ss-dev/usr/share/doc
	ln -sf libss${SS_SOVERSION} ${debdir}/ss-dev/usr/share/doc/ss-dev

	mkdir -p ${debdir}/libcomerr${COMERR_SOVERSION}/usr/share/doc/libcomerr${COMERR_SOVERSION}
	mkdir -p ${debdir}/comerr-dev/usr/share/doc
	ln -sf libcomerr${COMERR_SOVERSION} ${debdir}/comerr-dev/usr/share/doc/comerr-dev

ifneq ($(UTIL_LINUX_NG),yes)
	mkdir -p ${debdir}/libuuid${UUID_SOVERSION}/usr/share/doc/libuuid${UUID_SOVERSION}
	mkdir -p ${debdir}/uuid-dev/usr/share/doc
#	ln -sf libuuid${UUID_SOVERSION} ${debdir}/uuid-dev/usr/share/doc/uuid-dev
endif

	mkdir -p ${debdir}/e2fslibs/usr/share/doc/e2fslibs
	mkdir -p ${debdir}/e2fslibs-dev/usr/share/doc
	ln -sf e2fslibs ${debdir}/e2fslibs-dev/usr/share/doc/e2fslibs-dev

ifneq ($(UTIL_LINUX_NG),yes)
	$(INSTALL) -p -m 0644 debian/libblkid.copyright \
		${debdir}/libblkid${BLKID_SOVERSION}/usr/share/doc/libblkid${BLKID_SOVERSION}/copyright
endif

	dh_installdocs -Ne2fsprogs-udeb -Nlibblkid1-udeb -Nlibuuid1-udeb

  # HTML docs
	$(INSTALL) -d ${debdir}/e2fslibs-dev/usr/share/doc/e2fslibs/html-info/
	$(INSTALL) -p -m 0644 ${stdbuilddir}/doc/*.html \
	   ${debdir}/e2fslibs-dev/usr/share/doc/e2fslibs/html-info/
	$(INSTALL) -d ${debdir}/comerr-dev/usr/share/doc/libcomerr${COMERR_SOVERSION}/html-info/
	$(INSTALL) -p -m 0644 ${stdbuilddir}/lib/et/*.html \
	   ${debdir}/comerr-dev/usr/share/doc/libcomerr${COMERR_SOVERSION}/html-info/

  # texinfo docs
	mkdir -p ${debdir}/comerr-dev/usr/share/doc/libcomerr${COMERR_SOVERSION}
	$(INSTALL) -p -m 0644 ${topdir}/doc/libext2fs.texinfo \
	   ${debdir}/e2fslibs-dev/usr/share/doc/e2fslibs/libext2fs.texi
	$(INSTALL) -p -m 0644 ${topdir}/lib/et/com_err.texinfo \
	   ${debdir}/comerr-dev/usr/share/doc/libcomerr${COMERR_SOVERSION}/com_err.texi

	$(INSTALL) -d ${debdir}/comerr-dev/usr/share/doc/libcomerr${COMERR_SOVERSION}/examples
	$(INSTALL) -p -m 0644 lib/ss/ss_err.et \
		${stdbuilddir}/lib/ext2fs/ext2_err.et \
		${debdir}/comerr-dev/usr/share/doc/libcomerr${COMERR_SOVERSION}/examples
	$(INSTALL) -d ${debdir}/ss-dev/usr/share/doc/libss${SS_SOVERSION}/examples
	$(INSTALL) -p -m 0644 debugfs/debug_cmds.ct \
		${debdir}/ss-dev/usr/share/doc/libss${SS_SOVERSION}/examples

	dh_installinfo -pcomerr-dev ${stdbuilddir}/lib/et/com_err.info
	dh_installinfo -pe2fslibs-dev ${stdbuilddir}/doc/libext2fs.info

	dh_installchangelogs
	dh_fixperms
	dh_strip $(call dh_strip_args,e2fsprogs)
ifneq ($(BUILD_E2FSCK_STATIC),no)
	dh_strip $(call dh_strip_args2,e2fsck-static,e2fsprogs)
endif
ifneq ($(ismips),)
	dh_strip $(call dh_strip_args,e2fslibs) -Xlib64ext2fs-nopic.a
else
	dh_strip $(call dh_strip_args,e2fslibs)
endif
	dh_strip $(call dh_strip_args,libss${SS_SOVERSION})
	dh_strip $(call dh_strip_args,libcomerr${COMERR_SOVERSION})
ifneq ($(UTIL_LINUX_NG),yes)
	dh_strip $(call dh_strip_args,libblkid${BLKID_SOVERSION})
	dh_strip $(call dh_strip_args,libuuid${UUID_SOVERSION})
endif
	dh_strip

	# dpkg symbol handling
	for i in $(SYMBOL_LIBS); \
	do \
		echo "Generating symbols for $$i..."; \
		dpkg-gensymbols -p$$i -Pdebian/$$i > debian/$$i.tmp-patch; \
		cat debian/$$i.tmp-patch; \
		patch debian/$$i.symbols < debian/$$i.tmp-patch; \
		/bin/rm debian/$$i.tmp-patch; \
	done

	dh_compress

	dh_makeshlibs --add-udeb=e2fsprogs-udeb
ifeq ($(SKIP_UDEB),)
ifneq ($(UTIL_LINUX_NG),yes)
	echo "udeb: libblkid 1 libblkid1-udeb" >> \
		debian/libblkid1/DEBIAN/shlibs
	echo "udeb: libuuid 1 libuuid1-udeb" >> debian/libuuid1/DEBIAN/shlibs
endif
endif

	dh_installdeb
	dh_shlibdeps -l${stdbuilddir}/lib
	dh_shlibdeps -pe2fsprogs -l${stdbuilddir}/lib \
		-u"-Ldebian/e2fsprogs.shlibs.local"
ifeq ($(SKIP_UDEB),)
	dh_shlibdeps -pe2fsprogs-udeb -l${stdbuilddir}/lib \
		-u"-Ldebian/e2fsprogs-udeb.shlibs.local"
endif
ifeq ($(SKIP_FUSE2FS),)
	dh_shlibdeps -pfuse2fs -l${stdbuilddir}/lib \
		-u"-Ldebian/e2fsprogs.shlibs.local"
endif

	dh_gencontrol -Ncomerr-dev -Nss-dev -Nuuid-dev \
		-Ne2fsprogs-udeb -Nlibblkid1-udeb -Nlibuuid1-udeb
	DH_OPTIONS= dh_gencontrol -pcomerr-dev \
	  -u '-v${COMERR_VERSION}-${MAIN_VERSION} -VmainBinary=${MAIN_VERSION}'
	DH_OPTIONS= dh_gencontrol -pss-dev \
	  -u '-v${SS_VERSION}-${MAIN_VERSION} -VmainBinary=${MAIN_VERSION}'
ifneq ($(UTIL_LINUX_NG),yes)
	DH_OPTIONS= dh_gencontrol -puuid-dev \
	  -u '-v${UUID_VERSION}-${MAIN_VERSION} -VmainBinary=${MAIN_VERSION}'
endif
ifeq ($(SKIP_UDEB),)
	dh_gencontrol	-pe2fsprogs-udeb -- -fdebian/files~
ifneq ($(UTIL_LINUX_NG),yes)
	dh_gencontrol	-plibblkid1-udeb -- -fdebian/files~
	dh_gencontrol	-plibuuid1-udeb -- -fdebian/files~
endif
endif

ifeq ($(SKIP_UDEB),)
	dpkg-distaddfile $(UDEB_NAME) debian-installer $(UDEB_PRIORITY)
ifneq ($(UTIL_LINUX_NG),yes)
	dpkg-distaddfile $(BLKID_UDEB_NAME) debian-installer $(BLKID_UDEB_PRIORITY)
	dpkg-distaddfile $(UUID_UDEB_NAME) debian-installer $(UUID_UDEB_PRIORITY)
endif
endif
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean checkroot mrproper \
	debug_flags debian-files

debug_flags:
	@echo CFLAGS is $(CFLAGS)
	@echo LDFLAGS is $(LDFLAGS)
	@echo CPPFLAGS is $(CPPFLAGS)