#!/usr/bin/make -f

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

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

# minimise needless linking
#export DEB_LDFLAGS_MAINT_APPEND= -Wl,--as-needed

SHELLFILES = etc/mc/mcedit.menu \
etc/mc/edit.indent.rc \
usr/lib/mc/extfs.d/bpp \
usr/lib/mc/extfs.d/urar \
usr/lib/mc/extfs.d/uace \
usr/lib/mc/extfs.d/ulha \
usr/lib/mc/extfs.d/uzoo \
usr/lib/mc/extfs.d/rpm \
usr/lib/mc/extfs.d/audio \
usr/lib/mc/extfs.d/uarc \
usr/lib/mc/extfs.d/ucab \
usr/lib/mc/extfs.d/u7z \
usr/lib/mc/extfs.d/lslR \
usr/lib/mc/extfs.d/uarj \
usr/lib/mc/extfs.d/trpm \
usr/lib/mc/extfs.d/iso9660

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

# use --as-needed only if supported by dh-autoreconf (to simplify backporting)
#DH_AS_NEEDED=$(shell dpkg --compare-versions $$(dpkg --status dh-autoreconf | grep Version | cut -d' ' -f2) ge 6 && echo --as-needed)
override_dh_autoreconf:
	[ ! -x "./autogen.sh" ] || ./autogen.sh
	intltoolize --automake --copy --force
	dh_autoreconf
#	dh_autoreconf $(DH_AS_NEEDED)

# --enable-werror will cause FTBFS on GNU Hurd:
# src/vfs/fish/fish.c: In function 'fish_fh_open':
# fish.c:1517:5: error: 'and' of mutually exclusive equal-tests is always 0 [-Werror]
#
# AWK="awk" is inheritance of 4.7.* series, see http://bugs.debian.org/499723
# might be still necessary for extfs scripts
override_dh_auto_configure:
	dh_auto_configure -- AWK="awk" X11_WWW="x-www-browser" \
		--libexecdir='/usr/lib' \
		--with-x \
		--with-screen=slang \
		--disable-rpath \
		--disable-static \
		--disable-silent-rules \
		--disable-aspell \
		--enable-vfs-sftp \
		--enable-tests \
		--without-ext2undel \
		mc_cv_nroff_tascii=""

#		--enable-vfs-undelfs
#		--enable-vfs-smb=yes

override_dh_auto_build:
	# update translations
	cd po && $(MAKE) update-po
	dh_auto_build

override_dh_auto_test:
	## allow test(s) failure
	-dh_auto_test

override_dh_auto_install:
	dh_auto_install --max-parallel=1

override_dh_fixperms:
	-chmod -c 755 debian/mc/etc/mc/*.rc
	dh_fixperms
	# cons.saver has to be sgid tty to access vcsa
	chgrp tty debian/mc/usr/lib/mc/cons.saver || true
	chmod g+s debian/mc/usr/lib/mc/cons.saver || true
	for f in $(SHELLFILES) ; do \
		sed -i -e 's;/bin/sh;/bin/bash;g' debian/mc/$$f ; \
	done
	-chmod -c 755 debian/mc/usr/lib/mc/fish/*
	-chmod -c 644 debian/mc/usr/lib/mc/fish/README.fish

override_dh_installchangelogs:
	dh_installchangelogs doc/NEWS

#override_dh_strip:
#	dh_strip --dbgsym-migration='mc-dbg (<< 3:4.8.17~)'

.PHONY: get-orig-source
## http://wiki.debian.org/onlyjob/get-orig-source
PKD  = $(abspath $(dir $(MAKEFILE_LIST)))
VER ?= $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m{^Version:\s+(?:\d+:)?(\d.*)(?:\-\d+.*)};')
get-orig-source:
	uscan --noconf --verbose --rename --destdir=$(CURDIR) --check-dirname-level=0 --force-download --download-version $(VER) $(PKD)
