#!/usr/bin/make -f

export PYBUILD_INSTALL_ARGS=--install-lib=/usr/share
# Run upstream's tests from the source tree, where cleaners/ is. pybuild
# exempts only localhost from its blocking http_proxy, while TestNetwork's
# local server listens on 127.0.0.1. NsisUtilities.py is missing from the
# tarball, and the deselected tests need a user session, swap, the network
# or a file system on a block device.
export PYBUILD_TEST_CUSTOM=1
export PYBUILD_TEST_ARGS=cd {dir}; no_proxy=localhost,127.0.0.1 {interpreter} -m pytest tests \
	--ignore=tests/TestNsisUtilities.py \
	--deselect=tests/TestFileUtilities.py::FileUtilitiesTestCase::test_same_partition \
	--deselect=tests/TestFileUtilities.py::FileUtilitiesTestCase::test_free_space \
	--deselect=tests/TestInit.py::InitTestCase::test_expanduser \
	--deselect=tests/TestMemory.py::MemoryTestCase::test_get_proc_swaps \
	--deselect=tests/TestNetwork.py::NetworkTestCase::test_get_ip_for_url

%:
	dh $@ --buildsystem=pybuild

execute_before_dh_auto_clean:
	find po -name "*.mo" -delete

execute_after_dh_auto_install:
	make -C po install DESTDIR=$(CURDIR)/debian/bleachbit
	# Remove setuptools metadata, useless for a private installation
	rm -rf $(CURDIR)/debian/bleachbit/usr/share/bleachbit-*.egg-info
	# Remove Windows-specific modules
	rm -f $(CURDIR)/debian/bleachbit/usr/share/bleachbit/Winapp.py
	rm -f $(CURDIR)/debian/bleachbit/usr/share/bleachbit/Windows.py
	rm -f $(CURDIR)/debian/bleachbit/usr/share/bleachbit/WindowsWipe.py
	# Remove the macOS-specific module
	rm -f $(CURDIR)/debian/bleachbit/usr/share/bleachbit/Mac.py

override_dh_install:
	# Install the launcher directly, so /usr/bin/bleachbit is a real file
	install -D -m 755 bleachbit.py $(CURDIR)/debian/bleachbit/usr/bin/bleachbit
	# Do not install Windows- and macOS-specific cleaners
	# (negated tokens such as os="!linux" are not handled, none exist yet)
	dh_install $(addprefix -X,$(notdir $(shell grep -lP '<cleaner [^>]*os="(?![^"]*\b(linux|unix)\b)' cleaners/*.xml)))
	# Automatically generate .desktop file to invoke BleachBit as superuser
	sed -e "s/Name=BleachBit/Name=BleachBit \(as root\)/; \
		s/TryExec=bleachbit/TryExec=pkexec/; \
		s/Exec=bleachbit/Exec=pkexec bleachbit/" \
		-e "/^\[Desktop Entry\]/a X-AppStream-Ignore=true" \
		org.bleachbit.BleachBit.desktop > debian/bleachbit/usr/share/applications/bleachbit-root.desktop
