#!/usr/bin/make -f

ARCH=all
DIST=$(shell LC_ALL=C dpkg-parsechangelog -S Distribution)
DEBVER=$(shell LC_ALL=C dpkg-parsechangelog -S Version | sed s/.*://)
TARNAME=dist-upgrader_$(DEBVER)_$(ARCH).tar.gz
DH_ARGS=--with=python3 --buildsystem=pybuild
PY3REQUESTED := $(shell py3versions -r)
PY3DEFAULT := $(shell py3versions -d)
PY3 := $(filter-out $(PY3DEFAULT),$(PY3REQUESTED)) python3

%:
	dh $@ $(DH_ARGS)

override_dh_auto_clean:
	set -ex; for python in $(PY3); do \
		LANG=C.UTF-8 LC_ALL=C.UTF-8 $$python setup.py clean -a; \
	done
	find -name __pycache__ | xargs rm -rf
	rm -rf ./build ./DistUpgrade/$(DEBVER) ./DistUpgrade/mo \
		./DistUpgrade/$(DIST) ./DistUpgrade/$(DIST).tar.gz \
		./DistUpgrade/ubuntu-drivers-obsolete.pkgs ./po/mo

override_dh_auto_build:
	set -ex; for python in $(PY3); do \
		LANG=C.UTF-8 LC_ALL=C.UTF-8 $$python setup.py build; \
	done

override_dh_auto_install:
	set -ex; for python in $(PY3); do \
		LANG=C.UTF-8 LC_ALL=C.UTF-8 $$python setup.py install --root=$(CURDIR)/debian/tmp --install-layout=deb; \
	done
	# Remove .egg-info files
	find debian/tmp -name '*.egg-info' -exec rm -rf {} +

override_dh_python3-indep:
	mkdir -p debian/ubuntu-release-upgrader-gtk/usr/bin
	cp ./debian/ubuntu-release-upgrader-gtk/usr/lib/ubuntu-release-upgrader/check-new-release-gtk debian/ubuntu-release-upgrader-gtk/usr/bin
	dh_python3 -i
	rm ./debian/ubuntu-release-upgrader-gtk/usr/bin/check-new-release-gtk
	rmdir debian/ubuntu-release-upgrader-gtk/usr/bin

override_dh_missing:
	dh_missing --exclude='*.egg-info' --fail-missing

binary: binary-arch binary-indep

binary-indep:
	dh $@ $(DH_ARGS)
	# Special tarball for dist-upgrader
	(cd DistUpgrade/ && \
	   ./build-tarball.sh && \
	   mkdir -p $(DEBVER) && \
	   cp $(DIST).tar.gz ./*ReleaseAnnouncement* $(DEBVER) && \
	   tar czvf ../../$(TARNAME) \
	       $(DEBVER)/*ReleaseAnnouncement* \
	       $(DEBVER)/$(DIST).tar.gz )
	dpkg-distaddfile $(TARNAME) raw-dist-upgrader -

.PHONY: binary binary-indep
