#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

SHELL := sh -e

DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

SONAME = $(shell objdump -p debian/tmp/usr/lib/*/libntfs-3g.so.*.* | awk -Fso. '/SONAME/ { print $$2 }')

ifeq ($(DEB_HOST_ARCH_OS), linux)
CONFIGURE_FLAGS = --enable-posix-acls
endif

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

%:
	dh ${@}

override_dh_auto_configure:
	dh_auto_configure -- --exec-prefix=/usr --enable-crypto \
		--enable-extras --enable-xattr-mappings \
		--enable-quarantined --disable-ldconfig \
		--enable-mount-helper --with-fuse=internal \
		$(CONFIGURE_FLAGS)

override_dh_auto_install:
	dh_auto_install

	# remove symlinks, will recreate them in /usr/sbin using dh_link
	rm debian/tmp/sbin/mount.ntfs-3g
	rm debian/tmp/sbin/mount.lowntfs-3g
	rmdir debian/tmp/sbin

	# 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

	# adding hal policy
	# not used by Debian anymore
	#install -D -m 0644 debian/local/25-ntfs-3g-policy.fdi debian/ntfs-3g/usr/share/hal/fdi/policy/10osvendor/25-ntfs-3g-policy.fdi

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

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

	# moving undocumented programs to ntfs-3g-dev
	mkdir -p debian/ntfs-3g-dev/usr/bin
	mv debian/tmp/usr/bin/ntfsdump_logfile debian/ntfs-3g-dev/usr/bin
	mv debian/tmp/usr/bin/ntfsmftalloc debian/ntfs-3g-dev/usr/bin
	mv debian/tmp/usr/bin/ntfsck debian/ntfs-3g-dev/usr/bin

#override_dh_gencontrol:
#	dh_gencontrol -- -Vntfs-3g:Provides="libntfs-3g$(SONAME)" -Vntfs-3g-udeb:Provides="libntfs-3g-udeb$(SONAME)"

override_dh_installchangelogs:
	dh_installchangelogs debian/local/changelog

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

	dh_link --remaining-packages

execute_before_dh_installdeb:
	chmod 4755 debian/ntfs-3g/usr/bin/ntfs-3g

override_dh_makeshlibs:
	dh_makeshlibs --add-udeb=ntfs-3g-udeb -Vlibntfs-3g$(SONAME)t64

.PHONY: override_dh_auto_configure override_dh_auto_install \
	override_dh_installchangelogs override_dh_link \
	execute_before_dh_installdeb override_dh_makeshlibs
