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

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

# Install directory
DESTDIR=$(CURDIR)/debian/sabnzbdplus

%:
	dh $@ --with python2

override_dh_auto_build:
	# Generate translations
	python tools/make_mo.py
	find locale -type d -empty -delete
	# Create 48x48 png icon for XDG menu
	convert -resize 48x48 interfaces/wizard/static/images/icon_sab.png sabnzbdplus.png

override_dh_clean:
	rm -rf build/ locale/ sabnzbdplus.png
	find . -wholename "./email/*.tmpl" ! -name "*-en.tmpl" -delete
	find . -name "*.pyc" -delete
	dh_clean 

override_dh_install:
	dh_install \
		--exclude=static/MochiKit/ \
		--exclude=wizard/README.TXT \
		--exclude=Config/README.txt \
		--exclude=utils/feedparser.py \
		--exclude=utils/configobj.py \
		--exclude=excanvas/excanvas.js \
		--exclude=images-split.tar.gz \
		--exclude=cherrypy/LICENSE.txt \
		--exclude=cherrypy/cherryd \
		--exclude=cherrypy/scaffold \
		--exclude=/src/
	mv $(DESTDIR)/usr/bin/SABnzbd.py $(DESTDIR)/usr/bin/sabnzbdplus

override_dh_installchangelogs:
	dh_installchangelogs
	dh_installchangelogs -p sabnzbdplus CHANGELOG.txt
	dh_installchangelogs -p sabnzbdplus-theme-plush -k interfaces/Plush/README.txt
	dh_installchangelogs -p sabnzbdplus-theme-smpl -k interfaces/smpl/README.txt
	dh_installchangelogs -p sabnzbdplus-theme-mobile -k interfaces/Mobile/README.txt

override_dh_installinit:
	dh_installinit -- defaults 98 02


DEB_DIR := $(abspath $(dir $(MAKEFILE_LIST)))
DEB_SRC := $(shell dpkg-parsechangelog -l$(DEB_DIR)/changelog --show-field Source)
DEB_TAG := +dfsg
# clean upstream version
UPS_VER := $(shell dpkg-parsechangelog -l$(DEB_DIR)/changelog --show-field Version | sed 's/.*://; s/-[^-]*$$//; s/\+.*//')
# basedir inside upstream tarball
UPS_DIR := SABnzbd-$(UPS_VER)

get-orig-source:
	# download, decompress and rename upstream sources
	uscan --noconf --force --verbose --rename --check-dirname-level=0 --destdir=$(CURDIR) --download-version $(UPS_VER)
	gunzip $(DEB_SRC)_$(UPS_VER).orig.tar.gz
	mv $(DEB_SRC)_$(UPS_VER).orig.tar $(DEB_SRC)_$(UPS_VER)$(DEB_TAG).orig.tar
	# download the missing source files from cvs
	svn export --non-interactive svn://anonscm.debian.org/python-apps/packages/sabnzbdplus/trunk/missing-sources $(CURDIR)/$(UPS_DIR)
	# merge missing sources into upstream tarball and create new orig.tar.gz
	tar --append --owner=root --group=root --mode=a+rX --file $(DEB_SRC)_$(UPS_VER)$(DEB_TAG).orig.tar $(UPS_DIR)
	rm -f $(DEB_SRC)_$(UPS_VER)$(DEB_TAG).orig.tar.gz
	gzip --best $(DEB_SRC)_$(UPS_VER)$(DEB_TAG).orig.tar
	# cleanup leftovers
	rm -rf $(UPS_DIR)
