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

include /usr/share/dpkg/pkg-info.mk

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

export SETUPTOOLS_SCM_PRETEND_VERSION=$(shell echo ${DEB_VERSION} | tr '~-' '+.')

# FTBFS if $LANG is unset
export LANG=C

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

override_dh_auto_build:
	chmod -x $(CURDIR)/in/wpath.py.in
	dh_auto_build

override_dh_auto_test:
	cp -pv $(CURDIR)/.pybuild/cpython3_*/build/wicd/wpath.py src/wicd/wpath.py
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
#	dh_auto_test
	cd tests/
	-PYTHONPATH="..:../../src" python3 -m unittest discover -v
endif

override_dh_auto_clean:
	dh_auto_clean
	-find . \( -name "*.pyc" -o -name "*.pyo" \) -delete
	-rm -rf build/ init/ scripts/ translations/ install.log

override_dh_auto_install:
	dh_auto_install

	cp -rv --update=none $(CURDIR)/debian/skel/* $(CURDIR)/debian/tmp/

	cp -rv $(CURDIR)/src/wicd/encryption/ $(CURDIR)/debian/tmp/etc/wicd/
	cp -rv $(CURDIR)/icons/* $(CURDIR)/debian/tmp/usr/share/icons/hicolor/
	cp -rv $(CURDIR)/images/* $(CURDIR)/debian/tmp/usr/share/wicd/icons/hicolor/
	cp -v $(CURDIR)/other/wicd-tray.desktop $(CURDIR)/debian/tmp/etc/xdg/autostart/
	cp -v $(CURDIR)/other/dhclient.conf.template.default $(CURDIR)/debian/tmp/etc/wicd/
	cp -v $(CURDIR)/other/wicd-gtk.xpm $(CURDIR)/debian/tmp/usr/share/pixmaps/
	cp -v $(CURDIR)/template/other/wicd.conf $(CURDIR)/debian/tmp/usr/share/dbus-1/system.d/
	cp -v $(CURDIR)/template/init/debian/wicd $(CURDIR)/debian/tmp/etc/init.d/
	cp -v $(CURDIR)/template/other/55wicd $(CURDIR)/debian/tmp/usr/lib/pm-utils/sleep.d/
	cp -v $(CURDIR)/template/init/systemd/wicd.service $(CURDIR)/debian/tmp/usr/lib/systemd/system/
	cp -v $(CURDIR)/template/scripts/wicd-* $(CURDIR)/debian/tmp/usr/bin/
	mv -v $(CURDIR)/debian/tmp/usr/bin/wicd $(CURDIR)/debian/tmp/usr/sbin/wicd
	cp -v $(CURDIR)/man/nl/*.1 $(CURDIR)/debian/tmp/usr/share/man/nl/man1/
	cp -v $(CURDIR)/man/nl/*.5 $(CURDIR)/debian/tmp/usr/share/man/nl/man5/
	cp -v $(CURDIR)/man/nl/*.8 $(CURDIR)/debian/tmp/usr/share/man/nl/man8/

	find $(CURDIR)/debian/tmp -name "*.png" -exec chmod 644 {} \;
	find $(CURDIR)/debian/tmp/etc/wicd/ -type f -exec chmod 644 {} \;
	find $(CURDIR)/debian/tmp \( -name "*.glade" -o -name "*.conf" -o -name "*.desktop" \) -exec chmod 644 {} \;

	-find $(CURDIR)/debian/tmp/ -name ".empty_on_purpose" -o -name ".gitkeep" -delete
	-find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete

	-rm -rf $(CURDIR)/debian/tmp/var/run

override_dh_installinit:
	dh_python3 -i
	dh_installinit -i --name=wicd --restart-after-upgrade

override_dh_bugfiles:
	dh_bugfiles -A

override_dh_python3:
	dh_python3
	dh_python3 -p wicd-curses /usr/share/wicd/curses
	dh_python3 -p wicd-daemon /usr/share/wicd/daemon
	dh_python3 -p wicd-gtk /usr/share/wicd/gtk
	dh_python3 -p wicd-cli /usr/share/wicd/cli
