#!/usr/bin/make -f

SHELL := sh -e

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

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
        NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
        MAKEFLAGS += -j$(NUMJOBS)
endif

upstream:
	lynx -dump http://b.andre.pagesperso-orange.fr/changelog.html > debian/local/changelog

%:
	dh ${@} --with autotools_dev

override_dh_auto_configure:
	dh_auto_configure --	--host=$(DEB_HOST_GNU_TYPE) \
				--build=$(DEB_BUILD_GNU_TYPE) \
				--prefix=/usr \
				--exec-prefix=/ \
				--mandir=\$${prefix}/share/man \
				--enable-crypto \
				--enable-extras \
				--enable-posix-acls \
				--enable-xattr-mappings \
				--disable-ldconfig \
				--with-fuse=external \
				CFLAGS="$(CFLAGS)" \
				LDFLAGS="-Wl,-z,defs"

override_dh_auto_install:
	dh_auto_install

	# adding initramfs-tools integration
	install -D -m 0755 debian/local/ntfs-3g.hook debian/ntfs-3g/usr/share/initramfs-tools/hooks/ntfs_3g
	install -D -m 0755 debian/local/ntfs-3g.local-premount debian/ntfs-3g/usr/share/initramfs-tools/scripts/local-premount/ntfs_3g
	install -D -m 0755 debian/local/ntfs-3g.local-bottom debian/ntfs-3g/usr/share/initramfs-tools/scripts/local-bottom/ntfs_3g

	# removing unused files
	rm -f debian/tmp/lib/*.la

	# removing rpath
	for _PROGRAM in \
		bin/lowntfs-3g \
		bin/ntfs-3g \
		bin/ntfs-3g.probe \
		bin/ntfs-3g.secaudit \
		bin/ntfs-3g.usermap \
		bin/ntfscat \
		bin/ntfscluster \
		bin/ntfscmp \
		bin/ntfsck \
		bin/ntfsdecrypt \
		bin/ntfsdump_logfile \
		bin/ntfsfix \
		bin/ntfsinfo \
		bin/ntfsls \
		bin/ntfsmftalloc \
		bin/ntfsmove \
		bin/ntfstruncate \
		bin/ntfswipe \
		sbin/mkntfs \
		sbin/ntfsclone \
		sbin/ntfscp \
		sbin/ntfslabel \
		sbin/ntfsresize \
		sbin/ntfsundelete; \
	do \
		chrpath --delete debian/tmp/$${_PROGRAM}; \
	done

override_dh_installchangelogs:
	dh_installchangelogs debian/local/changelog

override_dh_install:
	dh_install --fail-missing

override_dh_link:
	rm -rf debian/ntfs-3g-dev/usr/share/doc

	# correcting symlink target
	dh_link -pntfs-3g-dev lib/$$(basename $$(readlink debian/tmp/lib/libntfs-3g.so)) usr/lib/libntfs-3g.so

	dh_link --remaining-packages

override_dh_strip:
	dh_strip --dbg-package=ntfs-3g-dbg

override_dh_makeshlibs:
	dh_makeshlibs --add-udeb=ntfs-3g-udeb
