#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable).
# Output every command that modifies files on the build system.

SHELL := sh -e

VERSION := $(shell dpkg-parsechangelog | awk '/^Version: / { print $$2 }' | rev | cut -d- -f2- | rev)

#export DH_VERBOSE = 1


# See FEATURE AREAS in dpkg-buildflags(1).
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# See ENVIRONMENT in dpkg-buildflags(1).
# Package maintainers to append CFLAGS.
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# Package maintainers to append LDFLAGS.
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed


%:
	dh ${@} --buildsystem=pybuild --with=python3


# dh_make generated override targets.
# This is an example for Cmake (see <https://bugs.debian.org/641051>).
#override_dh_auto_configure:
#	dh_auto_configure -- \
#	-DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)

execute_after_dh_auto_clean:
	rm -f RELEASE-VERSION

	# help pybuild
	for ITEM in $$(find . -type d -name "*.egg-info" -or -type f -name "*.egg"); \
	do \
		rm -rf "$${ITEM}"; \
	done

	# remove generated files
	rm -rf deluge/plugins/*/build
	rm -f deluge/ui/web/js/deluge-all-debug.js
	rm -f deluge/ui/web/js/extjs/ext-extensions-debug.js
	rm -f deluge/ui/web/js/gettext.js

	for file in $$(find . -type f -name "*.in"); \
	do \
		rm -f "$$(dirname "$${file}")"/"$$(basename "$${file}" .in)"; \
	done

execute_before_dh_auto_build:
	echo "$(VERSION)" > RELEASE-VERSION

override_dh_auto_test:
	# disabled

execute_after_dh_auto_install:
	# unpacking plugins
	cd debian/tmp/usr/lib/python*/*-packages/deluge/plugins/; \
	for EGG in *.egg; \
	do \
		mkdir -p $${EGG%-py?.*}.egg; \
		unzip $${EGG} -d $${EGG%-py?.*}.egg; \
		rm -f $${EGG}; \
	done

	# removing unused files
	find debian/tmp -type d -name '__pycache__' | xargs rm -rf

execute_after_dh_install:
	if command -v dh_movetousr > /dev/null 2>&1; then dh_movetousr; fi