#!/usr/bin/make -f

# Verbose mode
#export DH_VERBOSE=1

export PYBUILD_NAME=swift

export PYBUILD_TEST_ARGS=--verbose -p no:requests_mock test/unit
export PYBUILD_BEFORE_TEST=cp -r {dir}/etc {build_dir}
export PYBUILD_AFTER_TEST=rm -rf {build_dir}/etc

SHELL := /bin/sh -e

DEBVERS ?= $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p')
VERSION ?= $(shell echo '$(DEBVERS)' | sed -e 's/^[[:digit:]]*://' -e 's/[-].*//' -e 's/[+].*//' -e 's/~git.*//g' -e 's/~/./g')
export PBR_VERSION=$(VERSION)

%:
	dh $@ --with python3,sphinxdoc --buildsystem=pybuild

# clean sphinx build output
override_dh_clean:
	dh_clean
	rm -rf doc/build

override_dh_auto_test:
	PATH=$(CURDIR)/bin:$(PATH) PYTHONPATH=$(CURDIR) dh_auto_test

# build with sphinx documentation
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
override_dh_sphinxdoc:
	PYTHONPATH=. sphinx-build -b html doc/source $(CURDIR)/debian/swift-doc/usr/share/doc/swift-doc/html
	dh_sphinxdoc
endif

get-orig-source:
	uscan --verbose --force-download --rename --destdir=../build-area

override_dh_auto_build:
	dh_auto_build
	set -e ; set -x ; \
	for i in $(sort $(wildcard debian/*.init.in)) ; do \
		debian/gen-init $$i ; \
	done

override_dh_auto_install:
	pkgos-dh_auto_install --no-py2 --in-tmp

override_dh_install:
	dh_install --sourcedir=debian/tmp -Xdebian/swift-account-generator -Xdebian/swift-object-generator -Xdebian/swift-container-generator
	dh_missing --fail-missing
	install -D -m 0640 $(CURDIR)/debian/account-server.conf $(CURDIR)/debian/python3-swift/etc/swift/account-server.conf
	install -D -m 0640 $(CURDIR)/debian/container-server.conf $(CURDIR)/debian/python3-swift/etc/swift/container-server.conf
	install -D -m 0640 $(CURDIR)/debian/object-server.conf $(CURDIR)/debian/python3-swift/etc/swift/object-server.conf
	install -D -m 0640 $(CURDIR)/debian/object-expirer.conf $(CURDIR)/debian/python3-swift/etc/swift/object-expirer.conf

override_dh_installinit:
	set -e ; set -x ; \
	for FILE in $(sort $(patsubst debian/%,%,$(wildcard debian/*.init))) ; do \
		PKG=`echo $$FILE | cut -d. -f1` ; \
		NAME=`echo $$FILE | cut -d. -f2` ; \
		dh_installinit --error-handler=true -p$$PKG --name=$$NAME ;\
	done

override_dh_installsystemd:
	set -e ; set -x ; \
	for FILE in $(sort $(patsubst debian/%,%,$(wildcard debian/*.service))) ; do \
		PKG=`echo $$FILE | cut -d. -f1` ; \
		NAME=`echo $$FILE | cut -d. -f2` ; \
		dh_installsystemd -p$$PKG --name=$$NAME ;\
	done

override_dh_compress:
	dh_compress -Xconf-sample
