#!/usr/bin/make -f

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

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

# enable all hardening build flags
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

devi := $(shell lsb_release -r -s | cut -d '.'  -f1)
ifeq ($(shell if [ $(devi) -gt 7 ] ; then echo gt ; else echo lt ; fi),gt)
   DEBVE2=yes
else
   DEBVE2=no
endif

DEBVE3=no
ifeq ($(shell if [ $(devi) -gt 8 ] ; then echo gt ; else echo lt ; fi),gt)
   DEBVE3=yes
endif

OLDER=$(shell lsb_release -c -s | cut -d '.'  -f1)

LCFLAGSB=$(shell dpkg-buildflags --get CFLAGS)
LCPFLAGSB=$(shell dpkg-buildflags --get CPPFLAGS)

DEB_HOST_ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)
OPTI_FLAGS_LIBONLY := -fomit-frame-pointer 
### PICCORO optimizer flags for i386 x86 machines
ifeq ($(DEB_HOST_ARCH),i386)
 ifeq ($(OLDER),wheezy)
    OPTI_FLAGS_LIBONLY += -march=i586 -mtune=generic -mmmx
 else
    OPTI_FLAGS_LIBONLY += -march=i686 -mtune=generic -mmmx -msse
 endif
else
ifeq ($(DEB_HOST_ARCH),i486)
 ifeq ($(OLDER),wheezy)
    OPTI_FLAGS_LIBONLY += -march=i586 -mtune=generic -mmmx
 else
    OPTI_FLAGS_LIBONLY += -march=i686 -mtune=generic -mmmx -msse
 endif
endif
endif

ifeq ($(DEB_HOST_ARCH),amd64)
    OPTI_FLAGS_LIBONLY += -mmmx -msse -msse2
endif

export CFLAGS=$(LCFLAGSB) $(OPTI_FLAGS_LIBONLY)
export LDFLAGS=$(shell dpkg-buildflags --get LDFLAGS)
export CXXFLAGS=$(LCPFLAGSB) $(OPTI_FLAGS_LIBONLY)
export CPPFLAGS=$(LCPFLAGSB) $(OPTI_FLAGS_LIBONLY)

%:
	dh $@ --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_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 \
		--enable-aspell \
		--enable-vfs-sftp \
		--enable-vfs-undelfs \
		--enable-tests
#		--enable-vfs-smb=yes

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

override_dh_auto_test:
	# do not stop on failed test(s)
	-dh_auto_test

override_dh_auto_install:
	dh_auto_install --max-parallel=1

override_dh_fixperms:
	chmod 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

override_dh_installchangelogs:
	dh_installchangelogs doc/NEWS

override_dh_strip:
	dh_strip --dbg-package=mc-dbg
ifeq ($(DEBVE3),yes)
	dh_strip --dbgsym-migration='mc-dbg (<< 3:4.8.17~)'
endif

override_dh_builddeb:
	dh_builddeb -- -Zxz


