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

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

## fix FTBFS@ppc64: https://github.com/lizardfs/lizardfs/issues/249
ifneq (,$(filter $(DEB_HOST_ARCH),armel mips mipsel powerpc powerpcspe sparc hppa m68k sh4))
DEB_LDFLAGS_MAINT_APPEND = -latomic
endif

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

export XSLTPROCFLAGS=--nonet

%:
	dh $@ --buildsystem=cmake --builddirectory=build --with python2,bash-completion

override_dh_auto_clean:
	$(RM) -rv external/gtest
	dh_auto_clean

override_dh_auto_configure:
	## copy gtest sources
	mkdir -p external/gtest
	cp -Rv /usr/src/googletest/googletest/* external/gtest/
	dh_auto_configure --               \
               -DCMAKE_BUILD_TYPE=Release   \
               -DCMAKE_INSTALL_PREFIX="/"   \
               -DCMAKE_VERBOSE_MAKEFILE=ON  \
               -DENABLE_VERBOSE_ASCIIDOC=ON \
               -DENABLE_DEBIAN_PATHS=YES    \
               -DENABLE_TCMALLOC=NO         \
               -DENABLE_UTILS=YES           \
	;
#               -DENABLE_TESTS=YES

override_dh_auto_install:
	dh_auto_install
	## rename config files
	for i in debian/tmp/etc/lizardfs/*.cfg.dist; do \
	    mv -n -v "$$i" "$${i%%.dist}"          \
	;done

override_dh_python2:
	dh_python2
	dh_python2 /usr/share/mfscgi

override_dh_gencontrol:
	dh_gencontrol -- -Vmy:Built-Using="$(foreach pkg,googletest,$(word 2,$(shell dpkg --status $(pkg) | grep ^Package)) (=$(word 2,$(shell dpkg --status $(pkg) | grep Version))),)"

override_dh_compress:
	## See #781131
	dh_compress --exclude="examples/mfsexports.cfg" --exclude="examples/mfsmaster.cfg"

override_dh_installchangelogs:
	dh_installchangelogs NEWS

override_dh_installinit:
	dh_installinit --no-start

override_dh_installsystemd:
	dh_installsystemd --no-stop-on-upgrade --no-enable --no-start

override_dh_strip:
	dh_strip --dbgsym-migration='lizardfs-dbg (<< 3.10.0~)'

## http://wiki.debian.org/onlyjob/get-orig-source
include /usr/share/dpkg/pkg-info.mk
.PHONY: get-orig-source
PKD   = $(abspath $(dir $(MAKEFILE_LIST)))
VER  ?= $(shell sh -c 'printf "$${1%%+dfsg*}"' -- "$(DEB_VERSION_UPSTREAM)")

get-orig-source: $(DEB_SOURCE)_$(VER).orig.tar.xz $(info I: $(DEB_SOURCE)_$(VER))
	@

UURL = https://github.com/lizardfs/lizardfs
$(DEB_SOURCE)_$(VER).orig.tar.xz:
	$(if $(wildcard $(DEB_SOURCE)-$(VER)),$(error $(DEB_SOURCE)-$(VER) exist, aborting..))
	@echo "# Downloading..."
	wget --tries=3 --timeout=40 --read-timeout=40 --continue -O $@ \
          $(UURL)/archive/v$(VER).tar.gz \
        || uscan --noconf --verbose --rename --destdir=$(CURDIR) --check-dirname-level=0 --force-download --download-current-version $(PKD)
	mk-origtargz --repack --repack-suffix "" --compression xz --directory . $@