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

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

# This has to be exported to make some magic below work.
export DH_OPTIONS
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

DEB_SOURCE := $(shell dpkg-parsechangelog | grep Source: | sed -e 's/Source: //')
DEB_VERSION := $(shell dpkg-parsechangelog | grep Version: | sed -e 's/Version: //')
DEB_UPSTREAM_VERSION := $(shell echo $(DEB_VERSION) | sed -e 's/-[^-]*$$//')
GIT_URL = https://github.com/Tribler/tribler.git

%:
	dh $@ --with python2

override_dh_auto_configure:
	chmod a-x $(CURDIR)/Tribler/Core/DecentralizedTracking/pymdht/ui/images/*.png
	dh_auto_configure

override_dh_install:
	dh_install --exclude=.git
	rm -f $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/Core/DecentralizedTracking/pymdht/LICENSE.txt
	rm -f $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/LICENSE.txt
	rm -f $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/Main/vwxGUI/images/license
	rm -f $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/Main/webUI/static/LICENSE
	rm -f $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/ns-LICENSE.txt
	rm -rf $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/Test
	rm -rf $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/Main/Build
	rm -rf $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/Player/Build
	rm -rf $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/Transport/Build
	rm -f $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/Main/Utility/Feeds/feedparser.py
	rm -f $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/Core/DecentralizedTracking/pymdht/LGPL-2.1.txt
	rm -f $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/Main/webUI/static/mootools.js
	rm -f $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/Main/webUI/static/excanvas.js
	rm -f $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/dispersy/twisted/plugins/dropin.cache
	rm -f $(CURDIR)/debian/tribler/usr/share/tribler/Tribler/dispersy/libnacl/LICENSE

	dh_link -ptribler usr/share/javascript/mootools/mootools.js usr/share/tribler/Tribler/Main/webUI/static/mootools.js
	dh_link -ptribler usr/share/javascript/excanvas/excanvas.js usr/share/tribler/Tribler/Main/webUI/static/excanvas.js

get-orig-source:
	set -e; if echo $(DEB_VERSION) | grep -c "git"; \
	then \
		git_version=`echo $(DEB_VERSION) | sed -e 's/^.*git\([0-9]*\)*\.\(.*\)-.*$$/\2/g'`; \
	else \
		git_version=$(DEB_UPSTREAM_VERSION); \
	fi; \
	tmpdir=`mktemp -d -t`; \
	cd $$tmpdir; \
	echo "checkout upstream repository ..."; \
	git clone --recursive $(GIT_URL); echo "getting specific upstream revision/tag: $$git_version"; \
	cd `ls | head -n 1`; git checkout -b orig $$git_version; cd ..; \
	tar --exclude=.git --exclude=.gitignore --exclude='*.exe' --exclude='draft-ietf-ppsp-*' \
	-cJvf $(CURDIR)/$(DEB_SOURCE)_$(DEB_UPSTREAM_VERSION).orig.tar.xz `ls | head -n 1`; \
	cd $(CURDIR); \
	rm -rf $$tmpdir
