#!/usr/bin/make -f

export PYBUILD_INSTALL_ARGS=--install-lib=/usr/share

%:
	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
	dh_install $(addprefix -X,$(notdir $(shell grep -l -e os=.windows. -e os=.macos. $(shell grep -L -e os=.linux. -e os=.unix. cleaners/*.xml) | xargs ls)))
	# 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/" \
		org.bleachbit.BleachBit.desktop > debian/bleachbit/usr/share/applications/bleachbit-root.desktop
