#!/usr/bin/make -f

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

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

# Or shall one use objdump?
SONAMEMAJ=$(shell find debian/ntfs-3g -name 'libntfs-3g.so*' -type f | cut -d. -f 3)

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

%:
	dh ${@} --with autoreconf --parallel 

override_dh_auto_configure:
	dh_auto_configure -- --exec-prefix=NONE --enable-crypto --enable-extras --enable-xattr-mappings --disable-ldconfig --with-fuse=external $(CONFIGURE_FLAGS)

override_dh_auto_install:
	dh_auto_install

	# moving ntfsdecrypt, depends on /usr
	# move the rest to /bin /sbin
	mkdir -p debian/tmp/usr/bin
	mkdir -p debian/tmp/bin
	mv debian/tmp/usr/bin/* debian/tmp/bin
	mv debian/tmp/usr/sbin/* debian/tmp/sbin
	mv debian/tmp/bin/ntfsdecrypt debian/tmp/usr/bin

	# 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
	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
	rm -f debian/tmp/lib/*/*.so

	# removing rpath
	chrpath --delete --keepgoing `find debian/tmp -executable -type f`

override_dh_builddeb:
	dh_builddeb -- -Zxz

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/$(DEB_HOST_MULTIARCH)/$(shell readlink debian/tmp/lib/$(DEB_HOST_MULTIARCH)/libntfs-3g.so.$(SONAMEMAJ)) usr/lib/$(DEB_HOST_MULTIARCH)/libntfs-3g.so

	dh_link --remaining-packages

override_dh_makeshlibs:
	dh_makeshlibs --add-udeb=ntfs-3g-udeb -V'libntfs-3g$(SONAMEMAJ)'

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

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