#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-

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

%:
	dh $@ --parallel
	# --with bash-completion

override_dh_auto_clean:
	dh_auto_clean
	$(MAKE) -C src -f makefile.unix clean

C_UPNP = $(if $(shell dpkg --status libminiupnpc-dev 2>/dev/null),1,-)
override_dh_auto_configure:
	## update translations
	lupdate bitcoin-qt.pro -silent
	## make GUI tests Makefile
	qmake bitcoin-qt.pro USE_QRCODE=1 USE_DBUS=1 USE_UPNP=$(C_UPNP) RELEASE=0 BITCOIN_QT_TEST=1
	mv Makefile Makefile_test
	## make GUI Makefile
	qmake bitcoin-qt.pro USE_QRCODE=1 USE_DBUS=1 USE_UPNP=$(C_UPNP) RELEASE=0
	## Hardening
	perl -0pi -e 's[(CXXFLAGS\s*=)][$$1 $$ENV{CFLAGS} $$ENV{CPPFLAGS}]; \
                      s[(LFLAGS\s*=)][$$1 -Wl,--as-needed $$ENV{LDFLAGS}]; \
                      s[(CFLAGS\s*=)][$$1 $$ENV{CFLAGS} $$ENV{CPPFLAGS}];' \
                     $$(find . -name 'Makefile*')

override_dh_auto_build:
	## build GUI test
	$(MAKE) -f Makefile_test
	## 'clean' before building GUI (test executable will not be deleted)
	$(MAKE) -f Makefile_test clean
	## build GUI
	dh_auto_build
	## build console app.
	$(MAKE) -C src -f makefile.unix litecoind USE_UPNP=$(C_UPNP)
	## build console app. test
	$(MAKE) -C src -f makefile.unix test_litecoin USE_UPNP=$(C_UPNP)
	## build menu and icons
	convert src/qt/res/icons/bitcoin.png -resize 32x32 $(CURDIR)/debian/litecoin.xpm
	convert src/qt/res/icons/bitcoin.png -resize 64x64 $(CURDIR)/debian/litecoin.png
	desktop2menu debian/litecoin-qt.desktop litecoin-qt > debian/litecoin-qt.menu

override_dh_auto_test:
	## Set $HOME to debian/tmp since buildd do not have RW permission to /home/buildd.
	HOME=$(CURDIR)/debian/tmp src/test_litecoin
	-HOME=$(CURDIR)/debian/tmp ./litecoin-qt_test

override_dh_builddeb:
	dh_builddeb -- -Zxz


DPATH := $(abspath $(dir $(MAKEFILE_LIST)))
VER ?= $(shell dpkg-parsechangelog -l$(DPATH)/changelog | perl -ne 'print $$1 if m{Version:\s*([\d\.]+[a-z]?)}')
.PHONY: get-orig-source
get-orig-source:
	@echo "# Downloading..."
	uscan --noconf --verbose --rename --destdir=$(CURDIR) --check-dirname-level=0 --force-download --download-version $(VER) $(DPATH)
