#! /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.

# be paranoid
export LC_ALL=C

DEB_BUILD_ARCH := $(shell dpkg --print-architecture)
DEB_BUILD_GNU_TYPE := $(shell ./config.guess)
ifndef DEB_HOST_ARCH
  DEB_HOST_ARCH := ${DEB_BUILD_ARCH}
endif
DEB_HOST_GNU_CPU := $(patsubst hurd-%,%,$(DEB_HOST_ARCH))
ifeq ($(filter-out hurd-%,${DEB_HOST_ARCH}),)
  DEB_HOST_GNU_SYSTEM := gnu
else
# FIXME: we won't always have only Hurd and Linux
  DEB_HOST_GNU_SYSTEM := linux
endif
DEB_HOST_GNU_TYPE := ${DEB_HOST_GNU_CPU}-${DEB_HOST_GNU_SYSTEM}


# find the version for the main package, from changelog file
MAIN_VERSION = $(shell head -1 debian/changelog | cut '-d ' -f 2 | sed 's/[()]//g')
# find versions for libraries going into their own packages, from their Makefile.in's
COMERR_VERSION = $(shell grep ELF_VERSION lib/et/Makefile.in | cut '-d ' -f3)
COMERR_MAJOR = $(word 1,$(subst ., ,${COMERR_VERSION}))
SS_VERSION = $(shell grep ELF_VERSION lib/ss/Makefile.in | cut '-d ' -f3)
SS_MAJOR = $(word 1,$(subst ., ,${SS_VERSION}))
UUID_VERSION = $(shell grep ELF_VERSION lib/uuid/Makefile.in | cut '-d ' -f3)
UUID_MAJOR = $(word 1,$(subst ., ,${UUID_VERSION}))

package=e2fsprogs

topdir=$(shell pwd)
debdir=${topdir}/debian
maindir=${debdir}/tmp
builddir=${debdir}/BUILD
docdir=${maindir}/usr/share/doc/${package}
MANDIR=/usr/share/man
mandir=${maindir}${MANDIR}

SUBPACKAGES_DIRS = tmp e2fslibs-dev comerr-dev ss-dev

CCOPTS = -O2 -fsigned-char
LIBC-DEV = libc6-dev

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

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
CCOPTS += -g -O1
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTALL_PROGRAM += -s
endif

ifeq (${DEB_HOST_ARCH},alpha)
CCOPTS += -DHAVE_NETINET_IN_H
LIBC-DEV = libc6.1-dev
else
CCOPTS += -D__NO_STRING_INLINES
endif

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	make -C ${builddir}
	( cd ${builddir}/doc && \
	  texi2html -split_chapter ${topdir}/doc/libext2fs.texinfo )
	( cd ${builddir}/lib/et && make com_err.info && \
	  texi2html -split_chapter -expandinfo ${topdir}/lib/et/com_err.texinfo )
	touch build-stamp

configure-stamp:
	dh_testdir
	mkdir -p ${builddir}
ifeq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
	cd ${builddir} && CFLAGS="${CFLAGS}" \
		${topdir}/configure --with-ccopts="${CCOPTS}" \
		--enable-elf-shlibs --enable-dynamic-e2fsck \
		--enable-nls --enable-compression \
		--mandir=${MANDIR} --infodir=/usr/share/info
else
	cd ${builddir} && CFLAGS="${CFLAGS}" CC="${DEB_HOST_GNU_TYPE}-gcc" \
		${topdir}/configure --with-ccopts="${CCOPTS}" \
		--enable-elf-shlibs --enable-dynamic-e2fsck \
		--enable-nls --enable-compression \
		--mandir=${MANDIR} --infodir=/usr/share/info \
		--build=$(DEB_BUILD_GNU_TYPE) $(DEB_HOST_GNU_TYPE)
endif
	touch configure-stamp

clean:
	dh_testdir
	rm -f build-stamp configure-stamp install
	-make -C ${builddir} -i distclean
	rm -rf ${builddir}
	rm -f doc/libext2fs_*.html lib/et/com_err_*.html
	dh_clean

binary-indep: build
  # no arch-independant debs.

binary-arch: build
	dh_testdir -a
	dh_testroot -a
	dh_clean -a
	dh_installdirs -a

	mkdir -p ${maindir}/sbin
	make -C ${builddir} install DESTDIR=${maindir} INSTALL_PROGRAM="${INSTALL_PROGRAM}" LDCONFIG=true
  # static libs and .h files
	make -C ${builddir} install-libs DESTDIR=${maindir} LDCONFIG=true

  # statically-linked fsck
	${INSTALL_PROGRAM} ${builddir}/e2fsck/e2fsck.static ${maindir}/sbin
	ln -s e2fsck.8.gz ${mandir}/man8/e2fsck.static.8.gz

# docs
  # fully handled by debhelper
	rm -rf ${maindir}/usr/share/info

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

	mkdir -p ${debdir}/e2fsck-static/usr/share/doc/
	ln -sf e2fsprogs ${debdir}/e2fsck-static/usr/share/doc/e2fsck-static

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

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

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

	dh_installdocs -a

	for i in libcomerr${COMERR_MAJOR} libss${SS_MAJOR}; do \
		install -m 644 debian/$$i.copyright ${maindir}/usr/share/doc/$$i/copyright ;\
	done

  # HTML docs
	install -d ${debdir}/e2fslibs-dev/usr/share/doc/e2fslibs-dev/html-info/
	install -p ${builddir}/doc/libext2fs_*.html \
	   ${debdir}/e2fslibs-dev/usr/share/doc/e2fslibs-dev/html-info/
	install -d ${debdir}/comerr-dev/usr/share/doc/comerr-dev/html-info/
	install -p ${builddir}/lib/et/com_err_*.html \
	   ${debdir}/comerr-dev/usr/share/doc/comerr-dev/html-info/

  # texinfo docs
	install -p ${topdir}/doc/libext2fs.texinfo \
	   ${debdir}/e2fslibs-dev/usr/share/doc/e2fslibs-dev/libext2fs.texi
	install -p ${topdir}/lib/et/com_err.texinfo \
	   ${debdir}/comerr-dev/usr/share/doc/libcomerr${COMERR_MAJOR}/com_err.texi

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

  # examples
	dh_installexamples -a

  # dh_installmanpages still makes a mess as of 0.54 (see below dh_movefiles)
	ln -s e2fsck.8 ${mandir}/man8/fsck.ext2.8
	ln -s mke2fs.8 ${mandir}/man8/mkfs.ext2.8

	dh_movefiles -a
  # cleanup empty dirs
	cd ${maindir}/usr/include/ && rmdir * && cd .. && rmdir include/
	cd ${maindir}/usr/share/ && rmdir et ss && cd man && rmdir man3
	cd ${maindir}/usr && rmdir lib

  # broken + useless in 0.52
  #	dh_installmanpages -a

	dh_undocumented -a

	dh_installchangelogs -pe2fsprogs ChangeLog
	for i in libcomerr${COMERR_MAJOR} libss${SS_MAJOR}; do \
		ln -s ../e2fsprogs/changelog.Debian.gz ${maindir}/usr/share/doc/$$i/ ;\
		ln -s ../e2fsprogs/changelog.gz ${maindir}/usr/share/doc/$$i/ ;\
	done

	dh_strip -a
	dh_compress -a
	dh_fixperms -a

	echo "libcdev:Depends=${LIBC-DEV}" >> debian/comerr-dev.substvars
	echo "libcdev:Depends=${LIBC-DEV}" >> debian/ss-dev.substvars
	echo "libcdev:Depends=${LIBC-DEV}" >> debian/uuid-dev.substvars
	echo "libcdev:Depends=${LIBC-DEV}" >> debian/e2fslibs-dev.substvars

	dh_installdeb -a
	dh_shlibdeps -a -l${builddir}/lib
	dh_gencontrol -pe2fsprogs -pe2fslibs-dev -pe2fsck-static
	dh_gencontrol -pcomerr-dev \
	  -u '-v${COMERR_VERSION}-${MAIN_VERSION}'
	dh_gencontrol -pss-dev \
	  -u '-v${SS_VERSION}-${MAIN_VERSION}'
	dh_gencontrol -puuid-dev \
	  -u '-v${UUID_VERSION}-${MAIN_VERSION}'
# We do this one by hand
#	dh_makeshlibs -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean checkroot
